Create geom_ribbon for min-max range

Given the following data:

df<-data.frame( year=(1996:2000), a=c(2,1.5,1.5,2,3), b=c(2,2,2,3,4), c=c(2,3,3,1,1)) 

with ggplot:

  ggplot(df,aes(x=year))+ geom_line(aes(y=a))+ geom_line(aes(y=b))+ geom_line(aes(y=c)) 

as follows:

normal plot

as. I can create a tape that always shows the area between the minimum and maximum (think of it as min-max-range) so that it looks like this: better plot with range

+4
r ggplot2
source share
1 answer

Regarding the first calculation of the minimum and maximum values:

 df$min <- apply(df[, -1], 1, min) df$max <- apply(df[, -1], 1, max) 

And then just tape over the tape:

 ggplot(df, aes(x = year, ymin = min, ymax = max)) + geom_ribbon() 

ggplot2 ribbon example

+2
source share

All Articles