Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

r - How to highlight area between two lines? ggplot

I have data frame containing variable and it's conf. interval

time x     x.upper   x.lower
   1 1.00     0.91      1.11
   2 1.03     0.92      1.13
   3 1.03     0.95      1.17
   2 1.06     0.90      1.13

I ggplot it:

library(ggplot2)
ggplot(data = df,aes(time,x))+
    geom_line(aes(y = x.upper), colour = 'red') +
    geom_line(aes(y = x.lower), colour = 'blue')+
    geom_line()

I want to highlight area between red and blue lines, smth similar to geom_smooth() function. How can I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A geom_ribbon is exactly what you need

ggplot(data = df,aes(time,x))+
    geom_ribbon(aes(x=time, ymax=x.upper, ymin=x.lower), fill="pink", alpha=.5) +
    geom_line(aes(y = x.upper), colour = 'red') +
    geom_line(aes(y = x.lower), colour = 'blue')+
    geom_line()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...