ggplot2 insert striped reference line from x up to lineplot and filled reference line from lineplot to y

时间:2017-06-15 10:01:31

标签: r ggplot2

I'm trying to reproduce a simple lineplot, but there's a part of it that I don't know the proper term for or how to reproduce in ggplot2. It looks like this: enter image description here

As you can see, there's a purple line that is striped and goes horizontal from the x-axis up to where the striped line and the line plot intersects, and then it turns into a filled line from the intersection point to the y-axis. What is this called? Is there an easy way of doing this in ggplot2? Something similar to geom_abline?

In order to make the intersection you need at least one number, I can confirm the number set is X=100.

I've tried making a solution using BOD data with geom_line, but it doesn't line up properly:

bod_intersect_time <- (BOD %>% mutate(demand=round(demand)) %>% filter(demand==10))$Time
 ggplot() + 
 geom_line(data=BOD, aes(x=Time, y=demand)) + 
 geom_line(data=data.frame(Time=c(0, bod_intersect_time), demand=c(10,10)), aes(x=Time, y=demand, color="line", linetype="full") ) + 
 geom_line(data=data.frame(Time=c(bod_intersect_time, bod_intersect_time), demand=c(10, 0 )), aes(x=Time, y=demand, color="line", linetype="striped"))

Turns into this:

enter image description here

Is there a better way?

Update: Thanks to @Axeman I got the last piece of the puzzle to fix my code:

bod_intersect_time <- approx(BOD$demand, BOD$Time, 10)$y

It now intersects properly: enter image description here

But I still think there should be an easier way, shouldn't it just be something like geom_intersects(data=BOD, y=10)?

0 个答案:

没有答案