#R code for creating ggplots of time series with smooth (GAM) and linear term
######################
#Import Libraries and set working directory
######################
library ( zoo )
library ( hydroTSM ) #you need to install these packages first before you can load them here
library ( lubridate )
library ( mgcv )
library ( ggplot2 )
library ( gridExtra )
library ( scales )
options ( scipen = 999 )
setwd ( " C:/Users/z5025317/OneDrive - UNSW/WRL_Postdoc_Manual_Backup/WRL_Postdoc/Projects/Paper#1/" )
######################
GRACE.monthly.av.df <- data.frame ( read.csv ( " ./Data/GRACE/Zunyi/CSV/With_border_pixels/MDB.monthly.TWSA.Mean.2002-2017 with border pixels.csv" ) )
GRACE.monthly.std.df <- data.frame ( read.csv ( " ./Data/GRACE/Zunyi/CSV/With_border_pixels/MDB.monthly.TWSA.Stdv.2002-2017 with border pixels.csv" ) )
######################
#Set inputs
######################
Case.Study <- " CASESTUDY2"
Estuary <- " HUNTER"
Riv.Gauge.loc <- " GRETA" #GRETA
Est.Gauge.loc <- " RAYMONDTERRACE" #"CORAKI" #"RAYMONDTERRACE" # "HEXHAMBRIDGE"
logtransformFlow <- TRUE
ggplotGAM.k <- 15
rivTempGAM.k <- 20
Version <- ' V3'
Fontsize <- 12
######################
######################
Output.Directory <- paste ( ' ./Output/CASESTUDY2_V4/' , Estuary , ' /Recent_Trends/' , sep = " " )
if ( file.exists ( Output.Directory ) ) {
print ( ' output folder already existed and was not created again' )
} else {
dir.create ( file.path ( Output.Directory ) )
print ( ' output folder did not exist and was created' )
}
Output.Directory <- paste ( ' ./Output/CASESTUDY2_V4/' , Estuary , ' /Recent_Trends/Riv_' , Riv.Gauge.loc , ' _Est_' , Est.Gauge.loc , ' _GAMk' , ggplotGAM.k , ' d/' , sep = " " )
if ( file.exists ( Output.Directory ) ) {
print ( ' output folder already existed and was not created again' )
} else {
dir.create ( file.path ( Output.Directory ) )
print ( ' output folder did not exist and was created' )
}
######################
######################
#Set input file paths
######################
pattern = paste ( ' SILO_climdata_' , Estuary , ' _Catchment*' , sep = " " )
AirT_CSV_Path <- list.files ( paste ( " ./Data/SILO/" , Case.Study , ' /' , sep = " " ) , pattern , full.names = T , recursive = T )
pattern = paste ( Estuary , ' @' , Riv.Gauge.loc , ' .*.ALL.csv' , sep = " " )
RivT_CSV_Path <- list.files ( " ./Data/River_Gauge_Data/" , pattern , full.names = T )
pattern = paste ( Estuary , ' @' , Est.Gauge.loc , ' .*.ALL.csv' , sep = " " )
EstT_CSV_Path <- list.files ( " ./Data/River_Gauge_Data/" , pattern , full.names = T )
pattern = paste ( ' sstmean_NNRP_' , Estuary , ' *' , sep = " " )
SST_CSV_Path <- list.files ( paste ( " ./Data/NARCLIM_Site_CSVs/" , Case.Study , ' /' , sep = " " ) , pattern , full.names = T , recursive = T )
######################
######################
#Analyse
######################
############tasmean
#Load a daily (no gaps) time series as a time serie baseline for other time series used here
AirT.df <- data.frame ( read.csv ( AirT_CSV_Path ) )
AirT.full.TS <- zoo ( ( AirT.df $ max_temp_Celsius + AirT.df $ max_temp_Celsius ) / 2 , order.by = as.Date ( AirT.df [ , " date" ] , format = " %Y-%m-%d" ) ) #=daily time series of rainfall for creation of clean, daily TS of ET and Q
AirT.TS <- window ( AirT.full.TS , start = as.Date ( " 1990-01-01" ) , end = as.Date ( " 2018-01-01" ) )
AirT.full.df <- data.frame ( AirT.full.TS )
AirT.df <- data.frame ( AirT.TS )
colnames ( AirT.df ) <- ' tasmean'
colnames ( AirT.full.df ) <- ' tasmean'
############
AirT.df $ Julday1 <- seq ( 1 , length ( AirT.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( tasmean ~ Julday1 , AirT.df )
AirT.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
AirT.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
############
AirT.full.df $ Julday1 <- seq ( 1 , length ( AirT.full.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( tasmean ~ Julday1 , AirT.full.df )
AirT.full.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
AirT.full.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
############tasmean
############River temp
#Load a daily (no gaps) time series as a time serie baseline for other time series used here
#Here we use the raw DPI CSV format that comes with a bunch of metadata
RivT.df <- data.frame ( read.csv ( RivT_CSV_Path ) )
char.df <- data.frame ( lapply ( RivT.df [2 , ] , as.character ) , stringsAsFactors = FALSE )
#dat <- data.frame(lapply(RivT.df[(4:nrow(RivT.df)),(2:ncol(RivT.df))], as.numeric), stringsAsFactors=FALSE)
#dat <- RivT.df.num[!is.na(as.numeric(as.character(RivT.df.num))),]
dat <- RivT.df [ ( 4 : nrow ( RivT.df ) ) , ]
colnames ( dat ) <- lapply ( RivT.df [2 , ] , as.character )
dat $ Date <- gsub ( x = dat $ Date , pattern = " 00:00:00" , replacement = " " , fixed = T )
colnames ( dat ) <- gsub ( x = colnames ( dat ) , pattern = " Water Temp(C)" , replacement = " Temp" , fixed = T )
RivT.df <- dat
rm ( dat , char.df )
RivT.full.TS <- zoo ( as.numeric ( as.character ( RivT.df $ Temp ) ) , order.by = as.Date ( RivT.df [ , " Date" ] , format = " %d/%m/%Y" ) ) #=daily time series of rainfall for creation of clean, daily TS of ET and Q
RivT.full.TS <- window ( RivT.full.TS , start = as.Date ( " 1995-07-01" ) , end = as.Date ( " 2018-01-01" ) )
RivT.full.TS <- na.approx ( RivT.full.TS )
RivT.TS <- RivT.full.TS
RivT.full.df <- data.frame ( RivT.TS ) ### This is only done because
RivT.df <- data.frame ( RivT.TS )
colnames ( RivT.df ) <- ' rivTmean'
colnames ( RivT.full.df ) <- ' rivTmean'
############
RivT.df $ Julday1 <- seq ( 1 , length ( RivT.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( rivTmean ~ Julday1 , RivT.df )
RivT.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
RivT.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
RivT.full.df $ Julday1 <- seq ( 1 , length ( RivT.full.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( rivTmean ~ Julday1 , RivT.full.df )
RivT.full.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
RivT.full.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
############River temp
#export interpolated data as csv
#csv.name <- "C:/Users/z5025317/OneDrive - UNSW/CC_Estuaries_CASESTUDY2/Data/River_Gauge_Data/HUNTER@Greta_210064_Temp_interpolated.csv"
#write.csv(RivT.df, csv.name)
############River flow
#Load a daily (no gaps) time series as a time serie baseline for other time series used here
#Here we use the raw DPI CSV format that comes with a bunch of metadata
RivQ.df <- data.frame ( read.csv ( RivT_CSV_Path ) )
char.df <- data.frame ( lapply ( RivQ.df [2 , ] , as.character ) , stringsAsFactors = FALSE )
dat <- RivQ.df [ ( 4 : nrow ( RivQ.df ) ) , ]
colnames ( dat ) <- lapply ( RivQ.df [2 , ] , as.character )
dat $ Date <- gsub ( x = dat $ Date , pattern = " 00:00:00" , replacement = " " , fixed = T )
colnames ( dat ) <- gsub ( x = colnames ( dat ) , pattern = " Discharge (ML/d)" , replacement = " Flow" , fixed = T )
RivQ.df <- dat
rm ( dat , char.df )
#RivQ.full.TS <- zoo(log10((as.numeric(as.character(RivQ.df$Flow))) +1) , order.by= as.Date(RivQ.df[,"Date"], format = "%d/%m/%Y")) #=daily time series of rainfall for creation of clean, daily TS of ET and Q
RivQ.full.TS <- zoo ( ( as.numeric ( as.character ( RivQ.df $ Flow ) ) ) / 86.4 , order.by = as.Date ( RivQ.df [ , " Date" ] , format = " %d/%m/%Y" ) ) #=daily time series of rainfall for creation of clean, daily TS of ET and Q
RivQ.TS <- window ( RivQ.full.TS , start = as.Date ( " 1990-01-01" ) , end = as.Date ( " 2018-01-01" ) )
#RivQ.full.TS <- window(RivQ.full.TS, start=as.Date("2013-06-01"), end=as.Date("2018-06-01"))
RivQ.full.df <- data.frame ( RivQ.full.TS ) ### This is only done because
RivQ.df <- data.frame ( RivQ.TS )
colnames ( RivQ.df ) <- ' RivQmean'
colnames ( RivQ.full.df ) <- ' RivQmean'
############trends
RivQ.df $ Julday1 <- seq ( 1 , length ( RivQ.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( RivQmean ~ Julday1 , RivQ.df )
RivQ.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
RivQ.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
RivQ.full.df $ Julday1 <- seq ( 1 , length ( RivQ.full.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( RivQmean ~ Julday1 , RivQ.full.df )
RivQ.full.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
RivQ.full.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
############River Flow
############ SST
#Load a daily (no gaps) time series as a time serie baseline for other time series used here
SST.df <- data.frame ( read.csv ( SST_CSV_Path ) )
SST.full.TS <- zoo ( SST.df $ NNRP_R1_1950 -273.15 , order.by = as.Date ( SST.df [ , " X" ] , format = " %Y-%m-%d" ) ) #=daily time series of rainfall for creation of clean, daily TS of ET and Q
SST.TS <- window ( SST.full.TS , start = as.Date ( " 1990-01-01" ) , end = as.Date ( " 2018-01-01" ) )
#SST.full.TS <- window(SST.full.TS, start=as.Date("2013-06-01"), end=as.Date("2018-06-01"))
SST.full.df <- data.frame ( SST.full.TS )
SST.df <- data.frame ( SST.TS )
colnames ( SST.df ) <- ' SSTmean'
colnames ( SST.full.df ) <- ' SSTmean'
############
SST.full.df $ Julday1 <- seq ( 1 , length ( SST.full.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( SSTmean ~ Julday1 , SST.full.df )
SST.full.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
SST.full.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
SST.df $ Julday1 <- seq ( 1 , length ( SST.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all2 <- lm ( SSTmean ~ Julday1 , SST.df )
SST.pvalNCV_ECall <- summary ( linear.trend.model_EC_all2 ) $ coefficients [2 , 4 ]
SST.lintrend <- summary ( linear.trend.model_EC_all2 ) $ coefficients [2 , 1 ] * 365
############ SST
############Estuary temp
#Load a daily (no gaps) time series as a time serie baseline for other time series used here
#Here we use the raw DPI CSV format that comes with a bunch of metadata
EstT.df <- data.frame ( read.csv ( EstT_CSV_Path ) )
char.df <- data.frame ( lapply ( EstT.df [2 , ] , as.character ) , stringsAsFactors = FALSE )
#dat <- data.frame(lapply(EstT.df[(4:nrow(EstT.df)),(2:ncol(EstT.df))], as.numeric), stringsAsFactors=FALSE)
#dat <- EstT.df.num[!is.na(as.numeric(as.character(EstT.df.num))),]
dat <- EstT.df [ ( 4 : nrow ( EstT.df ) ) , ]
colnames ( dat ) <- lapply ( EstT.df [2 , ] , as.character )
dat $ Date <- gsub ( x = dat $ Date , pattern = " 00:00:00" , replacement = " " , fixed = T )
colnames ( dat ) <- gsub ( x = colnames ( dat ) , pattern = " Water Temp(C)" , replacement = " Temp" , fixed = T )
EstT.df <- dat
rm ( dat , char.df )
#replace negative values with NA
EstT.df $ Temp <- replace ( EstT.df $ Temp , which ( as.numeric ( as.character ( EstT.df $ Temp ) ) < 11 ) , NA )
EstT.full.TS <- zoo ( as.numeric ( as.character ( EstT.df $ Temp ) ) , order.by = as.Date ( EstT.df [ , " Date" ] , format = " %d/%m/%Y" ) ) #=daily time series of rainfall for creation of clean, daily TS of ET and Q
EstT.TS <- window ( EstT.full.TS , start = as.Date ( " 2013-06-01" ) , end = as.Date ( " 2018-06-01" ) )
EstT.full.TS <- EstT.TS
EstT.full.df <- data.frame ( EstT.TS ) ### This is only done because of poor data at beginning
EstT.df <- data.frame ( EstT.TS )
colnames ( EstT.df ) <- ' EstTmean'
colnames ( EstT.full.df ) <- ' EstTmean'
############
EstT.df $ Julday1 <- seq ( 1 , length ( EstT.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( EstTmean ~ Julday1 , EstT.df )
EstT.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
EstT.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
EstT.full.df $ Julday1 <- seq ( 1 , length ( EstT.full.df [ , 1 ] ) , 1 )
linear.trend.model_EC_all <- lm ( EstTmean ~ Julday1 , EstT.full.df )
EstT.full.pvalNCV_ECall <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 4 ]
EstT.full.lintrend <- summary ( linear.trend.model_EC_all ) $ coefficients [2 , 1 ] * 365
############Est temp
######################
#Plot
######################
##################################### Full Time Period
#Air temp Full period
p1air <- ggplot ( AirT.full.df , aes ( y = tasmean , x = index ( AirT.full.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , " Catchment Centroid - Linear and smooth trends in catchment airT (SILO) lin trend was " ,
round ( AirT.full.lintrend , 3 ) , ' C<> /year with p=' , round ( AirT.full.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( " Air Temperature [C<> ]" ) + xlab ( " Time" )
p1riv <- ggplot ( RivT.full.df , aes ( y = rivTmean , x = index ( RivT.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , " - Linear and smooth trends in gauged river temperature (@" , Riv.Gauge.loc , " ) - Linear trend was " ,
round ( RivT.full.lintrend , 3 ) , ' C<EFBFBD> /year with p=' , sprintf ( " %.5f" , round ( RivT.full.pvalNCV_ECall , 10 ) ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( " River Temperature [C<> ]" ) + xlab ( " Time" ) + xlab ( NULL ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
if ( logtransformFlow == TRUE ) {
p1rivQ <- ggplot ( RivQ.full.df , aes ( y = log10 ( RivQmean +2 ) , x = index ( RivQ.full.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Riv.Gauge.loc , " - Linear and smooth trend in river flow (GAUGE) lin trend was " ,
round ( RivQ.full.lintrend , 3 ) , ' ML/day /year with p=' , round ( RivQ.full.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( expression ( paste ( " log10(River Flow [ML/day] + 2)" , sep = " " ) ) ) + xlab ( " Time" ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
} else {
p1rivQ <- ggplot ( RivQ.full.df , aes ( y = RivQmean , x = index ( RivQ.full.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Riv.Gauge.loc , " - Linear and smooth trend in river flow (GAUGE) lin trend was " ,
round ( RivQ.full.lintrend , 3 ) , ' ML/day with p=' , round ( RivQ.full.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( expression ( paste ( " River Flow [ML/day]" , sep = " " ) ) ) + xlab ( " Time" ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
}
#Sea temp Full period
p1sst <- ggplot ( SST.full.df , aes ( y = SSTmean , x = index ( SST.full.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , " offshore - Linear and smooth trends in sea surface temperature (NNRP NARCLIM reanalysis) linear trend was " ,
round ( SST.full.lintrend , 3 ) , ' C<EFBFBD> /year with p=' , sprintf ( " %.5f" , round ( SST.full.pvalNCV_ECall , 10 ) ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( " Sea Surface Temperature [C<> ]" ) + xlab ( " Time" ) + xlab ( NULL ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
p1Est <- ggplot ( EstT.full.df , aes ( y = EstTmean , x = index ( EstT.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Est.Gauge.loc , " - Linear and smooth trend in Estuary temperature (GAUGE) lin trend was " ,
round ( EstT.full.lintrend , 3 ) , ' C<EFBFBD> /year with p=' , round ( EstT.full.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( " Estuary Temperature [C<> ]" ) + xlab ( " Time" ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
p1Est2 <- ggplot ( EstT.full.df , aes ( y = EstTmean , x = index ( EstT.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Est.Gauge.loc , " - Linear and smooth trend in Estuary temperature (GAUGE) lin trend was " ,
round ( EstT.full.lintrend , 3 ) , ' C<EFBFBD> /year with p=' , round ( EstT.full.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
#stat_smooth(method=gam, formula=y~s(x, k=ggplotGAM.k), se=T, size=0.5) +
binomial_smooth ( formula = y ~ splines :: ns ( x , 2 ) ) +
ylab ( " Estuary Temperature [C<> ]" ) + xlab ( " Time" ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
png.name <- paste ( Output.Directory , Estuary , ' @' , Est.Gauge.loc , ' _Trends_estTmean_full_period_' , Sys.Date ( ) , " nosmoothb3.png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 4 , units = ' in' , res = 500 )
grid.arrange ( p1Est2 , ncol = 1 )
dev.off ( )
gA1 <- ggplotGrob ( p1riv )
gA2 <- ggplotGrob ( p1sst )
gA1 $ widths <- gA2 $ widths
#export to png
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_tasmean_full_period_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p1air , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_rivTmean_full_period_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p1riv , p1riv , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_rivTmean_SSTmean_full_period4_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gA1 , gA2 , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_RivQmean_full_period_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p1rivQ , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_SSTmean_full_period_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p1sst , p1sst , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Est.Gauge.loc , ' _Trends_estTmean_full_period_' , Sys.Date ( ) , " b.png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 4 , units = ' in' , res = 500 )
grid.arrange ( p1Est , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Est.Gauge.loc , ' _Trends_estTmean_full_period_' , Sys.Date ( ) , " nosmoothb.png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 4 , units = ' in' , res = 500 )
grid.arrange ( p1Est2 , ncol = 1 )
dev.off ( )
##################################### Full Time Period
######################### 1990-present
combined.TS <- window ( merge ( AirT.full.TS , window ( RivT.full.TS , start = as.Date ( " 1995-01-01" ) , end = end ( RivT.full.TS ) ) , SST.full.TS , EstT.full.TS , RivQ.full.TS , all = T ) , start = as.Date ( " 1990-01-01" ) , end = end ( AirT.full.TS ) )
combined.df <- data.frame ( combined.TS )
colnames ( combined.df ) <- c ( ' tasmean' , ' rivTmean' , ' SSTmean' , ' EstTmean' , ' rivQmean' )
#Air temp
p2air <- ggplot ( combined.df , aes ( y = tasmean , x = index ( combined.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , " Catchment centroid - Linear and smooth trend in catchment airT (SILO) linear trend was " ,
round ( AirT.lintrend , 3 ) , ' C<> /year with p=' , round ( AirT.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( " Air Temperature [C<> ]" ) + xlab ( NULL ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
#Riv temp
p2riv <- ggplot ( combined.df , aes ( y = rivTmean , x = index ( combined.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Riv.Gauge.loc , " - Linear and smooth trend in river temperature (GAUGE) linear trend was " ,
round ( RivT.lintrend , 3 ) , ' C<> /year with p=' , round ( RivT.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( " River Temperature [C<> ]" ) + xlab ( NULL ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
#Riv flow
if ( logtransformFlow == TRUE ) {
p2rivQ <- ggplot ( combined.df , aes ( y = log10 ( rivQmean +2 ) , x = index ( combined.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Riv.Gauge.loc , " - Linear and smooth trend in river flow (GAUGE) linear trend was " ,
round ( RivQ.full.lintrend , 3 ) , ' cubic-meter/year with p=' , round ( RivQ.full.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( expression ( paste ( " log10(River flow [m" ^ " 3" * " /s] + 2)" , sep = " " ) ) ) + xlab ( NULL ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
} else {
p2rivQ <- ggplot ( combined.df , aes ( y = rivQmean , x = index ( combined.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Riv.Gauge.loc , " - Linear and smooth trend in river flow (GAUGE) linear trend was " ,
round ( RivT.lintrend , 3 ) , ' cubic-meter/year with p=' , round ( RivT.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( expression ( paste ( " River flow [m" ^ " 3" * " /s]" , sep = " " ) ) ) + xlab ( NULL ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
}
#Sea temp
p2sst <- ggplot ( combined.df , aes ( y = SSTmean , x = index ( combined.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , " NNRP(NARCLIM reanalysis) - Linear and smooth trend in sea surface temperature (NNRP) lin trend was " ,
round ( SST.lintrend , 3 ) , ' C<> /year with p=' , round ( SST.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
stat_smooth ( method = gam , formula = y ~ s ( x , k = ggplotGAM.k ) , se = T , size = 0.5 ) +
ylab ( " Sea Surface Temperature [C<> ]" ) + xlab ( " Time" ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
#sst temp
p2Est <- ggplot ( combined.df , aes ( y = EstTmean , x = index ( combined.TS ) ) ) + geom_line ( alpha = 0.5 ) +
ggtitle ( paste ( Estuary , ' @' , Est.Gauge.loc , " - Linear and smooth trend in Estuary temperature (GAUGE) linear trend was " ,
round ( EstT.lintrend , 3 ) , ' C<> /year with p=' , round ( EstT.pvalNCV_ECall , 10 ) , sep = " " ) ) +
theme ( plot.title = element_text ( face = " bold" , size = 9 ) ) +
geom_smooth ( method = ' lm' , fill = " green" , formula = y ~ x , colour = " darkgreen" , size = 0.5 ) +
#stat_smooth(method=gam, formula=y~s(x, k=ggplotGAM.k), se=T, size=0.5) +
ylab ( " Estuary Temperature [C<> ]" ) + xlab ( " Time" ) +
theme ( axis.text = element_text ( size = Fontsize ) ) +
theme ( panel.grid.major.x = element_blank ( ) , panel.grid.minor.x = element_blank ( ) , panel.grid.major.y = element_line ( size = .1 , color = " white" ) )
#export to png
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_tasmean_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p2air , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_rivTmean_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p2riv , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_rivQmean_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p2rivQ , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_SSTmean_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( p2sst , ncol = 1 )
dev.off ( )
#export an overview plot as png
gA <- ggplotGrob ( p2air )
gB <- ggplotGrob ( p2riv )
gC <- ggplotGrob ( p2sst )
gD <- ggplotGrob ( p2Est )
gE <- ggplotGrob ( p2rivQ )
gA $ widths <- gE $ widths
gC $ widths <- gE $ widths
gD $ widths <- gE $ widths
gB $ widths <- gE $ widths
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_SST_RivT_AirT_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gA , gB , gC , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_SST_RivT_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gB , gC , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_AirT_RivT_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gA , gB , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_RivQ_RivT_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gB , gE , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_SST_RivT_EstT_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gB , gD , gC , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_RivQ_RivT_EstT_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gE , gB , gD , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_AirT_RivT_RivQ_1990-present_' , Sys.Date ( ) , " b.png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 11 , units = ' in' , res = 500 )
grid.arrange ( gB , gA , gE , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_AirT_RivT_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gA , gB , ncol = 1 )
dev.off ( )
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Trends_RivT_RivQ_1990-present_' , Sys.Date ( ) , " .png" , sep = " " )
png ( file = png.name , width = 10.5 , height = 7 , units = ' in' , res = 500 )
grid.arrange ( gB , gE , ncol = 1 )
dev.off ( )
lm_eqn <- function ( df ) {
m <- lm ( y ~ x , df ) ;
eq <- substitute ( italic ( y ) == a + b %.% italic ( x ) * " ," ~ ~ italic ( r ) ^2 ~ " =" ~ r2 ,
list ( a = format ( coef ( m ) [1 ] , digits = 2 ) ,
b = format ( coef ( m ) [2 ] , digits = 2 ) ,
r2 = format ( summary ( m ) $ r.squared , digits = 3 ) ) )
as.character ( as.expression ( eq ) ) ;
}
Modeling <- ' yes'
if ( Modeling == ' yes' ) {
source ( " C:/Users/z5025317/OneDrive - UNSW/WRL_Postdoc_Manual_Backup/WRL_Postdoc/Projects/Paper#1/Analysis/Code/FUN_do_ggplot_Scatterplot_2vars.R" )
AirT.full.TS.Monthly <- aggregate ( AirT.full.TS , as.yearmon ( time ( AirT.full.TS ) ) , FUN = mean )
RivT.full.TS.Monthly <- aggregate ( RivT.full.TS , as.yearmon ( time ( RivT.full.TS ) ) , FUN = mean )
RivQ.full.TS.Monthly <- aggregate ( RivQ.full.TS , as.yearmon ( time ( RivQ.full.TS ) ) , FUN = mean )
if ( logtransformFlow == TRUE ) {
RivQ.full.TS.Monthly <- log10 ( RivQ.full.TS.Monthly +2 )
}
All.TS.zoo <- merge ( AirT.full.TS.Monthly , RivT.full.TS.Monthly , RivQ.full.TS.Monthly , all = F )
AirvsRivT <- Generate.ggplot.scatterplot ( All.TS.zoo [ , 2 ] , All.TS.zoo [ , 1 ] , " River temp. [<5B> C]" , " Air temp. [<5B> C]" )
if ( logtransformFlow == TRUE ) {
QvsRivT <- Generate.ggplot.scatterplot ( All.TS.zoo [ , 2 ] , All.TS.zoo [ , 3 ] , " River temp. [<5B> C]" , expression ( paste ( " log10(River flow [m" ^ " 3" * " /s] + 2)" , sep = " " ) ) )
} else {
QvsRivT <- Generate.ggplot.scatterplot ( All.TS.zoo [ , 2 ] , All.TS.zoo [ , 3 ] , " River temp. [<5B> C]" , expression ( paste ( " River flow [m" ^ " 3" * " /s]" , sep = " " ) ) )
}
#export an overview plot as png
gA <- ggplotGrob ( AirvsRivT )
gB <- ggplotGrob ( QvsRivT )
gB $ widths <- gA $ widths
png.name <- paste ( Output.Directory , Estuary , ' @' , Riv.Gauge.loc , ' _Scatterplots_of_RivT_vs_predictors_1990-present_' , Sys.Date ( ) , " f.png" , sep = " " )
png ( file = png.name , width = 8 , height = 4 , units = ' in' , res = 500 )
grid.arrange ( gA , gB , ncol = 2 )
dev.off ( )
}