This example shows how to create a ribbon+line plot using the ggplot library

First, let’s open the required libraries

library(raster)
## Loading required package: sp
library(ggplot2)
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

Now let’s read some data to plot. Here we are reading 2 monthly historical temperature datasets from 2 different climate models

Make sure to update these lines according to your directory

dat1 = stack("../../Data/CMIP6/MIROC6/MIROC6_tas_historical.nc")
## Loading required namespace: ncdf4
dat2 = stack("../../Data/CMIP6/NCAR/NCAR_CESM2_tas_historical.nc")

Crop the 2 datasets to my study area

#My study area coordinates
lat1 = -25.0
lat2 = -20.0
lon1 = 307.0
lon2 = 310.0

#My study area domain 
domain = extent(lon1,lon2,lat1,lat2)

#Crop data for my study area
tair1 = crop(dat1,domain)
tair2 = crop(dat2,domain)

Now, let’s calculate the monthly spatial average temperature for both models, and convert the result from Kelvin to degree Celsius

tavg1 = cellStats(tair1,mean)
tavg2 = cellStats(tair2,mean)

#convert temperature from Kelvin to Celcius
tavg1 = tavg1 - 273.16
tavg2 = tavg2 - 273.16

And finally, let’s convert the resulting string of numbers into an proper time series.

Pay attention to the dates. They must agree with the temporal structure of your data. In our case, both datasets are monthly and run from 1979 to 2014.

#nt is the length of the dataset
nt = length(tavg1)
dates = seq(as.Date("1979-01-01"), length=nt, by="months")
tavg1 = xts(tavg1,order.by=dates,frequency=12)
tavg2 = xts(tavg2,order.by=dates,frequency=12)

tavg1 and tavg2 are monthly time series.

We now want to convert tavg1 and tavg2 into annual time series. We can do that with the apply.yearly function from the xts library

#Use function apply.yearly to calculate the annual mean temperatures
tavg_m1 = apply.yearly(tavg1,mean)
tavg_m2 = apply.yearly(tavg2,mean)

The variables tavg_m1 and tavg_m2 have the annual average temperatures from the two climate models.

Next, we calculate the ensemble mean, minimum, and maximum values

#Group the models together to speed up the process
group = cbind(tavg_m1,tavg_m2)  #group models 

ens_mean = apply(group,1,mean)  #ensemble mean values
ens_min  = apply(group,1,min)   #ensemble minimum values
ens_max  = apply(group,1,max)   #ensemble maximum values

Ribbon plots are a good way to display variability within the data, which is not depicted by the ensemble mean alone. In this example, we will use the minimim and maximum temperatures to create a ribbon

To start, let’s create an array containing the years in the datasets 1979 - 2014. This will be helpful for plotting the data

#generate an array with the years we need for the plot
years = seq(1979,2014)

Next, we need to group together all the information that we want to plot into one table. We will use the function cbind for that.

df = data.frame(years,ens_mean,ens_min,ens_max)

Now we can give names to the new table called df. You can use any names.

#Name the table columns of the 
colnames(df) = c("Year","Tmean","Tmin","Tmax")

Let’s take a look at the new table

#Check the 10 rows of the new table
head(df)
##            Year    Tmean     Tmin     Tmax
## 1979-12-01 1979 23.69278 23.27126 24.11430
## 1980-12-01 1980 22.76210 22.43175 23.09246
## 1981-12-01 1981 22.87730 22.82235 22.93225
## 1982-12-01 1982 23.58028 23.50343 23.65714
## 1983-12-01 1983 24.14590 23.65766 24.63413
## 1984-12-01 1984 23.60267 23.22523 23.98010

You can see that all the information needed for the plot is in df.

To plot:

Step 1: start by calling ggplot indicating the table that has the information that we want to plot (df).

Step 2: use geom_ribbon to plot a ribbon with Tmin and Tmax values.

Step 3: use geom_line to plot a line with the Tmean values

The aes parameter is used to indicate which column is to be used for the X-axis and which for the Y-axis. We also name the color for the ribbon and line plots here.

Step 4: add other ggplot fuctions: labs(), theme() to add features to your plot.

p1 = ggplot(df) +
   geom_ribbon(aes(x=Year,
                   ymin=Tmin,
                   ymax=Tmax),
                   fill='blue',alpha=0.2) +
   geom_line(aes(x=Year,
                 y=Tmean),
                 color='blue') +
   labs(x="",y="",title = "Ensemble mean with min/max ribbon") 
print(p1)