我正在尝试使用ggplot2在两个变量的每个点之间绘制一条直线,这些直线在同时观察到。 我查看了geom_segment,但我很难使其适用于我的情况。
这是我的最小工作示例,也是我想要实现的图形(我缺少的部分是蓝色的)。
我将不胜感激!
set.seed(1234)
y <- rnorm(10,0,0.01)
Date <- seq(as.Date("2000/1/1"), by = "day", length.out = 10)
example_df <- tibble(Date,y) %>% mutate(avg = mean(y))
ggplot(example_df, mapping = aes(x = Date)) + geom_point(mapping = aes(y = y)) +
geom_line(aes(y = y)) +
geom_line(aes(y = avg), col = "red")
答案 0 :(得分:5)
geom_segment
将起作用:
ggplot(example_df, aes(x = Date)) +
geom_point(aes(y = y)) +
geom_line(aes(y = y)) +
geom_line(aes(y = avg), col = "red")+
geom_segment(aes(xend = Date, y = y, yend = avg), col = 'blue')