如何在 ggplot2 中使用 geom_text 对齐不同长度的标签?

How to align labels with different length using geom_text in ggplot2?

我已经使用 geom_text 为我的绘图添加了标题,我想分别向左和向右对齐标签(因为我使用 facet_grid 并且我有两个不同的组)。

这是我的例子:

library(ggplot2)
library(dplyr)

# DATA
val1 <- c(2.1490626,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
val3 <- c(2.1490626,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)

df1 <- data.frame(value = val1)   
df2 <- data.frame(value = val2)   
df3 <- data.frame(value = val3)   

data <- bind_rows(lst(df1, df2, df3), .id = 'id')
data$Sex <- rep(c("Male", "Female"), times=72)
data$d <- "ff"
data <- as.data.frame(unclass(data), stringsAsFactors = TRUE)

# PLOT

p <- data %>% 
  ggplot(aes(value)) +
  geom_density(lwd = 1.2, colour="red", show.legend = FALSE) +
  geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.2) +
  facet_grid(id ~ Sex ) +
  xlab("type_data") + 
  ylab("Density") +
  ggtitle("title") +
  guides(fill=guide_legend(title="legend_title")) +
  theme(strip.text.y = element_blank())

p

# ADD CAPTION

caption_df = data.frame(value = c(min(data$value), max(data$value)), id = c(rep(tail(levels(data$id), n=1), times=length(levels(data$Sex)))),
                        Sex = c(levels(data$Sex)))
caption_df$txt = c("This is the first caption \nThis is the second caption", "This is the first caption \nThis is the second caption")


yrange <- layer_scales(p)$y$range$range
ypos <- min(yrange) - 0.4 * diff(yrange)


p + coord_cartesian(clip = "off", 
                    ylim = layer_scales(p)$y$range$range, 
                    xlim = layer_scales(p)$x$range$range) +
  geom_text(data = caption_df,
            aes(y = ypos, label = txt)) +
  theme(plot.margin = unit(c(1,1,2,1), "cm"))

这是它的样子:

这就是我想要的:

我试过使用 hjust=0 但它看起来不太好(它们都被置换了)

我试过 hjust=1 但我有同样的问题(移位):

vjust 也没有给我我需要的东西。

有人知道如何解决吗?

提前致谢

您可以在 geom_text 中同时使用 hjustnudge_x

hjust = 0 将 left-align 文字,而且还会移动文字的位置。 nudge_x 通过在文本周围添加一些“填充”来帮助抵消 hjust 的影响。

尝试 nudge_x 的不同值,看看哪一个最符合您的目标。

library(ggplot2)

data %>% 
  ggplot(aes(value)) +
  geom_density(lwd = 1.2, colour="red", show.legend = FALSE) +
  geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.2) +
  facet_grid(id ~ Sex ) +
  xlab("type_data") + 
  ylab("Density") +
  ggtitle("title") +
  guides(fill=guide_legend(title="legend_title")) +
  theme(strip.text.y = element_blank()) + 
  coord_cartesian(clip = "off", 
                  ylim = layer_scales(p)$y$range$range, 
                  xlim = layer_scales(p)$x$range$range) +
  geom_text(data = caption_df,
            aes(y = ypos, label = txt), hjust = 0, nudge_x =  -1) +
  theme(plot.margin = unit(c(1,1,2,1), "cm"))