我试图通过https://twitter.com/ariamsita来模拟这个图表:
但我不知道如何制作显示橙色线和紫色线之间间隙的线(带有标签)。
最佳答案
这里有一个方法。我们需要为标签和分割创建一个与您自己的数据集不同的数据集。然后我们可以使用geom_line
、geom_point
、geom_segment
和geom_label
:
library(tidyverse)
# create sample data
d <- data.frame(x = rep(1:5, 2),
y = c(1:5, 5:9),
category = rep(c("a", "b"), each = 5),
stringsAsFactors = FALSE)
# filter on a specific x value, and reshape the data to be "wide"
d_wide <- d %>%
filter(x == 5) %>%
spread(category, y)
ggplot(d, aes(x, y))+
geom_line(aes(colour = category))+
geom_point(aes(colour = category))+
geom_segment(data = d_wide,
aes(xend = x, y = a, yend = b))+
geom_label(data = d_wide,
aes(label = b - a, y = (b+a) / 2))
关于r - ggplot : display line that shows gap between two geom_lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61087469/