对齐ggplot中的标签

问题描述 投票:0回答:1

我正在用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个回答
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(...中的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的重要添加,它将所有内容向左对齐):

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.