r - 防止标签与 geom_segment 和 facet 重叠

标签 r ggplot2

我正在使用 geom_segment()facet_wrap() 来显示不同类型和模型的一些估计值。 previous post帮我整理东西,但我正在努力弄清楚如何重叠标签,以免它们重叠。一旦我添加更多模型以与更多数据点进行比较,事情就会变得一团糟。我试过在没有分辨率的情况下更改纵横比。

我如何重叠或展开标签以便它们可读,同时保留 y 轴的比例以便跨模型和类型进行比较?

示例数据

dat <- structure(list(temp = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 
4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 
5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 
1, 2, 3, 4, 5), rev = c(-5, -11, -20, -29, -40, -9, -20, -32, 
-45, -57, -12, -24, -37, -50, -62, -7, -20, -36, -52, -67, -5, 
-13, -23, -35, -47, -12, -24, -36, -48, -58, 0, 0, -3, -7, -12, 
0, 0, 0, 0, -1, -4, -9, -15, -21, -28, 2, 1, -1, -6, -13, -4, 
-7, -8, -8, -6, 8, 16, 23, 29, 34), type = c("Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", 
"Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", 
"Type 3", "Type 4", "Type 4", "Type 4", "Type 4", "Type 4", "Type 4", 
"Type 4", "Type 4", "Type 4", "Type 4", "Type 4", "Type 4", "Type 4", 
"Type 4", "Type 4"), model = c("A", "A", "A", "A", "A", "B", 
"B", "B", "B", "B", "C", "C", "C", "C", "C", "A", "A", "A", "A", 
"A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "A", "A", 
"A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", 
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", 
"C", "C")), .Names = c("temp", "rev", "type", "model"), row.names = c(NA, 
-60L), class = "data.frame")

绘图

df.labeled <- dat %>% 
  ungroup() %>% group_by(type, rev) %>% 
  mutate(label = c(rev[1], rep(NA, length(rev) - 1)))

ggplot(df.labeled, aes(temp, rev, color = model)) + 
  geom_segment(aes(xend = 0, yend = rev), linetype = "dashed", color = "grey") +
  geom_text(aes(label = label, x = -0.1), colour = "black", hjust = 1) +
  geom_vline(xintercept = 0) +
  geom_point() + geom_line() + facet_wrap(~type) + 
  scale_y_continuous(breaks = NULL) + 
  scale_x_continuous(limits = c(-0.5, NA)) +
  theme_bw() + theme(panel.grid = element_blank())

enter image description here

最佳答案

一种选择是错开这些值。但是,我认为直接标记要点更清晰且不易混淆。我在下面展示了这两种方法。

交错值标签

# Set up staggered x-values for the value labels
df.labeled = df.labeled %>% 
  group_by(type, is.na(label)) %>% 
  arrange(rev) %>%
  mutate(xval = rep(c(-0.1,-0.35), ceiling(n()/2))[1:n()])

ggplot(df.labeled, aes(temp, rev, color = model)) + 
  geom_segment(aes(xend=xval - 0.08, yend=rev), linetype="11", color="grey70", size=0.3) +
  geom_text(aes(x=xval, label=label), colour="black", hjust=1, size=2.5) +
  geom_vline(xintercept = 0, colour="grey90", size=0.3) +
  geom_point() + geom_line() + facet_wrap(~type) + 
  scale_y_continuous(breaks = NULL) + 
  scale_x_continuous(limits = c(-0.5, NA)) +
  theme_bw() + theme(panel.grid = element_blank())

enter image description here

使用 y 值作为点标签

ggplot(df.labeled, aes(temp, rev, color = model)) + 
  geom_text(aes(label = rev), size=2.5, show.legend=FALSE) +
  geom_line(alpha=0.3, size=0.7) + 
  facet_wrap(~type) + 
  scale_x_continuous(limits = c(0, NA)) +
  theme_bw() + 
  theme(panel.grid = element_blank()) +
  guides(colour=guide_legend(override.aes=list(alpha=1, size=1)))

enter image description here

关于r - 防止标签与 geom_segment 和 facet 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45785110/

相关文章:

r - 您可以轻松地在 ggplot2 的顶部/右侧绘制地毯/轴吗?

r - dplyr 过滤器中的标量比较

r - ggplot重命名facet_wrap中的facet标签

限制 ggplot+geom_density 上的 y 轴范围

r - 单击传单 map 中的点作为 Shiny 绘图的输入

r - 月-年条形图按年份进行分面和填充;数据输入为字符格式的日期

r - 在ggplot2中包装图例文本

r - 外部函数调用中的 NA/NaN/Inf (arg 5)

根据顺序删除重复项

r - 组合数据框中包含 NA 的行以形成完整的行