How to plot area between a predefined upper and lower bounds in ggplot2?

后端 未结 1 1593
渐次进展
渐次进展 2021-01-16 12:17

Data:

Morocco_ObsClim

Morocco_ProjClim

Code:

Morocco_ObservedClim <- read.csv(file = \"Morocco_ObservedClim.csv\", header=TRUE, se         


        
相关标签:
1条回答
  • 2021-01-16 13:10

    I think you are overcomplicating things. Consider:

    df <- subset(
      Morocco_ProjectedClim, 
      DataFormat=="anom" & gcm %in% c("avg", "min", "max") & Timeframe=="annual",
      select=c("sres", "Year", "gcm", "Temp")
    )
    df.cast <- dcast(df, sres + Year ~ gcm)
    library(ggplot2)
    ggplot(df.cast, aes(x=Year, y=avg)) + 
      geom_ribbon(aes(ymin=min, ymax=max, fill=sres), alpha=0.4) + 
      geom_line(aes(color=sres)) +
      scale_fill_manual(values=c("tan1", "red3", "royalblue3", "palegreen3")) +
      scale_color_manual(values=c("brown", "red", "blue", "green4"))
    

    enter image description here

    This does just the temperatures from the projected data set, but should give you a good idea on how to tackle the problem.


    EDIT: this adds actuals:

    ggplot(df.cast, aes(x=Year, y=avg)) + 
      geom_ribbon(aes(ymin=min, ymax=max, fill=sres), alpha=0.4) + 
      geom_line(aes(color=sres)) +
      geom_line(
        data=subset(Morocco_ObservedClim, DataSource == "avg" & DataFormat == "anom" & Timeframe == "annual"),
        aes(x=Year, y=Temp, color="Actual")
      ) +
      scale_fill_manual(values=c("tan1", "red3", "royalblue3", "palegreen3")) +
      scale_color_manual(values=c(`20thC`="brown", A1B="red", A2="blue", B1="green4", Actual="black"))
    

    Though note I didn't update the plot.

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