This example shows how to create a 2-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_yr1 = apply.yearly(tavg1,mean)
tavg_yr2 = apply.yearly(tavg2,mean)
We are now ready to plot the 2 annual time series.
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.
#binding all 3 variables, tavg_yr1, and tavg_yr2) into one table
df = cbind(years,tavg_yr1,tavg_yr2)
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","MIROC6","NCAR")
Let’s take a look at the new table
#Check the 10 rows of the new table
head(df)
## Year MIROC6 NCAR
## 1979-12-01 1979 23.27126 24.11430
## 1980-12-01 1980 23.09246 22.43175
## 1981-12-01 1981 22.93225 22.82235
## 1982-12-01 1982 23.50343 23.65714
## 1983-12-01 1983 23.65766 24.63413
## 1984-12-01 1984 23.98010 23.22523
You can see that all the information needed for the plot is in df, so we can now call ggplot.
Step 1: start by calling ggplot indicating the table that has the information that we want to plot (df).
Step 2: use geom_line to indicate that you want to draw a line plot, and the parameter aes to indicate which column is to be used for the X-axis and which for the Y-axis. Here we also name the color of the line the same name used for the Y-axis
Step 3: use scale_color_manual to match a color to the name given on step 2
Step 4: add other ggplot fuctions: labs(), theme() to add features to your plot.
p1 = ggplot(df) +
geom_line(aes(x=Year,y=MIROC6,color='MIROC6')) +
geom_line(aes(x=Year, y=NCAR, color='NCAR'))+
scale_color_manual(name='Model',
values=c('MIROC6'='red','NCAR'='blue'))+
labs(x="",y="",title = "Annual Temperature")
print(p1)