绘制自定义的垂直线直至曲线

时间:2018-10-31 13:21:10

标签: r ggplot2

我希望垂直线相交后不要超出曲线线

# example data:
x <- 1:50
dat <- data.frame(x = x, y = 1 - exp(-x/43)^4)

ggplot(dat, aes(x = x, y = y)) + geom_line() + geom_vline(xintercept = c(10, 20, 30), lty = "dashed")

enter image description here

1 个答案:

答案 0 :(得分:3)

改为使用 geom_segment

ggplot(dat, aes(x = x, y = y)) + 
  geom_line() + 
  geom_segment(aes(x = x, xend = x, y = min(dat$y), yend = y),
               data = dat[ dat$x %in% c(10, 20, 30), ],
               lty = "dashed")

enter image description here