library(rgdal)
library(raster)
library(sf)
library(ggplot2)
library(sp)
library(stringr)
library(ggpubr)
library(colorspace)

# load clim covar prep
setwd("D:/Helsinki/Moth_project/ResultsAug23/RDATA/1.EnvironmentalInfo")
load('ClimSmallFIN_1408.RDATA')
head(mapData2)
dim(mapData2)
#head(Fin_all_small)
mapData2 = mapData2[mapData2$Year<2021,]

# load Hab covar prep

load('HabSmallFIN1408.RDATA')
head(mapData2hab)
dim(mapData2hab)

######## Group covar of interest only

MapdataALL = cbind(mapData2[, c(1, 16:17, 2, 10, 15, 11)],
                   mapData2hab[, 1:8])

head(MapdataALL)
summary(MapdataALL)
class(MapdataALL$Year)

MapdataALL$Year = as.factor(MapdataALL$Year)


## Site ones
load('ClimSite080623.RDATA')
head(ClimSite)

ClimSite2 = ClimSite[ClimSite$Year > 1997, c(1:4, 7, 15, 17:18)]
head(ClimSite2)
ClimSite2 = ClimSite2[ClimSite2$Year < 2021,]
colnames(ClimSite2)[5:6] = c('cdd5w', 'gdd5s')

load('HabSites1108.RDATA')
head(Mclc)
head(Metricsclc)
Mclc2 = Mclc[order(Mclc$SiteID),]
Metricsclc2 = Metricsclc[order(Metricsclc$SiteID),]


EnvSite = cbind(ClimSite2, Mclc2[, 3:6], Metricsclc2[, 3:5])
head(EnvSite)
EnvSite.list = split(EnvSite, f = EnvSite$Year)     

Site.list.covar = lapply(EnvSite.list, "[" , c('precip', 'gdd5s', 'cdd5w', 'snow', 'clumpy', 'ent', 
                                               'ennMN', 'BLforest', 'Cforest', 'Mforest', 'SNHherb'))
dim(Site.list.covar[[1]])
head(Site.list.covar[[1]])

Mean_env_site = Reduce('+', Site.list.covar)/length(Site.list.covar)
dim(Mean_env_site)
summary(Mean_env_site)

Site_coord = ClimSite2[, 2:3]

Mean_env_site$X = EnvSite.list[[1]]$XEUREF
Mean_env_site$Y = EnvSite.list[[1]]$YEUREF


########  Plot and change trends
# 1 # Mean across years
MapdataALL.list = split(MapdataALL, f = MapdataALL$Year)     

length(MapdataALL.list)
dim(MapdataALL.list[[1]])
head(MapdataALL.list[[1]])

DatAll.list.covar = lapply(MapdataALL.list, "[" , c('precip', 'gdd5s', 'cdd5w', 'snow', 'clumpy', 'ent', 'ennMN', 'BLforest', 'Cforest', 'Mforest', 'SNHherb'))
dim(DatAll.list.covar[[1]])
head(DatAll.list.covar[[1]])

Mean_env = Reduce('+', DatAll.list.covar)/length(DatAll.list.covar)
dim(Mean_env)
summary(Mean_env)

Fin_coord = MapdataALL.list[[1]][, 2:3]

Mean_env$X = Fin_coord$X
Mean_env$Y = Fin_coord$Y

###  All change
head(Mean_env)
head(Mean_env_site)

Mean_env$spatial = 'FINLAND'
Mean_env_site$spatial = 'SITE'

ALLchange = rbind(Mean_env, Mean_env_site)



###  -----------------------------------------------------------------------------------------------
# 2 # trend

## Finland
head(MapdataALL)
head(EnvSite.list)

MapdataALL$SiteID = paste(MapdataALL$X, '', MapdataALL$Y)
SiteFIn_un = unique(MapdataALL$SiteID)

MapdataALL = MapdataALL[, c(1:7, 9:16, 8)]
head(MapdataALL)

Trend_site_FIN = Resid_site_FIN = Resid2_site_FIN = coef_trend_FIN = matrix(NA, nrow= 12340, ncol=11)
for (i in 1:length(SiteFIn_un)) {
  MapdataALL_i = MapdataALL[MapdataALL$SiteID == SiteFIn_un[i],]
  
  for (j in colnames(MapdataALL_i)[4:14]) {
    index_j = match(j, colnames(MapdataALL_i)[4:14])
    Fit_site_FIN = lm(reformulate('YearStart', j), data = MapdataALL_i)
    
    Resid_site_FIN[i, index_j] = var(resid(Fit_site_FIN))
    Resid2_site_FIN[i, index_j] = var(rstandard(Fit_site_FIN), na.rm=TRUE)
    coef_trend_FIN[i, index_j] = Fit_site_FIN$coefficients[2]
  }
}

colnames(Resid_site_FIN) = colnames(MapdataALL_i)[4:14]
Resid_site_FIN = as.data.frame(Resid_site_FIN)
Resid_site_FIN$X = Fin_coord$X
Resid_site_FIN$Y = Fin_coord$Y
Resid_site_FIN$spatial = 'FINLAND'

colnames(Resid2_site_FIN) = colnames(MapdataALL_i)[4:14]
Resid2_site_FIN = as.data.frame(Resid2_site_FIN)
Resid2_site_FIN$X = Fin_coord$X
Resid2_site_FIN$Y = Fin_coord$Y
Resid2_site_FIN$spatial = 'FINLAND'

colnames(coef_trend_FIN) = colnames(MapdataALL_i)[4:14]
coef_trend_FIN = as.data.frame(coef_trend_FIN)
coef_trend_FIN$X = Fin_coord$X
coef_trend_FIN$Y = Fin_coord$Y
coef_trend_FIN$spatial = 'FINLAND'


## Sites
head(MapdataALL)
head(EnvSite)

Site_un = unique(EnvSite$Site)

EnvSite = EnvSite[, c(1:4, 7,6,5, 8, 13:15, 10:12, 9)]

Trend_site_SITE = Resid_site_SITE = Resid2_site_SITE = coef_trend_SITE = matrix(NA, nrow= 109, ncol=11)
for (i in 1:length(Site_un)) {
  EnvSite_i = EnvSite[EnvSite$Site == Site_un[i],]
  
  for (j in colnames(EnvSite_i)[5:15]) {
    index_j = match(j, colnames(EnvSite_i)[5:15])
    Fit_site_SITE = lm(reformulate('Year', j), data = EnvSite_i)
    
    Resid_site_SITE[i, index_j] = var(resid(Fit_site_SITE))
    Resid2_site_SITE[i, index_j] = var(rstandard(Fit_site_SITE), na.rm=TRUE)
    coef_trend_SITE[i, index_j] = Fit_site_SITE$coefficients[2]
  }
}

colnames(Resid_site_SITE) = colnames(EnvSite_i)[5:15]
Resid_site_SITE = as.data.frame(Resid_site_SITE)
Resid_site_SITE$X = EnvSite.list[[1]]$XEUREF
Resid_site_SITE$Y = EnvSite.list[[1]]$YEUREF
Resid_site_SITE$spatial = 'SITE'

colnames(Resid2_site_SITE) = colnames(EnvSite_i)[5:15]
Resid2_site_SITE = as.data.frame(Resid2_site_SITE)
Resid2_site_SITE$X = EnvSite.list[[1]]$XEUREF
Resid2_site_SITE$Y = EnvSite.list[[1]]$YEUREF
Resid2_site_SITE$spatial = 'SITE'

colnames(coef_trend_SITE) = colnames(EnvSite_i)[5:15]
coef_trend_SITE = as.data.frame(coef_trend_SITE)
coef_trend_SITE$X = EnvSite.list[[1]]$XEUREF
coef_trend_SITE$Y = EnvSite.list[[1]]$YEUREF
coef_trend_SITE$spatial = 'SITE'

## plot 2#
head(coef_trend_SITE)
head(coef_trend_FIN)

ALL_coeftrend = rbind(coef_trend_SITE, coef_trend_FIN)




##..............................................................................................................
# 3 # unexplained by trend

head(Resid2_site_FIN)
head(Resid2_site_SITE)

ALL_residvar = rbind(Resid2_site_SITE, Resid2_site_FIN)



###
##.....................................................................................................

MapAVchangeprecip <- ggplot(data = Mean_env, aes(x=X, y=Y, color=precip)) + geom_point(size=0.6) + 
  ggtitle("a) Precipitation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


precipM.gg = ggplot(ALLchange, aes(x=spatial, y=precip, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


#ggarrange(MapAVchangeprecip, precip.gg, ncol=2)


MapAVchangecdd5w <- ggplot(data = Mean_env, aes(x=X, y=Y, color=cdd5w)) + geom_point(size=0.6) + 
  ggtitle("b) Winter chilling degree days") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

cdd5wM.gg = ggplot(ALLchange, aes(x=spatial, y=cdd5w, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


MapAVchangegdd5s <- ggplot(data = Mean_env, aes(x=X, y=Y, color=gdd5s)) + geom_point(size=0.6) + 
  ggtitle("c) Summer growing degree days") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

gdd5sM.gg = ggplot(ALLchange, aes(x=spatial, y=gdd5s, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


MapAVchangesnow <- ggplot(data = Mean_env, aes(x=X, y=Y, color=snow)) + geom_point(size=0.6) + 
  ggtitle("d) Snow depth") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

snowM.gg = ggplot(ALLchange, aes(x=spatial, y=snow, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())



## Hab

summary(Mean_env)

MapAVchangeBL <- ggplot(data = Mean_env, aes(x=X, y=Y, color=BLforest)) + geom_point(size=0.6) + 
  ggtitle("a) Broad-leaved forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick') +
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

BLM.gg = ggplot(ALLchange, aes(x=spatial, y=BLforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


MapAVchangeC <- ggplot(data = Mean_env, aes(x=X, y=Y, color=Cforest)) + geom_point(size=0.6) + 
  ggtitle("b) Coniferous forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick') +
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

CM.gg = ggplot(ALLchange, aes(x=spatial, y=Cforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


MapAVchangeM <- ggplot(data = Mean_env, aes(x=X, y=Y, color=Mforest)) + geom_point(size=0.6) + 
  ggtitle("c) Mixed forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick') +
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

MM.gg = ggplot(ALLchange, aes(x=spatial, y=Mforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


MapAVchangeSNH <- ggplot(data = Mean_env, aes(x=X, y=Y, color=SNHherb)) + geom_point(size=0.6) + 
  ggtitle("d) Semi natural and \n herbaceous vegetation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick') +
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

SNHM.gg = ggplot(ALLchange, aes(x=spatial, y=SNHherb, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


MapAVchangeclumpy <- ggplot(data = Mean_env, aes(x=X, y=Y, color=clumpy)) + geom_point(size=0.6) + 
  ggtitle("e) Forest fragmentation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick') +
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

clumpyM.gg = ggplot(ALLchange, aes(x=spatial, y=clumpy, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) + 
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


MapAVchangeent <- ggplot(data = Mean_env, aes(x=X, y=Y, color=ent)) + geom_point(size=0.6) + 
  ggtitle("f) Landscape diversity") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick') +
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

entM.gg = ggplot(ALLchange, aes(x=spatial, y=ent, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) + 
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())





TRENDchangeprecip <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=precip)) + geom_point(size=0.6) + 
  ggtitle("e) Precipitation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)),
                        limits=c(-8,8)) + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 



precipT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=precip, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-8, 8)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


#ggarrange(TRENDchangeprecip, precip.gg, ncol=2)


TRENDchangecdd5w <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=cdd5w)) + geom_point(size=0.6) + 
  ggtitle("f) Winter chilling degree days") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)),
                        limits=c(-30,30)) + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


cdd5wT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=cdd5w, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-30, 30)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


TRENDchangegdd5s <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=gdd5s)) + geom_point(size=0.6) + 
  ggtitle("g) Summer growing degree days") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)),
                        limits=c(-15,15)) + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


gdd5sT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=gdd5s, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-15, 15)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


TRENDchangesnow <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=snow)) + geom_point(size=0.6) + 
  ggtitle("h) Snow Depth") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)),
                        limits=c(-1,1)) + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


snowT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=snow, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-1, 1)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())



## Hab

TRENDchangeBL <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=BLforest)) + geom_point(size=0.6) + 
  ggtitle("g) Broad-leaved forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)), 
                        limits=c(-0.06, 0.06)) + theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick') +
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 

BLT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=BLforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-0.06, 0.06)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick"))+ 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


TRENDchangeC <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=Cforest)) + geom_point(size=0.6) + 
  ggtitle("h) Coniferous forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)), 
                        limits=c(-0.06, 0.06)) + theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


CT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=Cforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-0.06, 0.06)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


TRENDchangeM <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=Mforest)) + geom_point(size=0.6) + 
  ggtitle("i) Mixed forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)), 
                        limits=c(-0.06, 0.06)) + theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


MT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=Mforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-0.06, 0.06)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


TRENDchangeSNH <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=SNHherb)) + geom_point(size=0.6) + 
  ggtitle("j) Semi natural and \n herbceous vegetation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)), 
                        limits=c(-0.06, 0.06)) + theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = coef_trend_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


SNHT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=SNHherb, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-0.06, 0.06)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


TRENDchangeclumpy <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=clumpy)) + geom_point(size=0.6) + 
  ggtitle("k) Forest fragmentation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)), 
                        limits=c(-0.11, 0.11)) + theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


clumpyT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=clumpy, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-0.11, 0.11)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) + 
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


TRENDchangeent <- ggplot(data = coef_trend_FIN, aes(x=X, y=Y, color=ent)) + geom_point(size=0.6) + 
  ggtitle("l) Landscape diversity") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_gradientn(colours = colorspace::diverging_hcl(11, h = c(180, 50), c = 80, l = c(20, 95), power = c(0.7, 1.3)), 
                        limits=c(-0.1, 0.1)) + theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


entT.gg = ggplot(ALL_coeftrend, aes(x=spatial, y=ent, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3), limits = c(-0.1, 0.1)) + 
  theme_minimal() + xlab('') + ylab('') +
  geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) + 
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())




VARUhangeprecip <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=precip)) + geom_point(size=0.6) + 
  ggtitle("i) Precipitation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 



precip.gg = ggplot(ALL_residvar, aes(x=spatial, y=precip, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


#ggarrange(VARUhangeprecip, precip.gg, ncol=2)


VARUhangecdd5w <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=cdd5w)) + geom_point(size=0.6) + 
  ggtitle("j) Winter chilling degree days") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


cdd5w.gg = ggplot(ALL_residvar, aes(x=spatial, y=cdd5w, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


VARUhangegdd5s <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=gdd5s)) + geom_point(size=0.6) + 
  ggtitle("k) Summer growing degree days") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


gdd5s.gg = ggplot(ALL_residvar, aes(x=spatial, y=gdd5s, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


VARUhangesnow <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=snow)) + geom_point(size=0.6) + 
  ggtitle("l) Snow depth") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "BluGrn") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


snow.gg = ggplot(ALL_residvar, aes(x=spatial, y=snow, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


## Hab

VARUhangeBL <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=BLforest)) + geom_point(size=0.6) + 
  ggtitle("m) Broad-leaved forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


BL.gg = ggplot(ALL_residvar, aes(x=spatial, y=BLforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick"))+ 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


VARUhangeC <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=Cforest)) + geom_point(size=0.6) + 
  ggtitle("n) Coniferous forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


C.gg = ggplot(ALL_residvar, aes(x=spatial, y=Cforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick"))+ 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


VARUhangeM <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=Mforest)) + geom_point(size=0.6) + 
  ggtitle("o) Mixed forest") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


M.gg = ggplot(ALL_residvar, aes(x=spatial, y=Mforest, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


VARUhangeSNH <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=SNHherb)) + geom_point(size=0.6) + 
  ggtitle("p) Semi natural and \n herbaceous vegetation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Resid2_site_SITE, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7))


SNH.gg = ggplot(ALL_residvar, aes(x=spatial, y=SNHherb, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) +
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


VARUhangeclumpy <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=clumpy)) + geom_point(size=0.6) + 
  ggtitle("q) Forest fragmentation") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7))


clumpy.gg = ggplot(ALL_residvar, aes(x=spatial, y=clumpy, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) + 
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())


VARUhangeent <- ggplot(data = Resid2_site_FIN, aes(x=X, y=Y, color=ent)) + geom_point(size=0.6) + 
  ggtitle("r) Landscape diversity") +
  xlab("East coordinate (km)") + ylab("North coordinate (km)") +
  scale_color_continuous_sequential(palette = "Purple-Blue") + 
  theme_classic()  +
  theme(legend.position = "left",plot.title = element_text(size=9), axis.text=element_text(size=6),
        axis.title = element_text(size = 9), legend.text = element_text(size = 8),
        legend.spacing.x = unit(0.5, 'cm'), legend.spacing.y = unit(0.3, 'cm'),
        legend.title = element_text(size = 0), axis.text.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks = element_blank(), 
        strip.text = element_text(size=8), strip.background = element_blank()) +
  geom_point(data = Mean_env_site, aes(x=X, y=Y), col='firebrick')+
  guides(color = guide_colorbar(title = "",
                                #label.position = "bottom",
                                #title.position = "left", title.vjust = 1,
                                # draw border around the legend
                                frame.colour = "black",
                                barwidth = 0.5,
                                barheight = 7)) 


ent.gg = ggplot(ALL_residvar, aes(x=spatial, y=ent, fill= spatial)) +
  geom_violin(trim=FALSE, draw_quantiles = c(0.25, 0.5, 0.75)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) + 
  theme_minimal() + xlab('') + ylab('') +
  #geom_hline(yintercept = 0, linetype="dashed", colour='gray60', size= 1) + 
  coord_flip() + scale_fill_manual(values=c("beige", "firebrick")) + 
  theme(legend.position = "none", axis.text=element_text(size=7),          strip.text = element_text(size=8), strip.background = element_blank())



## clim
Clim_arragne = ggarrange(MapAVchangeprecip, MapAVchangecdd5w, MapAVchangegdd5s, MapAVchangesnow, 
          precipM.gg, cdd5wM.gg, gdd5sM.gg, snowM.gg,
          TRENDchangeprecip, TRENDchangecdd5w, TRENDchangegdd5s, TRENDchangesnow, 
          precipT.gg, cdd5wT.gg, gdd5sT.gg, snowT.gg,
          VARUhangeprecip, VARUhangecdd5w, VARUhangegdd5s, VARUhangesnow, 
          precip.gg, cdd5w.gg, gdd5s.gg, snow.gg,
          ncol=4, nrow=6, heights = c(4,2,4,2,4,2))

ClimM = ggarrange(MapAVchangeprecip, MapAVchangecdd5w, MapAVchangegdd5s, MapAVchangesnow, 
                  precipM.gg, cdd5wM.gg, gdd5sM.gg, snowM.gg,
                  ncol=4, nrow=2, heights = c(4,2))

ClimT = ggarrange(TRENDchangeprecip, TRENDchangecdd5w, TRENDchangegdd5s, TRENDchangesnow, 
                  precipT.gg, cdd5wT.gg, gdd5sT.gg, snowT.gg,
                  ncol=4, nrow=2, heights = c(4,2))

ClimU = ggarrange(VARUhangeprecip, VARUhangecdd5w, VARUhangegdd5s, VARUhangesnow, 
                  precip.gg, cdd5w.gg, gdd5s.gg, snow.gg,
                  ncol=4, nrow=2, heights = c(4,2))

ClimM <- annotate_figure(ClimM, left = text_grob("Average", face = "bold", rot=90, size=12))
ClimT <- annotate_figure(ClimT, left = text_grob("Trend", face = "bold", rot=90, size=12))
ClimU <- annotate_figure(ClimU, left = text_grob("Residuals variance", face = "bold", rot=90, size=12))

p <- ggarrange(ClimM, ClimT, ClimU, nrow=3) 
annotate_figure(p, top = text_grob("Climatic conditions", face = "bold", size = 16))

## Hab

ggarrange(MapAVchangeBL, MapAVchangeC, MapAVchangeM, MapAVchangeSNH, MapAVchangeclumpy, MapAVchangeent,
          BLM.gg, CM.gg, MM.gg, SNHM.gg, clumpyM.gg, entM.gg,
          TRENDchangeBL, TRENDchangeC, TRENDchangeM, TRENDchangeSNH, TRENDchangeclumpy, TRENDchangeent,
          BLT.gg, CT.gg, MT.gg, SNHT.gg, clumpyT.gg, entT.gg,
          VARUhangeBL, VARUhangeC, VARUhangeM, VARUhangeSNH, VARUhangeclumpy, VARUhangeent,
          BL.gg, C.gg, M.gg, SNH.gg, clumpy.gg, ent.gg,
          ncol=6, nrow=6, heights = c(4,2,4,2,4,2))

HabM = ggarrange(MapAVchangeBL, MapAVchangeC, MapAVchangeM, MapAVchangeSNH, MapAVchangeclumpy, MapAVchangeent,
                 BLM.gg, CM.gg, MM.gg, SNHM.gg, clumpyM.gg, entM.gg,
                  ncol=6, nrow=2, heights = c(4,2))

HabT = ggarrange(TRENDchangeBL, TRENDchangeC, TRENDchangeM, TRENDchangeSNH, TRENDchangeclumpy, TRENDchangeent,
                 BLT.gg, CT.gg, MT.gg, SNHT.gg, clumpyT.gg, entT.gg,
                  ncol=6, nrow=2, heights = c(4,2))

HabU = ggarrange(VARUhangeBL, VARUhangeC, VARUhangeM, VARUhangeSNH, VARUhangeclumpy, VARUhangeent,
                 BL.gg, C.gg, M.gg, SNH.gg, clumpy.gg, ent.gg,
                  ncol=6, nrow=2, heights = c(4,2))

HabM <- annotate_figure(HabM, left = text_grob("Average", face = "bold", rot=90, size=12))
HabT <- annotate_figure(HabT, left = text_grob("Trend", face = "bold", rot=90, size=12))
HabU <- annotate_figure(HabU, left = text_grob("Residuals variance", face = "bold", rot=90, size=12))

p2 <- ggarrange(HabM, HabT, HabU, nrow=3) 
annotate_figure(p2, top = text_grob("Habitat conditions", face = "bold", size = 16))
