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
<- st_bbox(c(xmin = -80.2, xmax = -79.4, ymin = 8.8, ymax = 9.5), crs = st_crs(4326))
bbox <- st_as_sfc(bbox) %>% st_transform(3857)
bbox_sf <- basemap_ggplot(bbox_sf, map_service = "esri", map_type = "world_shaded_relief", map_res= 5) ##max i can go is 5 basemap
Loading basemap 'world_shaded_relief' from map service 'esri'...
Using geom_raster() with maxpixels = 4811450.
<- seq(-80.2, -79.4, by = 0.2)
x_labels_4326 <- seq(8.8, 9.5, by = 0.2)
y_labels_4326
<- expand.grid(lon = x_labels_4326, lat = y_labels_4326) %>%
grid_lines st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
st_transform(3857)
<- st_coordinates(grid_lines)
coords_3857 <- unique(coords_3857[ , "X"])
x_breaks_3857 <- unique(coords_3857[ , "Y"])
y_breaks_3857
<- c(
cb_palette "ACP" = "red",
"STRI" = "blue"
)
<- basemap +
p geom_point(data = site_df, aes(x = X, y = Y, color = source), size = 2) + scale_color_manual(values = cb_palette) +
scale_x_continuous(breaks = x_breaks_3857, labels = x_labels_4326, expand= c(0.001,0.001)) + ## error if grid lines expanded by 0,0
scale_y_continuous(breaks = y_breaks_3857, labels = y_labels_4326, expand =c(0.001,0.001)) +
coord_equal()+
theme(
axis.title = element_blank(),
panel.grid = element_blank(),
axis.ticks.length = unit(0.3, "cm"),
axis.text = element_text(size = 10),
panel.background = element_blank()
)
<- 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_orig<- classify(!is.na(chelsa_orig[[1]]), cbind(1, 1), others = NA)
no_water_mask plot(chelsa_orig)
plot(no_water_mask)
#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_origcrs(chelsa2_orig) <- "EPSG:4326"
<- chelsa2_orig*no_water_mask
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')
chp<- chp*no_water_mask
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)
chelsaearthenvcrs(chelsaearthenv) <- "EPSG:4326"
<-chelsaearthenv*no_water_mask
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="near")
pbcor_worldclimplot(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,chirps,chp,terra,worldclim,pbcor_chelsa,pbcor_chp,pbcor_worldclim)
res_stack
<-list('layer.1'="CHELSA 1.2",
models'layer.2'="CHELSA 2.1",
'layer.3'="CHELSA EarthEnv",
'layer.4'="CHIRPS v2.0",
'layer.5'="CHPclim v.1.0",
'layer.6'="TerraClimate",
'layer.7'="WorldClim v2.1",
'layer.8'="PBCOR CHELSA 1.2",
'layer.9'="PBCOR CHPclim",
'layer.10'="PBCOR WorldClim 2.1"
)<- 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_origcrs(chelsa2_orig) <- "EPSG:4326"
<- chelsa2_orig*no_water_mask
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')
chp<- chp*no_water_mask
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)
chelsaearthenvcrs(chelsaearthenv) <- "EPSG:4326"
<-chelsaearthenv*no_water_mask
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,chirps,chp,terra,worldclim,pbcor_chelsa,pbcor_chp,pbcor_worldclim)
res_stack_jfma
<-list('layer.1'="CHELSA 1.2",
models'layer.2'="CHELSA 2.1",
'layer.3'="CHELSA EarthEnv",
'layer.4'="CHIRPS v2.0",
'layer.5'="CHPclim v.1.0",
'layer.6'="TerraClimate",
'layer.7'="WorldClim v2.1",
'layer.8'="PBCOR CHELSA 1.2",
'layer.9'="PBCOR CHPclim",
'layer.10'="PBCOR WorldClim 2.1"
)<- 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=8.25, height=6.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", "CHIRPS.2.0",
data_sources "CHPclim", "TerraClimate", "WorldClim.2.1", "CHELSA.W5E5v1.0",
"PBCOR.CHELSA.1.2", "PBCOR.CHPclim", "PBCOR.WorldClim.2.1")
<- c("CHELSA 1.2", "CHELSA 2.1", "CHELSA EarthEnv", "CHPclim", "WorldClim 2.1", "CHIRPS v2.0",
source_names "CHELSA-W5E5v1.0", "TerraClimate", "PBCOR CHELSA 1.2", "PBCOR CHPclim", "PBCOR WorldClim 2.1")
$source <- gsub("CHIRPS 2.0", "CHIRPS v2.0", precipitation_data_subset$source)
precipitation_data_subset
<- precipitation_data_subset %>%
bias_frame select(site, longitude, latitude, annual, source) %>%
mutate(bias = NA)
<- precipitation_data_subset %>%
bias_frame_jfma select(site, longitude, latitude, jfma, source) %>%
mutate(bias = NA)
for(i in seq_along(data_sources)) {
$source == source_names[i], "bias"] <-
bias_frame[bias_frame$source == source_names[i], data_sources[i]] -
precipitation_data_subset[precipitation_data_subset$source == source_names[i], "annual"]
precipitation_data_subset[precipitation_data_subset
}
<- paste0(data_sources, "_jfma")
jfma_sources for(i in seq_along(jfma_sources)) {
$source == source_names[i], "bias"] <-
bias_frame_jfma[bias_frame_jfma$source == source_names[i], jfma_sources[i]] -
precipitation_data_subset[precipitation_data_subset$source == source_names[i], "jfma"]
precipitation_data_subset[precipitation_data_subset
}
$source <- factor(bias_frame$source, levels = c("CHELSA 1.2", "CHELSA 2.1", "CHELSA EarthEnv", "CHIRPS v2.0",
bias_frame"CHPclim", "TerraClimate", "WorldClim 2.1", "CHELSA-W5E5v1.0",
"PBCOR CHELSA 1.2", "PBCOR CHPclim", "PBCOR WorldClim 2.1"))
$source <- factor(bias_frame_jfma$source, levels = c("CHELSA 1.2", "CHELSA 2.1", "CHELSA EarthEnv", "CHIRPS v2.0",
bias_frame_jfma"CHPclim", "TerraClimate", "WorldClim 2.1", "CHELSA-W5E5v1.0",
"PBCOR CHELSA 1.2", "PBCOR CHPclim", "PBCOR WorldClim 2.1"))
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.
<- max(abs(bias_frame$bias), na.rm = TRUE)
max_abs_bias <- basemap_ggplot(bbox_sf, map_service = "esri", map_type = "world_shaded_relief", map_res= 2) +
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,
midpoint = 0,
limits = c(-max_abs_bias, max_abs_bias))+
facet_wrap(~source,ncol=5) +
coord_equal()+
labs(color = "Bias (mm)") +
ggtitle("Total Annual Precipitation")+ theme(legend.title = element_text(size = 10))
Loading basemap 'world_shaded_relief' from map service 'esri'...
Using geom_raster() with maxpixels = 1202280.
<- max(abs(bias_frame_jfma$bias), na.rm = TRUE)
max_abs_bias_jfma
<- basemap_ggplot(bbox_sf, map_service = "esri", map_type = "world_shaded_relief", map_res= 2) +
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,
midpoint = 0,
limits = c(-max_abs_bias_jfma, max_abs_bias_jfma)) +
facet_wrap(~source,ncol = 5) +
coord_equal()+
labs(color = "Bias (mm)") +
ggtitle("January to April Precipitation") + theme(legend.title = element_text(size = 10))
Loading basemap 'world_shaded_relief' from map service 'esri'...
Using geom_raster() with maxpixels = 1202280.
<- 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=9, 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 v2.0",
source_names "CHELSA-W5E5v1.0", "TerraClimate", "PBCOR CHELSA 1.2", "PBCOR CHPclim", "PBCOR WorldClim 2.1")
$source <- gsub("CHIRPS 2.0", "CHIRPS v2.0", precipitation_data_subset$source)
precipitation_data_subset
<- 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
}$source <- factor(corr_frame$source, levels = c("CHELSA 1.2","CHELSA 2.1","CHELSA EarthEnv", "CHIRPS v2.0","CHPclim",
corr_frame"TerraClimate","WorldClim 2.1","CHELSA-W5E5v1.0",
"PBCOR CHELSA 1.2","PBCOR CHPclim","PBCOR WorldClim 2.1"))
$source <- factor(corr_frame_jfma$source, levels = c("CHELSA 1.2","CHELSA 2.1","CHELSA EarthEnv", "CHIRPS v2.0","CHPclim",
corr_frame_jfma"TerraClimate","WorldClim 2.1","CHELSA-W5E5v1.0",
"PBCOR CHELSA 1.2","PBCOR CHPclim","PBCOR WorldClim 2.1"))
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_abline(slope = 1, intercept = 0, lty= 2)+
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))+
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 Precipitation")+
)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=8.25, height=7.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_abline(slope = 1, intercept = 0, lty= 2)+
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()+
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 to April Precipitation")+
)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
$dataset_name <- gsub("CHIRPS 2.0", "CHIRPS v2.0", acp38_ag$dataset_name)
acp38_ag
<- acp38_ag %>%
acp38_ag_long ::pivot_longer(
tidyrcols = c(CHELSA1.2, CHELSA2.1, CHELSA_EarthEnv, CHELSAW5E5v1.0,
.0, CHPclim, TerraClimate, WorldClim2.1,
CHIRPS2.2, PBCOR_CHPclim, PBCOR_WorldClim2.1),
PBCOR_CHELSA1names_to = "colname",
values_to = "value"
%>%
) mutate(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 v2.0", # Match this AFTER pivot
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))
<- acp38_ag_long %>% filter(dataset_name=="Ground")%>%
yeargroup_by(site) %>% summarise(annual_mean=sum(value,na.rm=TRUE))
$dataset_name <- factor(acp38_ag_long$dataset_name, levels = c("CHELSA 1.2","CHELSA 2.1","CHELSA EarthEnv","CHELSA-W5E5v1.0",
acp38_ag_long"CHIRPS v2.0","CHPclim", "TerraClimate","WorldClim 2.1",
"PBCOR CHELSA 1.2","PBCOR CHPclim","PBCOR WorldClim 2.1","Ground",
"Ground manual"))
unique(acp38_ag_long$dataset_name)
[1] CHELSA 1.2 CHELSA 2.1 CHELSA EarthEnv
[4] CHPclim WorldClim 2.1 CHIRPS v2.0
[7] CHELSA-W5E5v1.0 TerraClimate PBCOR CHELSA 1.2
[10] PBCOR CHPclim PBCOR WorldClim 2.1 Ground
[13] Ground manual
13 Levels: CHELSA 1.2 CHELSA 2.1 CHELSA EarthEnv ... Ground manual
<- setNames(
site_labels paste0(
$site, " (", round(year$annual_mean, 0), " mm/yr)"
year
),$site
year
)
$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_plot geom_line(aes(color = dataset_name, linewidth = dataset_name)) +
geom_point(aes(color = dataset_name), size = 0.2) +
facet_wrap(
vars(site),
ncol = 4,
labeller = labeller(site = site_labels)
+
) scale_color_manual(
labels = c(
"CHELSA 1.2", "CHELSA 2.1", "CHELSA EarthEnv", "CHELSA-W5E5v1.0",
"CHIRPS v2.0", "CHPclim", "TerraClimate", "WorldClim 2.1",
"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_discrete_manual(
"linewidth",
values = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.5, 0.5)
+
) scale_x_continuous(
breaks = 1: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 = 1.2, hjust = 1, size = 7),
strip.text = element_text(size = 6),
axis.text.y = element_text(size = 7),
axis.title.x = element_text(size = 8),
axis.title.y = element_text(size = 8),
legend.text = element_text(size = 7),
legend.title = element_text(size = 8)
+
) ylab("Monthly Precipitation (mm)") +
labs(
color = "Gridded dataset"
+
) guides(linewidth = "none")
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$dataset_name <- gsub("CHIRPS 2.0", "CHIRPS v2.0", acp38_year$dataset_name)
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 v2.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))
<- acp38_year_long %>% filter(dataset_name=="Ground")%>%
yeargroup_by(site) %>% summarise(annual_mean=mean(value,na.rm=TRUE))
$site <- factor(acp38_year_long$site, # Reordering group factor levels
acp38_year_longlevels = c("SANMIGUEL", "AGUACLARA", "PELUCA", "BCI","GUACHA","CASCADAS","GAMBOA","PEDROMIGUEL"))
<- setNames(
site_labels paste0(
$site, " (", round(year$annual_mean, 0), " mm/yr)"
year
),$site
year
)$dataset_name<-factor(acp38_year_long$dataset_name,
acp38_year_longlevels=c("CHELSA 1.2","CHELSA 2.1", "CHELSA EarthEnv","CHELSA-W5E5v1.0","CHIRPS v2.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_plot geom_line(aes(color = dataset_name, linewidth= dataset_name))+
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, labeller = labeller(site = site_labels)) +
theme_classic() +
xlab("Year") +
ylab("Annual Precipitation (mm)") +
labs(color = "Gridded dataset") +
theme(
strip.text = element_text(size = 7),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.title.x = element_text(size = 8),
axis.title.y = element_text(size = 8),
legend.text = element_text(size = 7),
legend.title = element_text(size = 8)
+
) scale_color_manual(values = c(
"CHELSA 1.2" = "#a65628",
"CHELSA 2.1" = "#e7298a",
"CHELSA EarthEnv" = "#e31a1c",
"CHIRPS v2.0" = "#ff7f00",
"CHELSA-W5E5v1.0" = "#984ea3",
"TerraClimate" = "#4daf4a",
"Ground" = "black",
"Ground manual" = "purple"
+
)) scale_discrete_manual("linewidth",values = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.5, 0.5))+
guides(linewidth = "none")
##annual
ggsave(interannual_plot,path='../plots' ,filename = "../plots/interannual.tiff", units="in", width=8.25, height=4, dpi=900)
Warning: Removed 77 rows containing missing values or values outside the scale range
(`geom_line()`).
Warning: Removed 618 rows containing missing values or values outside the scale range
(`geom_point()`).
interannual_plot
Warning: Removed 77 rows containing missing values or values outside the scale range
(`geom_line()`).
Removed 618 rows containing missing values or values outside the scale range
(`geom_point()`).