在ggplot中对齐标签

时间:2020-03-31 16:28:29

标签: r ggplot2 label geom-text

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

预先感谢

1 个答案:

答案 0 :(得分: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)
        )

给你这个:

enter image description here

我找不到一种很好的方法来使文本在每个条的右边或左边对齐。由于您在数据集中使用position_dodge,因此无法使用nudge_x,例如,将所有内容平均“向右”移动。 It's not allowed to use both in the geom。玩hjust类的作品,但您可以期望在这两行中出现有趣的事情,就像您已经体验到的那样。

还要注意,我已经将您的两个geom_text呼叫合并为一个,您可以在其中使用paste0()paste()进行合并并即时创建自己的标签。

vjust可以使您将它们全都放在相同的高度上-我认为这是您所指的,对吗?

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

将标签放在侧面,轻推+位置闪避

如果要将标签放在侧面,则无法使用nudge_x来这样做,因为您无法在nudge_x中合并positiongeom_text自变量。一种解决方法是基于df$año创建一个新变量以用于标签美观,然后在aes(...的{​​{1}}调用中使用该变量:

geom_text

然后,将所有内容保留在原始绘图调用中,但仅更改最后一个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 调用(请注意,geom_text的重要添加使所有内容都向左对齐):

hjust=0

enter image description here