【问题标题】:Align labels in ggplot在ggplot中对齐标签
【发布时间】:2020-03-31 16:28:29
【问题描述】:

我正在用 ggplot 制作一个情节,但我遇到了一些标签问题。这是我的代码:

ggplot(data = prueba[prueba$Prueba=="lectura",], aes(x = año, 
                          y = Promedio, 
                          group = Tipo, 
                          color = Tipo))+
  geom_point(shape = 18,
             size = 4, position = position_dodge(0.9)) +
  geom_errorbar(aes(ymin = Promedio - Desviacion,
                    ymax = Promedio + Desviacion,
                    width = 0.4),
                position = position_dodge(0.9),
                size = 1.3,
                show.legend = F) +
  ylim(60,140) +
  scale_color_manual(values = c("#A32986",
                                "#A4589E"),
                     breaks = c("Nacional", 
                                "Sena")) +
  theme(
    panel.background = element_rect(fill = "white",
                                    color = "#C6C6C6",
                                    size = 1,
                                    linetype = "solid"),
    panel.grid.minor = element_line(size = 0.1,
                                    linetype = "dashed"),
    axis.title = element_text(color = "#575756",
                              size = 13),
    axis.text = element_text(color = "#575756",
                             size = 11),
    legend.title = element_blank(),
    legend.position = "bottom",
    legend.text = element_text(color = "#575756",
                               size = 13)
    ) + geom_text(aes(label = round(Promedio)),
                  position = position_dodge(0.9),
                  hjust = -0.5,
                  size = 5,
                  color = "#575756") +
  geom_text(aes(label = paste("(", round(Desviacion), ")")),
            color = "#575756",
            position = position_dodge(0.9),
            hjust = -0.1,
            vjust = 2,
            size = 5) + xlab(NULL)

结果是

enter image description here

如您所见,标签未正确对齐。我想组织 de 标签,以便上部标签与下部标签围绕同一轴居中。有谁知道我该怎么做?

我的数据可以通过以下方式重现

structure(list(Tipo = c("Sena", "Sena", "Sena", "Sena", "Sena", 
"Sena", "Sena", "Sena", "Sena", "Sena", "Sena", "Sena", "Sena", 
"Sena", "Sena", "Sena", "Sena", "Sena", "Sena", "Sena", "Sena", 
"Sena", "Sena", "Sena", "Sena", "Sena", "Sena", "Sena", "Sena", 
"Sena", "Sena", "Sena", "Nacional", "Nacional", "Nacional", "Nacional", 
"Nacional", "Nacional", "Nacional", "Nacional", "Nacional", "Nacional", 
"Nacional", "Nacional", "Nacional", "Nacional", "Nacional", "Nacional", 
"Nacional", "Nacional", "Nacional", "Nacional", "Nacional", "Nacional", 
"Nacional", "Nacional", "Nacional", "Nacional", "Nacional", "Nacional", 
"Nacional", "Nacional", "Nacional", "Nacional", "Nacional", "Nacional", 
"Nacional", "Nacional", "Nacional", "Nacional"), año = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L), .Label = c("2016", "2017", "2018", "2019"
), class = "factor"), Promedio = c(96.57, 98.03, 98.51, 100.22, 
98.17, 94.57, 98.07, 98.8, 87.38, 101.45, 99, 99.35, 96.11, 98.82, 
97.1, 95.8, 97.49, 99.86, 90.59, 97.88, 90.6, 95.13, 97.56, 98.25, 
94.03, 89.12, 91.94, 97.21, 94.77, 98.8, 99.04, 98.49, 99, 99, 
100, 100, 100, 100, 99, 100, 98, 101, 100, 96, 100, 90, 102, 
98, 100, 100, 100, 95, 132, 92, 100, 93, 102, 146, 148, 100, 
147, 96, 95, 100, 90, 98, 93, 100, 134, 143), Desviacion = c(19.96, 
20.12, 20.27, 19.78, 19.7, 18.21, 19.85, 20.17, 29.58, 22.13, 
21.98, 19.35, 20.65, 19.78, 21.17, 25.08, 22.66, 21.62, 20.9, 
20.52, 21.32, 18.83, 19.49, 21.09, 21.46, 20.54, 23.93, 21.3, 
20.09, 26.99, 19.72, 21.3, 21, 21, 21, 20, 20, 20, 21, 20, 22, 
23, 21, 22, 20, 28, 23, 25, 23, 22, 20, 22, 31, 21, 22, 22, 22, 
27, 28, 20, 28, 21, 21, 22, 21, 22, 25, 28, 30, 28), Prueba = c("mantenimiento", 
"lectura", "competencias", "promoción", "inglés", "ensamblaje", 
"razonamiento", "comunicación", "mantenimiento", "promoción", 
"comunicación", "lectura", "razonamiento", "inglés", "competencias", 
"ensamblaje", "competencias", "comunicación", "mantenimiento", 
"inglés", "razonamiento", "promoción", "ensamblaje", "lectura", 
"mantenimiento", "razonamiento", "competencias", "lectura", "promoción", 
"comunicación", "ensamblaje", "inglés", "competencias", "razonamiento", 
"inglés", "promoción", "comunicación", "mantenimiento", "lectura", 
"ensamblaje", "competencias", "comunicación", "inglés", "razonamiento", 
"lectura", "mantenimiento", "promoción", "ensamblaje", "competencias", 
"inglés", "ensamblaje", "promoción", "formar", "mantenimiento", 
"lectura", "razonamiento", "comunicación", "enseñar", "evaluar", 
"ensamblaje", "evaluar", "promoción", "mantenimiento", "inglés", 
"razonamiento", "lectura", "competencias", "comunicación", "formar", 
"enseñar")), row.names = c(56L, 57L, 58L, 59L, 60L, 61L, 62L, 
63L, 451L, 452L, 453L, 454L, 455L, 456L, 457L, 458L, 934L, 935L, 
936L, 937L, 938L, 939L, 940L, 941L, 2036L, 2037L, 2038L, 2039L, 
2040L, 2041L, 2042L, 2043L, 24800L, 24801L, 24802L, 24803L, 24804L, 
24805L, 24806L, 24807L, 68059L, 68060L, 68061L, 68062L, 68063L, 
68064L, 68065L, 68066L, 171328L, 171329L, 171330L, 171331L, 171332L, 
171333L, 171334L, 171335L, 171336L, 171337L, 171338L, 339673L, 
339674L, 339675L, 339676L, 339677L, 339678L, 339679L, 339680L, 
339681L, 339682L, 339683L), class = "data.frame")

提前致谢

【问题讨论】:

    标签: r ggplot2 label geom-text


    【解决方案1】:

    我想出了以下内容(请注意,我并没有使用你所有的美学,但应该足以说明解决这个问题的一种可能方法)。

    将标签放在上面的固定高度

    ggplot(data=subset(df, Prueba=='lectura'), aes(x=año, y=Promedio, group=Tipo, color=Tipo)) +
        geom_point(shape=18, size=4, position=position_dodge(0.9)) + 
        geom_errorbar(aes(
                ymin=Promedio-Desviacion, ymax=Promedio+Desviacion, width=0.4
            ), position=position_dodge(0.9), size=1.3, show.legend=F) +
        ylim(60,140) +
        geom_text(aes(
                label = paste0(round(Promedio), "\n(", round(Desviacion), ")"), y=100),
                vjust=-4.2, position=position_dodge(0.9)
            )
    

    给你这个:

    我找不到将文本对齐到每个栏的右侧或左侧的好方法。由于您将position_dodge 与您的数据集一起使用,因此您无法使用nudge_x,例如,将所有内容均等地“向右”移动。 It's not allowed to use both in the geom。玩hjust 之类的作品,但您可以期待这里会发生有趣的事情,就像您已经体验过的那样。

    另请注意,我已将您的两个 geom_text 调用合并为一个,您可以在其中使用 paste0() 或 paste() 即时组合并“制作自己的标签”。

    vjust 全面使用可让您将它们全部放在相同的高度 - 我认为这就是您所指的,对吗?

    再次 - 颜色与您的不同,因为我没有包括您的 scale_color_manual 和 theme 电话。

    将标签放在一边,轻推 + Position_dodge

    如果您想将标签放在一边,您无法使用 nudge_x 执行此操作,因为您无法在 geom_text 中组合 nudge_x 和 position 参数。一种解决方法是基于df$año 创建一个用于标签美学的新变量,然后在geom_text 中的aes(... 调用中使用它:

    df$año_new <- as.numeric(df$año) + 0.1 # nudge of "0.1". Had to force numeric, since + does not work on factors
    

    然后你保留原始 plot 调用中的所有内容,但只更改最后一个 geom_text 调用(注意重要的添加 hjust=0,它将所有内容都对齐到左侧):

    geom_text(aes(
                x=año_new, y=100,
                label = paste0(round(Promedio), "\n(", round(Desviacion), ")")
            ),
            hjust=0,
            position=position_dodge(0.9)
        )
    

    【讨论】:

    • 不错的答案!在geom_text 中设置aes(y = Promedio+Desviacion) 可能会很好,而不是依赖于找到一个有效的常量值。
    • 说得好。我发现将y=100 更改为y=Promedio+Desviacion+5 效果很好。您仍然需要“standoff”常量值将其推到误差线上方。唯一的一点是 OP 似乎提到他们希望标签对齐,所以恒定是最好的方法。
    • 我理解你的所作所为,并且找到了。但是,就我的情节而言,将标签显示在右侧(或左侧)作为点很方便。你有办法通过使用 nudge_x 选项来做到这一点吗?我是否必须通过不使用 position_dodge 来重新构建整个代码并不重要。
    • 问题是position=...和nudge_x=...不能一起使用。
    • 您仍然需要position=,因为您的数据是基于 x=2016 绘制的,例如(将这两个点都放在 x=2016 上)。 dodge 正在努力将它们分开。我已经用另一个类似于上面的例子修改了答案。请注意,我使用 hjust=0 左对齐并发现 0.1 在这里作为“轻推”工作。基本上,该方法是“轻推数据集”,而不是在函数调用中轻推。也许有一种在函数调用中“轻推”的好方法,但不是我在你还需要使用 position=... 时看到的。
    猜你喜欢
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2017-03-11
    • 1970-01-01
    相关资源
    最近更新 更多