Search code examples
rplotgraphfill

Fill area between two lines drawn with plot()


how can I fill the whole area between the two lines that were drawn with plot()

plot(years,investments1)
plot(years,investments2)

Thanks for your help.

enter image description here


Solution

  • how about ggplot geom_ribbon?

    library(ggplot2)
    
    set.seed(1)
    df <- data.frame(
      x = seq(1,100),
      ymin = rnorm(100,10,3),
      ymax = rnorm(100,22,2) 
    )
    
    ggplot(df,aes(x=x))+
      geom_line(aes(x,ymin),color="red")+
      geom_ribbon(aes(ymin=ymin,ymax=ymax),fill="lightblue")+
      geom_line(aes(x=x,y=ymax),color="black")
    

    enter image description here