在ggplot中对齐标签

Align labels in ggplot

我正在用 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")

提前致谢

我想到了以下内容(请注意,我没有使用你所有的审美,但应该足以让大家明白这一点,找到一种可能的解决方法)。

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

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 来平均移动所有内容 "to the right"。 It's not allowed to use both in the geom。玩 hjust 种作品,但你可以期待有趣的事情发生在这里与你已经体验过的两条线。

另请注意,我已将您的两个 geom_text 调用合并为一个,您可以在其中使用 paste0()paste() 进行组合并即时使用 "make your own label" .

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

同样 - 颜色与您的不同,因为我没有包括您的 scale_color_manualtheme 电话。

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

如果你想把标签放在一边,你不能用 nudge_x 做到这一点,因为你无法在 [=16= 中组合 nudge_xposition 参数].解决方法是基于 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)
    )