0

I dont understand how to define lower and upper bound for my averages to plot them in ggplot as shown here or here.

My data frame looks like this:

 > data
   scenario decade FutureRangeSize.FullDisp
38    rcp85   2060                    85847
1     rcp45   2020                   376997
75    rcp85   2100                     6328
42    rcp45   2060                    68878
54    rcp85   2060                    44859
37    rcp45   2060                    70499
13    rcp85   2020                   192015
80    rcp85   2060                     1571
48    rcp45   2100                    61264
26    rcp85   2020                   123585
49    rcp85   2100                    72115
58    rcp85   2060                    32498
52    rcp45   2060                    52573
19    rcp85   2020                   139007
61    rcp85   2060                    17571
36    rcp45   2100                    85076
27    rcp45   2060                   234809
84    rcp85   2100                      123
44    rcp85   2020                    52735
71    rcp45   2100                     8967

So there are two scenarions (rcp45, rcp85) and decades ranging from 2020 to 2100. I would like to do an average for each decade for each scenario respectively and plot the lower and upper bound as shaded area in one plot.

Can anybody provide a working example how to do this?

parallax
  • 119
  • 9

1 Answers1

1

You can start with a simple tidyverse

library(tidyverse)

d %>% 
 group_by(scenario, decade) %>% 
 summarise_at(vars(FutureRangeSize.FullDisp), funs(Mean=mean, Sd=sd)) %>% 
  mutate(Sd=ifelse(is.na(Sd),0,Sd)) %>% 
  ggplot(aes(x=decade, y=Mean, color=scenario, fill=scenario)) + 
    geom_point() + 
    geom_ribbon(aes(ymin = Mean - Sd, ymax= Mean + Sd), alpha=0.2)

enter image description here

Or try a stat_summary approach as described here

ggplot(d, aes(x=decade,y=FutureRangeSize.FullDisp, color=scenario)) + 
 geom_point() +
 stat_summary(fun.data = 'mean_sdl', geom = 'smooth', alpha=0.2)
Roman
  • 17,008
  • 3
  • 36
  • 49