setwd("C:/Users/Vicente/repo/ClimatePanama/ClimatePanamaBook")
library(terra)
library(ggplot2)
library(rasterVis)
library(dplyr)
library(basemaps)
library(sf)
library(gridExtra)
library(scico)
library(tidyr)
6 Plots and Figures
6.1 Libraries
We utilize rasterVis, basemaps and gridExtra packages for the figures presented on the paperAuguie and Antonov (2017). The package sf is used for vectors (Pebesma 2018). Finally, scico package its used for color-blind safe palettes (Crameri 2023).
6.2 Figure 1. Sites plot
Read in files
setwd("C:/Users/Vicente/repo/ClimatePanama/ClimatePanamaBook")
<- "../data_ground/met_data/ACP_data/cleanedVV/ACP_met_stations.csv"
ACP_MET<- "../tables/stri_met_stations.csv"
STRI_MET
<- read.csv(STRI_MET)
striStation<- read.csv(ACP_MET)
acpStation
<- terra::ext(c(-80.2,-79.4, 8.8, 9.5)) extentMap
Stations wrangling and cleaning
<- striStation %>%
striStation ::mutate(
dplyrlongitude = as.character(longitude),
longitude = readr::parse_number(longitude)
)$source<- "STRI"
striStation$source<-"ACP"
acpStation<-read.csv(file="../tables/climatologies.csv") precipitation_data_subset
Combine sites data into a single dataset
<-data.frame(unique(precipitation_data_subset$site))%>%
siterename(site=unique.precipitation_data_subset.site.)%>%
left_join(striStation[,c('alias','latitude','longitude','source')], by = c("site" = "alias"))%>%
left_join(acpStation[,c('site','latitude','longitude','source')], by = c("site" = "site"))%>%
mutate(latitude=ifelse(is.na(latitude.x),latitude.y,latitude.x))%>%
mutate(longitude=ifelse(is.na(longitude.x),longitude.y,longitude.x))%>%
mutate(source=ifelse(is.na(source.x),source.y,source.x))%>%
::select(site,latitude,longitude,source)%>%filter(!is.na(latitude))
dplyr$longitude<- abs(site$longitude)*-1
site<- st_as_sf(site, coords = c("longitude","latitude"), crs = 4326)
site_sf <- st_transform(site_sf, 3857)
site_sf_3857 <- st_coordinates(site_sf_3857)
site_coords <- cbind(site, site_coords) site_df
Plot using the color blind safe terrain color palette
set_defaults(map_service = "esri", map_type = "world_shaded_relief")
<- st_bbox(c(xmin = -80.2, xmax = -79.4, ymin = 8.7, ymax = 9.6), crs = st_crs(4326))
bbox
<- c(-80.13, -79.95, -79.77, -79.59, -79.41)
x_labels_4326 <- c(8.77, 8.95, 9.12, 9.30, 9.48)
y_labels_4326
# Add scales with correct labels
basemap_ggplot(bbox)+
scale_x_continuous(breaks = c(-8920000, -8900000, -8880000, -8860000, -8840000), labels = x_labels_4326) +
scale_y_continuous(breaks = c(980000, 1000000, 1020000, 1040000, 1060000), labels = y_labels_4326)+
theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
panel.grid.major = element_blank(), # Remove major grid lines
panel.grid.minor = element_blank(),
axis.text = element_text(size = 12),
panel.background = element_blank())
Loading basemap 'world_shaded_relief' from map service 'esri'...
Using geom_raster() with maxpixels = 386529.
<-basemap_ggplot(bbox) +geom_point(data = site_df, aes(x = X, y = Y, color = source), size = 2) +
pscale_x_continuous(breaks = c(-8920000, -8900000, -8880000, -8860000, -8840000), labels = x_labels_4326) +
scale_y_continuous(breaks = c(980000, 1000000, 1020000, 1040000, 1060000), labels = y_labels_4326) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(size = 12),
panel.background = element_blank())
Loading basemap 'world_shaded_relief' from map service 'esri'...
Using geom_raster() with maxpixels = 386529.
<- subset(site_df, !site %in% c("BCIELECT", "BCICLEAR", "BALBOAHTS", "DIABLO","ESCANDALOSA","PELUCA"))
site_df2<-subset(site_df, site %in% c("BCIELECT"))
BCIELECT<-subset(site_df, site %in% c("BCICLEAR"))
BCICLEAR<-subset(site_df, site %in% c("BALBOAHTS"))
BALBOAHTS<-subset(site_df, site %in% c("DIABLO"))
DIABLO<-subset(site_df, site %in% c("ESCANDALOSA"))
ESCANDALOSA<-subset(site_df, site %in% c("PELUCA"))
PELUCA
<-p +
sites_plotgeom_text(data = site_df2, aes(x = X, y = Y, label = site), color = "black", size = 2, nudge_y = 1000, fontface = "bold") +
geom_text(data = BCIELECT, aes(x = X, y = Y, label = site), color = "black", size = 2, nudge_x = -3900, fontface = "bold") +
geom_text(data = BCICLEAR, aes(x = X, y = Y, label = site), color = "black", size = 2, nudge_x = 4250, fontface = "bold") +
geom_text(data = BALBOAHTS, aes(x = X, y = Y, label = site), color = "black", size = 2, nudge_y = -1000, fontface = "bold") +
geom_text(data = DIABLO, aes(x = X, y = Y, label = site), color = "black", size = 2, nudge_x = -3000, fontface = "bold") +
geom_text(data = ESCANDALOSA, aes(x = X, y = Y, label = site), color = "black", size = 2, nudge_x = -3000, nudge_y = 1000, fontface = "bold") +
geom_text(data = PELUCA, aes(x = X, y = Y, label = site), color = "black", size = 2, nudge_x = -3000, fontface = "bold") +
labs(color = "Source")
ggsave("sites.tiff", path = "../plots", plot = sites_plot, dpi = 900, units = "in",width = 7.25, height=7.25)
sites_plot
6.3 Figure 2. Climatologies
Total annual precipitation, excluding the lowest correlation CHELSA w5e5
<-function(list,extent){
annual_pan<-terra::rast(list)
b<-terra::app(b,sum)
c<-terra::crop(c,extent)
dreturn(d)
}
Stack the climatologies and plot them with the same resolution
# CHELSA 1.2
<-list.files("../data_reanalysis/CHELSA 1.2/",full.names=TRUE,pattern = ".tif")
chelsa_orig<-annual_pan(chelsa_orig,extentMap)
chelsa_origplot(chelsa_orig)
#CHIRPS annual precip at 0.05
<-list.files("../data_reanalysis/CHIRPS 2.0/climatology",full.name=TRUE,pattern=".tif")
chirps<-annual_pan(chirps,extentMap)
chirps<-terra::project(chirps,chelsa_orig, method='near')
chirpsvalues(chirps)<-ifelse(values(chirps)<0,NA,values(chirps))
plot(chirps)
#chelsa 2.1 annual precip 0,0083333 degree orig
<-list.files("../data_reanalysis/CHELSA 2.1",full.names=TRUE,pattern = ".tif")
chelsa2_orig<-annual_pan(chelsa2_orig,extentMap)
chelsa2_origplot(chelsa2_orig)
#chp 0.05 degree original 1980-2009
<- list.files("../data_reanalysis/CHPclim",full.names=TRUE,pattern = ".tif")
chp<-annual_pan(chp,extentMap)
chp<-terra::project(chp,chelsa_orig, method='near')
chpplot(chp)
#pbcor 0.05 degree origina
<- list.files("../data_reanalysis/PBCOR/CHPclim corrected",full.names=TRUE,pattern = ".tif")
pbcor_chp<-annual_pan(pbcor_chp,extentMap)
pbcor_chp<-terra::project(pbcor_chp,chelsa_orig, method='near')
pbcor_chpplot(pbcor_chp)
#pbcor 0.05 degree original
<- list.files("../data_reanalysis/PBCOR/CHELSA 1.2 corrected",full.names=TRUE)
pbcor_chelsa<-annual_pan(pbcor_chelsa,extentMap)
pbcor_chelsa<-terra::project(pbcor_chelsa,chelsa_orig,method='near')
pbcor_chelsaplot(pbcor_chelsa)
#terra
<- list.files("../data_reanalysis/TerraClimate",full.names = TRUE,pattern = "\\.tif$")
terra<-annual_pan(terra,extentMap)
terra<-terra::project(terra,chelsa_orig,method = 'near')
terraplot(terra)
#Chelsa earthenv 2003-2016
<-list.files("../data_reanalysis/CHELSA EarthEnv/climatology",full.names = TRUE,pattern = ".tif")
chelsaearthenv<-annual_pan(chelsaearthenv,extentMap)
chelsaearthenvplot(chelsaearthenv)
#Pbcorworldclim
<-list.files("../data_reanalysis/PBCOR/WorldClim corrected",full.names = TRUE,pattern = ".tif")
pbcor_worldclim<-annual_pan(pbcor_worldclim,extentMap)
pbcor_worldclim<-terra::project(pbcor_worldclim,chelsa_orig,method="ngb") pbcor_worldclim
Warning: [project] argument 'method=ngb' is deprecated, it should be
'method=near'
plot(pbcor_worldclim)
#worldclim
<-list.files("../data_reanalysis/WorldClim 2.1",full.names=TRUE,pattern=".tif")
worldclim<-annual_pan(worldclim,extentMap)
worldclimplot(worldclim)
Stack the plots, define a list and plot in a grid
<-c(chelsa_orig,chelsa2_orig,chelsaearthenv,pbcor_chelsa,pbcor_chp,pbcor_worldclim,terra,chp,worldclim,chirps)
res_stack
<-list('layer.1'="CHELSA 1.2",
models'layer.2'="CHELSA 2.1",
'layer.3'="CHELSA EarthEnv",
'layer.4'="PBCOR CHELSA 1.2",
'layer.5'="PBCOR CHPclim",
'layer.6'="PBCOR WorldClim",
'layer.7'="TerraClimate",
'layer.8'="CHPclim v.1.0",
'layer.9'="WorldClim v2.1",
'layer.10'="CHIRPS v2")
<- function(variable,value){
model2return(models[value])
}
<-gplot(res_stack) +
jf1geom_raster(aes(fill = value)) +
facet_wrap(~ variable,labeller=model2,ncol = 5) +
scale_fill_scico(palette = 'roma',na.value="white",name = "mm/year")+
coord_equal()+theme_void()+theme(strip.text.x = element_text(size = 8, colour = "black"))+
labs(title = "Total Annual Precipitation")+
theme(plot.title = element_text(hjust = 0.5,size = 16))
ggsave(path = '../plots',filename = "annual_maps.tiff", units="in", width=10, height=6, dpi=900)
jf1
January to April Precipitation individual plots.
# CHELSA 1.2
<-list.files("../data_reanalysis/CHELSA 1.2/",full.names=TRUE,pattern = ".tif")
chelsa_orig<-chelsa_orig[1:4]
chelsa_orig<-annual_pan(chelsa_orig,extentMap)
chelsa_origplot(chelsa_orig)
#CHIRPS annual precip at 0.05
<-list.files("../data_reanalysis/CHIRPS 2.0/climatology",full.name=TRUE,pattern=".tif")
chirps<-chirps[1:4]
chirps<-annual_pan(chirps,extentMap)
chirps<-terra::project(chirps,chelsa_orig, method='near')
chirpsvalues(chirps)<-ifelse(values(chirps)<0,NA,values(chirps))
plot(chirps)
#chelsa 2.1 annual precip 0,0083333 degree orig
<-list.files("../data_reanalysis/CHELSA 2.1",full.names=TRUE,pattern = ".tif")
chelsa2_orig<- chelsa2_orig[1:4]
chelsa2_orig<-annual_pan(chelsa2_orig,extentMap)
chelsa2_origplot(chelsa2_orig)
#chp 0.05 degree original 1980-2009
<- list.files("../data_reanalysis/CHPclim",full.names=TRUE,pattern = ".tif")
chp<- chp[1:4]
chp<-annual_pan(chp,extentMap)
chp<-terra::project(chp,chelsa_orig, method='near')
chpplot(chp)
#pbcor 0.05 degree origina
<- list.files("../data_reanalysis/PBCOR/CHPclim corrected",full.names=TRUE,pattern = ".tif")
pbcor_chp<-pbcor_chp[1:4]
pbcor_chp<-annual_pan(pbcor_chp,extentMap)
pbcor_chp<-terra::project(pbcor_chp,chelsa_orig, method='near')
pbcor_chpplot(pbcor_chp)
#pbcor 0.05 degree original
<- list.files("../data_reanalysis/PBCOR/CHELSA 1.2 corrected",full.names=TRUE)
pbcor_chelsa<-pbcor_chelsa[1:4]
pbcor_chelsa<-annual_pan(pbcor_chelsa,extentMap)
pbcor_chelsa<-terra::project(pbcor_chelsa,chelsa_orig,method='near')
pbcor_chelsaplot(pbcor_chelsa)
#terra
<- list.files("../data_reanalysis/TerraClimate",full.names = TRUE,pattern = "\\.tif$")
terra<-terra[1:4]
terra<-annual_pan(terra,extentMap)
terra<-terra::project(terra,chelsa_orig,method = 'near')
terraplot(terra)
#Chelsa earthenv 2003-2016
<-list.files("../data_reanalysis/CHELSA EarthEnv/climatology",full.names = TRUE,pattern = ".tif")
chelsaearthenv<-chelsaearthenv[1:4]
chelsaearthenv<-annual_pan(chelsaearthenv,extentMap)
chelsaearthenvplot(chelsaearthenv)
#Pbcorworldclim
<-list.files("../data_reanalysis/PBCOR/WorldClim corrected",full.names = TRUE,pattern = ".tif")
pbcor_worldclim<-pbcor_worldclim[1:4]
pbcor_worldclim<-annual_pan(pbcor_worldclim,extentMap)
pbcor_worldclim<-terra::project(pbcor_worldclim,chelsa_orig,method="ngb") pbcor_worldclim
Warning: [project] argument 'method=ngb' is deprecated, it should be
'method=near'
plot(pbcor_worldclim)
#worldclim
<-list.files("../data_reanalysis/WorldClim 2.1",full.names=TRUE,pattern=".tif")
worldclim<-worldclim[1:4]
worldclim<-annual_pan(worldclim,extentMap)
worldclimplot(worldclim)
Stack the rasters and plot in a grid.
<-c(chelsa_orig,chelsa2_orig,chelsaearthenv,pbcor_chelsa,pbcor_chp,pbcor_worldclim,terra,chp,worldclim,chirps)
res_stack_jfma
<-list('layer.1'="CHELSA 1.2",
models'layer.2'="CHELSA 2.1",
'layer.3'="CHELSA EarthEnv",
'layer.4'="PBCOR CHELSA 1.2",
'layer.5'="PBCOR CHPclim",
'layer.6'="PBCOR WorldClim",
'layer.7'="TerraClimate",
'layer.8'="CHPclim v.1.0",
'layer.9'="WorldClim v2.1",
'layer.10'="CHIRPS v2")
<- function(variable,value){
model2return(models[value])
}
<-gplot(res_stack_jfma) +
jf2geom_raster(aes(fill = value)) +
facet_wrap(~ variable,labeller=model2,ncol = 5) +
scale_fill_scico(palette = 'roma',na.value="white",name = "mm")+
coord_equal()+theme_void()+theme(strip.text.x = element_text(size = 8, colour = "black"))+
labs(title = "January to April Precipitation")+
theme(plot.title = element_text(hjust = 0.5,size = 16))
ggsave(path = '../plots',filename = "jfma_maps.tiff", units="in", width=10, height=6, dpi=900)
jf2
Arrange both combined plots in a grid.
<-grid.arrange(jf1,jf2,ncol = 1) b
ggsave(b,path='../plots' ,filename = "../plots/climatologies.tiff", units="in", width=7.25, height=5.25, dpi=900)
b
TableGrob (2 x 1) "arrange": 2 grobs
z cells name grob
1 1 (1-1,1-1) arrange gtable[layout]
2 2 (2-2,1-1) arrange gtable[layout]
6.4 Figure 3. Annual Bias and January to April Bias
Create data frames for bias of each response variable
<- read.csv(file="../tables/climatologies.csv")
precipitation_data_subset <- c("CHELSA.1.2", "CHELSA.2.1", "CHELSA.EarthEnv", "CHPclim", "WorldClim.2.1", "CHIRPS.2.0", "CHELSA.W5E5v1.0", "TerraClimate", "PBCOR.CHELSA.1.2", "PBCOR.CHPclim", "PBCOR.WorldClim.2.1")
data_sources <- c("CHELSA 1.2", "CHELSA 2.1", "CHELSA EarthEnv", "CHPclim", "WorldClim 2.1", "CHIRPS 2.0", "CHELSA-W5E5v1.0", "TerraClimate", "PBCOR CHELSA 1.2", "PBCOR CHPclim", "PBCOR WorldClim 2.1")
source_names
<- precipitation_data_subset %>%
bias_frame select("site","longitude","latitude","annual","source") %>%
mutate(bias=NA)
<- precipitation_data_subset %>%
bias_frame_jfmaselect("site","longitude","latitude","jfma","source") %>%
mutate(bias=NA)
for(i in seq_along(data_sources)) {
$source == source_names[i], "bias"] <- precipitation_data_subset[precipitation_data_subset$source == source_names[i], data_sources[i]] - precipitation_data_subset[precipitation_data_subset$source == source_names[i], "annual"]
bias_frame[bias_frame
}<- paste0(data_sources, "_jfma")
jfma_sources for(i in seq_along(jfma_sources)) {
$source == source_names[i], "bias"] <- precipitation_data_subset[precipitation_data_subset$source == source_names[i], jfma_sources[i]] - precipitation_data_subset[precipitation_data_subset$source == source_names[i], "jfma"]
bias_frame_jfma[bias_frame_jfma }
Convert to other coordinate system for plotting purposes
<- bias_frame %>%
bias_frame st_as_sf(coords = c("longitude","latitude"), crs = 4326) %>%
st_transform(3857) %>%
cbind(st_coordinates(.)) %>%
rename(longitude_3857 = X, latitude_3857 = Y)%>%
filter(source!="CHELSA-W5E5v1.0")
<- bias_frame_jfma %>%
bias_frame_jfma st_as_sf(coords = c("longitude","latitude"), crs = 4326) %>%
st_transform(3857) %>%
cbind(st_coordinates(.)) %>%
rename(longitude_3857 = X, latitude_3857 = Y)%>%
filter(source!="CHELSA-W5E5v1.0")
Plot the panels for each response variable.
<- basemap_ggplot(bbox) +
Bias_annual_panels theme_void() +
geom_jitter(data = bias_frame,
aes(x = longitude_3857, y = latitude_3857, colour = bias),
size = 2, width = 0.3, height = 0.3) +
geom_jitter(data = bias_frame,
aes(x = longitude_3857, y = latitude_3857),
size = 2, shape = 21, width = 0.3, height = 0.3) +
scale_color_scico(palette = "roma", direction = -1) +
facet_wrap(~source,ncol=5) +
labs(color = "Bias (mm)") +
ggtitle("Annual Precipitation Bias")
Loading basemap 'world_shaded_relief' from map service 'esri'...
Using geom_raster() with maxpixels = 386529.
<- basemap_ggplot(bbox) +
Bias_jfma_panels theme_void() +
geom_jitter(data = bias_frame_jfma,
aes(x = longitude_3857, y = latitude_3857, colour = bias),
size = 2, width = 0.3, height = 0.3) +
geom_jitter(data = bias_frame_jfma,
aes(x = longitude_3857, y = latitude_3857),
size = 2, shape = 21, width = 0.3, height = 0.3) +
scale_color_scico(palette = "roma", direction = -1) +
facet_wrap(~source,ncol = 5) +
labs(color = "Bias (mm)") +
ggtitle("January to April Precipitation Bias")
Loading basemap 'world_shaded_relief' from map service 'esri'...
Using geom_raster() with maxpixels = 386529.
<- Bias_annual_panels +
Bias_annual_panels theme(strip.text = element_text(size = 7),
plot.title = element_text(hjust = 0.5,size=14),
plot.background = element_rect(fill = "white"))
<- Bias_jfma_panels +
Bias_jfma_panels theme(strip.text = element_text(size = 7),
plot.title = element_text(hjust = 0.5,size=14),
plot.background = element_rect(fill = "white"))
#ggsave(Bias_annual_panels,path = '../plots',filename = "annual_bias_maps.tiff", units="in", width=10, height=10, dpi=900)
#ggsave(Bias_jfma_panels,path = '../plots',filename = "jfma_bias_maps.tiff", units="in", width=10, height=10, dpi=900)
Bias_annual_panels
Bias_jfma_panels
Arrange both set of panels in a grid
<-grid.arrange(Bias_annual_panels,Bias_jfma_panels,ncol = 1) c
ggsave(c,path='../plots' ,filename = "../plots/bias_figure.tiff", units="in", width=7.5, height=6, dpi=900)
c
TableGrob (2 x 1) "arrange": 2 grobs
z cells name grob
1 1 (1-1,1-1) arrange gtable[layout]
2 2 (2-2,1-1) arrange gtable[layout]
6.5 Figure 4. Correlation plots
<- read.csv(file="../tables/climatologies.csv")
precipitation_data_subset <- c("CHELSA.1.2", "CHELSA.2.1", "CHELSA.EarthEnv", "CHPclim", "WorldClim.2.1", "CHIRPS.2.0", "CHELSA.W5E5v1.0", "TerraClimate", "PBCOR.CHELSA.1.2", "PBCOR.CHPclim", "PBCOR.WorldClim.2.1")
data_sources <- c("CHELSA 1.2", "CHELSA 2.1", "CHELSA EarthEnv", "CHPclim", "WorldClim 2.1", "CHIRPS 2.0", "CHELSA-W5E5v1.0", "TerraClimate", "PBCOR CHELSA 1.2", "PBCOR CHPclim", "PBCOR WorldClim 2.1")
source_names
<- precipitation_data_subset %>%
corr_frame select("site","longitude","latitude","annual","source") %>%
mutate(value=NA)
<- precipitation_data_subset %>%
corr_frame_jfmaselect("site","longitude","latitude","jfma","source") %>%
mutate(value=NA)
for(i in seq_along(data_sources)) {
$source == source_names[i], "value"] <- precipitation_data_subset[precipitation_data_subset$source == source_names[i], data_sources[i]]
corr_frame[corr_frame
}<- paste0(data_sources, "_jfma")
jfma_sources for(i in seq_along(jfma_sources)) {
$source == source_names[i], "value"] <- precipitation_data_subset[precipitation_data_subset$source == source_names[i], jfma_sources[i]]
corr_frame_jfma[corr_frame_jfma }
Plot using ggplot
<-ggplot(corr_frame[corr_frame$source!="CHELSA-W5E5v1.0",],aes(x = value,y=annual))+
annual_corrgeom_smooth(method='lm',se=FALSE)+
geom_point(col=rgb(0,0,0,alpha=0.5))+facet_wrap(vars(source),ncol=5)+
theme_classic()+
expand_limits(x=c(1000,4500),y=c(1000,4500))+
geom_abline(slope = 1, intercept = 0, lty= 2)+
coord_equal()+
theme(
strip.text = element_text(size = 8)
)
<- data.frame(
dat_text source = unique(corr_frame[corr_frame$source!="CHELSA-W5E5v1.0","source"])
)
for(i in 1:10){
<- lm(value ~ annual, data = corr_frame[corr_frame$source == dat_text$source[i],])
model $eq[i] <- paste0("y = ", round(coef(model)[1], 2), " + ", round(coef(model)[2], 2), "x")
dat_text
}<-annual_corr + geom_text(
annual_corrdata = dat_text,
mapping = aes(x = 1800, y = 1300, label = eq),
hjust = 0,
vjust = 1,
size = 2
+labs(title = "Total Annual Correlation Plots")+
)xlab("Gridded products annual precipitation (mm)") +
ylab("In-situ annual precipitation (mm)")
ggsave(annual_corr,path='../plots' ,filename = "../plots/correlation_annual.tiff", units="in", width=7.25, height=6.25, dpi=900)
`geom_smooth()` using formula = 'y ~ x'
annual_corr
`geom_smooth()` using formula = 'y ~ x'
Plot January to April correlation plots
<-ggplot(corr_frame_jfma[corr_frame_jfma$source!="CHELSA-W5E5v1.0",],aes(x = value,y=jfma))+
jfma_corrgeom_smooth(method='lm',se=FALSE)+
geom_point(col=rgb(0,0,0,alpha=0.5))+facet_wrap(vars(source),ncol=5)+
expand_limits(x=c(0,800),y=c(0,800))+
theme_classic()+
geom_abline(slope = 1, intercept = 0, lty= 2)+
coord_equal()+
theme(
strip.text = element_text(size = 8)
)
<- data.frame(
dat_text source = unique(corr_frame_jfma[corr_frame_jfma$source!="CHELSA-W5E5v1.0","source"])
)
for(i in 1:10){
<- lm(value ~ jfma, data = corr_frame_jfma[corr_frame_jfma$source == dat_text$source[i],])
model $eq[i] <- paste0("y = ", round(coef(model)[1], 2), " + ", round(coef(model)[2], 2), "x")
dat_text
}<-jfma_corr + geom_text(
jfma_corrdata = dat_text,
mapping = aes(x = 350, y = 50, label = eq),
hjust = 0,
vjust = 1,
size = 2
+labs(title = "January-April Correlation Plots")+
)xlab("Gridded products Jan-Apr precipitation (mm)") +
ylab("In-situ Jan-Apr precipitation (mm)")
ggsave(jfma_corr,path='../plots' ,filename = "../plots/correlation_jfma.tiff", units="in", width=8, height=7, dpi=900)
`geom_smooth()` using formula = 'y ~ x'
jfma_corr
`geom_smooth()` using formula = 'y ~ x'
Arrange both correlation panels in a grid
<-grid.arrange(annual_corr,jfma_corr,ncol = 1) d
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
ggsave(d,path='../plots' ,filename = "../plots/correlation.tiff", units="in", width=7.5, height=7.5, dpi=900)
6.6 Figure.5 Seasonality plots
Mutating the data is necessary to consider a second line representing a second set of ground data in BCI.
<-read.csv("../tables/acp38_ag.csv")
acp38_ag<- acp38_ag %>%
acp38_ag_long ::pivot_longer(cols = c(CHELSA1.2:PBCOR_WorldClim2.1), names_to = "colname", values_to = "value")%>%
tidyrmutate(colname = case_when(
== "CHELSA1.2" ~ "CHELSA 1.2",
colname == "CHELSA2.1" ~ "CHELSA 2.1",
colname == "CHELSA_EarthEnv" ~ "CHELSA EarthEnv",
colname == "CHPclim" ~ "CHPclim",
colname == "WorldClim2.1" ~ "WorldClim 2.1",
colname == "CHIRPS2.0" ~ "CHIRPS 2.0",
colname == "CHELSAW5E5v1.0" ~ "CHELSA-W5E5v1.0",
colname == "TerraClimate" ~ "TerraClimate",
colname == "PBCOR_CHELSA1.2" ~ "PBCOR CHELSA 1.2",
colname == "PBCOR_CHPclim" ~ "PBCOR CHPclim",
colname == "PBCOR_WorldClim2.1" ~ "PBCOR WorldClim 2.1",
colname %>%
))filter((dataset_name == colname) | (dataset_name == "Ground" & colname == "CHELSA 1.2"))%>%
mutate(value = ifelse(dataset_name == "Ground", mon_mean, value)) %>%
mutate(dataset_name = ifelse(site == "BCICLEAR" & dataset_name == "Ground", "Ground manual", dataset_name))%>%
filter(!(site == "BCICLEAR" & dataset_name != "Ground manual"))%>%
mutate(site=ifelse(site=="BCICLEAR","BCI",site))
$dataset_name <- factor(acp38_ag_long$dataset_name, levels = c("CHELSA 1.2","CHELSA 2.1","CHELSA EarthEnv", "CHPclim",
acp38_ag_long"WorldClim 2.1","CHIRPS 2.0","CHELSA-W5E5v1.0","TerraClimate",
"PBCOR CHELSA 1.2","PBCOR CHPclim","PBCOR WorldClim 2.1","Ground",
"Ground manual"))
$site <- factor(acp38_ag_long$site, # Reordering group factor levels
acp38_ag_longlevels = c("SANMIGUEL", "AGUACLARA", "PELUCA", "BCI","GUACHA","CASCADAS","GAMBOA","PEDROMIGUEL"))
Plot using ggplot2 and customs colors
<-ggplot(acp38_ag_long,aes(x=Month,y=value))+
seasonality_plotgeom_line(aes(color=dataset_name),linewidth=0.2)+
geom_point(aes(color=dataset_name),size=0.2)+
facet_wrap(vars(site),ncol = 4)+
scale_color_manual(labels = c("CHELSA 1.2","CHELSA 2.1","CHELSA EarthEnv", "CHPclim",
"WorldClim 2.1","CHIRPS 2.0","CHELSA-W5E5v1.0","TerraClimate",
"PBCOR CHELSA 1.2","PBCOR CHPclim","PBCOR WorldClim 2.1","Ground",
"Ground manual"),values=c('#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#a6cee3','brown',"black","purple")) +
scale_x_continuous(breaks=c(1,2,3,4,5,6,7,8,9,10,11,12),
labels=c("Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))+theme_classic()+
theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust=1,size=6),strip.text = element_text(size = 5),)+
ylab("Precipitation (mm)")+
labs(color = "Gridded dataset")+
labs(title="Seasonal Precipitation Patterns")
ggsave(seasonality_plot,path='../plots' ,filename = "../plots/seasonality.tiff", units="in", width=7.25, height=5, dpi=900)
seasonality_plot
6.7 Figure 6. Interannual variability
Mutating the dataset to consider the existence of a second line representing another set of ground data coming from a secondary rain gauge in BCI.
<-read.csv("../tables/acp38_year.csv")
acp38_year
<- acp38_year%>%
acp38_year_long::pivot_longer(cols=c(annual_precip,CHELSA1.2:TerraClimate), names_to = "colname", values_to = "value")%>%
tidyrmutate(colname = case_when(
== "CHELSA1.2" ~ "CHELSA 1.2",
colname == "CHELSA2.1" ~ "CHELSA 2.1",
colname == "CHELSA_EarthEnv" ~ "CHELSA EarthEnv",
colname == "CHIRPS2.0" ~ "CHIRPS 2.0",
colname == "CHELSAW5E5v1.0" ~ "CHELSA-W5E5v1.0",
colname == "TerraClimate" ~ "TerraClimate",
colname == "annual_precip" ~ "Ground"
colname %>%
))filter((dataset_name == colname) | (dataset_name == "CHELSA 1.2" & colname == "Ground"))%>%
mutate(dataset_name= ifelse(dataset_name == "CHELSA 1.2" & colname == "Ground"& site=="BCICLEAR","Ground manual",dataset_name))%>%
filter(!(site == "BCICLEAR" & dataset_name != "Ground manual"))%>%
mutate(site=ifelse(site=="BCICLEAR","BCI",site))%>%
mutate(dataset_name=ifelse(dataset_name=="CHELSA 1.2"&colname=="Ground","Ground",dataset_name))
$site <- factor(acp38_year_long$site, # Reordering group factor levels
acp38_year_longlevels = c("SANMIGUEL", "AGUACLARA", "PELUCA", "BCI","GUACHA","CASCADAS","GAMBOA","PEDROMIGUEL"))
$dataset_name<-factor(acp38_year_long$dataset_name,
acp38_year_longlevels=c("CHELSA 1.2","CHELSA 2.1", "CHELSA EarthEnv","CHIRPS 2.0","CHELSA-W5E5v1.0","TerraClimate","Ground","Ground manual"))
<- acp38_year_long %>%
acp38_year_long filter(!is.na(dataset_name))
Plot using ggplot2
<-ggplot(acp38_year_long,aes(x=Year,y=value))+
interannual_plotgeom_line(aes(color=dataset_name),linewidth=0.2)+
geom_point(aes(color=dataset_name),size=0.2)+
expand_limits(x=c(1970,2016),y=c(900,5900))+
facet_wrap(vars(site),ncol=4)+
theme_classic()+
xlab("Year")+
ylab("Annual Total precipitation")+
labs(color="Gridded dataset")+
labs(title="Interannual Variability",size=15)+
theme(strip.text = element_text(size = 5),
axis.text.x = element_text(size=5),
axis.text.y = element_text(size=5),
axis.title.x = element_text(size = 6),
axis.title.y = element_text(size = 6),
legend.text = element_text(size = 5),
legend.title= element_text(size=6))+
scale_color_manual(values=c('#a65628','#e7298a','#e31a1c', '#ff7f00','#984ea3','#4daf4a','black','purple'))
<- acp38_year_long %>% filter(dataset_name=="Ground")%>%
yeargroup_by(site) %>% summarise(annual_mean=mean(value,na.rm=TRUE))
<-acp38_year_long %>% filter(dataset_name=="Ground manual")%>%
year2group_by(site)%>% summarise(annual_mean=mean(value,na.rm=TRUE))
<-interannual_plot + geom_text(
interannual_plotdata = year,
mapping = aes(x = -Inf, y = 5350, label = paste0("Mean annual precipitation=",round(annual_mean,0))),
hjust = -0.1,
vjust = -1,
size=1
+
)geom_text(
data = year2,
mapping = aes(x = 1990, y = 5350, label = paste0("(electronic), ",round(annual_mean,0),"(manual)")),
hjust = -0.1,
vjust = -1,
size=1)
##annual
ggsave(interannual_plot,path='../plots' ,filename = "../plots/interannual.tiff", units="in", width=7.25, height=3, dpi=900)
Warning: Removed 77 rows containing missing values (`geom_line()`).
Warning: Removed 618 rows containing missing values (`geom_point()`).
interannual_plot
Warning: Removed 77 rows containing missing values (`geom_line()`).
Removed 618 rows containing missing values (`geom_point()`).