Shading confidence intervals manually with ggplot2

后端 未结 1 621
难免孤独
难免孤独 2020-12-15 09:36

I have manually created a data set of life expectancies with accompanying 95% confidence bands. I plot these over the time scale but would prefer the bands to be shaded rat

相关标签:
1条回答
  • 2020-12-15 09:49

    It would be helpful if you provided your own data, but I think the following does what you are after.

    First, create some dummy data:

    ##I presume the lb and ub are lower/upper bound
    pl = data.frame(Time = 0:10, menle = rnorm(11))
    pl$menlelb = pl$menle -1
    pl$menleub = pl$menle +1
    

    Then create the plot. The shaded region is created using geom_ribbon:

    ggplot(pl, aes(Time)) + 
      geom_line(aes(y=menle), colour="blue") + 
      geom_ribbon(aes(ymin=menlelb, ymax=menleub), alpha=0.2)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题