'Plot customised vertical lines up to the curve

I want the vertical lines to not go beyond the curve line after they intersect

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



Solution 1:[1]

Use geom_segment instead:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zx8754