我正在使用geom_segment()并facet_wrap()显示不同类型和模型的一些估算值。上一篇文章帮助我整理了东西,但我正在努力弄清楚如何使标签过大,以免标签重叠。一旦添加更多模型以与更多数据点进行比较,事情就会变得混乱。我尝试过更改宽高比而不进行分辨率。
我如何覆盖或散布标签,以使其易于阅读,同时保留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")
Run Code Online (Sandbox Code Playgroud)
情节
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())
Run Code Online (Sandbox Code Playgroud)
一种选择是将值错开。但是,我认为直接标记这些点更干净,也不会造成混淆。我在下面显示两种方法。
# 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())
Run Code Online (Sandbox Code Playgroud)
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)))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
358 次 |
| 最近记录: |