【问题标题】:ggplot: How to set different alignments on the geom_text position based on type of variable?ggplot:如何根据变量类型在 geom_text 位置上设置不同的对齐方式?
【发布时间】:2022-01-06 13:06:35
【问题描述】:

我有一个 100% 堆积条形图,它显示 3 种类型的变量。我已经设置了一个示例数据库,因此我可以更轻松地创建图表。

我已经用我需要的颜色和信息调整了图表。但我无法独立定位标签。这是当前代码和输出。

代码:

(empilhado<-ggplot(dfm, aes(y = Year, x = abs(value), fill = variable)) + 
  scale_x_continuous(sec.axis = sec_axis(trans = ~.*1, name="Trab."), expand=expansion(mult=c(0,0.05)))+
  geom_col(data = rotulo, aes(y = Year, x=abs(trabalho), fill=NULL), width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y") +
  geom_label(data = rotulo, aes(y= Year, x = abs(trabalho), fill=NULL, label=paste(format(round(trabalho, digits=0), nsmall=0, decimal.mark=",", big.mark="."), 
                                                                                  format(round(aprovado, digits=0), nsmall=0, decimal.mark=",", big.mark="."))
                              ), color="black", size=4, position = position_fill(vjust=1.06)) +
  geom_col(width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y") +
  geom_text(aes(label=format(round(value, digits=0), nsmall=0, decimal.mark=",", big.mark=".")),
            size=4, color="white", position = position_fill(vjust=0.5)) +
  theme(panel.grid.major =   element_line(colour = "gray90",size=0.75), panel.grid.minor =   element_line(colour = "gray90",size=0.75),
        legend.position="top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.title.x = element_blank(), panel.background = element_blank())+
  scale_fill_manual(values = c("#000000","tomato","blue"))

输出:

现在怎么样了? Position_fill(vjust=0.5),所以所有标签都在其各自的栏内居中。

我想要什么?为了能够在左侧设置“Marionete”标签的位置(就像 vjust=0 一样),保持“Pedido”标签原样(在“Pedido”堆叠条的中心)并放置“右侧的 Fatura' 标签(就像 vjust=1 一样)。

提前致谢!

【问题讨论】:

    标签: r ggplot2 label position geom-text


    【解决方案1】:

    实现所需结果的一种选择是手动计算/设置每个标签的位置和水平对齐方式,而不是使用position="fill"

    利用一些随机模拟数据:

    library(ggplot2)
    library(dplyr)
    
    dfm <- dfm %>%
      group_by(Year) %>%
      arrange(desc(variable)) %>%
      mutate(
        pct = value / sum(value),
        x_label = case_when(
          variable == "Marionete" ~ 0,
          variable == "Pedido" ~ .5 * (cumsum(pct) + lag(cumsum(pct))),
          TRUE ~ 1
        ),
        hjust = case_when(
          variable == "Marionete" ~ 0,
          variable == "Pedido" ~ .5,
          TRUE ~ 1
        )
      )
    
    
    ggplot(dfm, aes(y = Year, x = abs(value), fill = variable)) +
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 1, name = "Trab."), expand = expansion(mult = c(0, 0.05))) +
      geom_col(width = .7, colour = "black", lwd = 0.1, position = "fill", orientation = "y") +
      geom_text(aes(x = x_label, label = format(round(value, digits = 0), nsmall = 0, decimal.mark = ",", big.mark = "."), hjust = hjust),
        size = 4, color = "white"
      ) +
      theme(
        panel.grid.major = element_line(colour = "gray90", size = 0.75), panel.grid.minor = element_line(colour = "gray90", size = 0.75),
        legend.position = "top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.title.x = element_blank(), panel.background = element_blank()
      ) +
      scale_fill_manual(values = c("#000000", "tomato", "blue"))
    

    数据

    set.seed(123)
    
    dfm <- data.frame(
      Year = rep(c(2006:2016), each = 3),
      value = sample(1:100, 3 * 11, replace = TRUE),
      variable = c("Fatura", "Pedido", "Marionete")
    )
    dfm$variable <- factor(dfm$variable, levels = c("Fatura", "Pedido", "Marionete"))
    dfm$Year <- factor(dfm$Year)
    

    【讨论】:

    • 太棒了!我试图像您一样在数据中强制使用它,但是您的行 variable == "Pedido" ~ .5 * (cumsum(pct) + lag(cumsum(pct))) 确实解决了我的 hjust(0.5) 问题。简单有效,非常感谢!我只将代码中的值更改为 abs(value),因为我必须将负数输出为正数。再次感谢!
    猜你喜欢
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2017-02-26
    • 2014-06-15
    • 1970-01-01
    相关资源
    最近更新 更多