dat <- data.frame(id = rep(1:4,each = 7), year = rep(2012:2018, times = 4),
y = runif(28), start = rep(2012:2015,each = 7), end = rep(2014:2017,each = 7))
ggplot(dat, aes(x = year, y = y)) + geom_line() + facet_wrap(~id)
如何为每个id插入两条垂直线,其位置由相应的起点和终点列给出?
答案 0 :(得分:1)
试试这个:
library(dplyr)
library(tidyr)
vlines <- dat %>%
select(id,start,end) %>%
distinct() %>%
gather(key = grp,value = x,start,end)
ggplot(dat, aes(x = year, y = y)) +
geom_line() +
facet_wrap(~id) +
geom_vline(data = vlines,aes(xintercept = x))