diff --git a/plotting/panel.R b/plotting/panel.R index 3c601b1ec15b288396648892d941752c86565aa2..2d2cc42ea895d5fb5773a3f6907df37b3e95b116 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -1,11 +1,12 @@ # Usefull library library(ggplot2) +library(scales) library(qpdf) library(gridExtra) # Time panel -panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', filename_opt='', variable='', df_trend=NULL, unit2day=365.25, is_sqrt=FALSE) { +panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', filename_opt='', variable='', df_trend=NULL, unit2day=365.25, is_sqrt=FALSE, missRect=FALSE) { outfile = "Panels" @@ -42,12 +43,28 @@ panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', fil dDate = df_data_code$Date[length(df_data_code$Date)] - df_data_code$Date[1] - datebreak = round(as.numeric(dDate) / unit2day / 12 , 0) + datebreak = round(as.numeric(dDate) / unit2day / 11 , 0) - p = ggplot() + + p = ggplot() + theme_bw() + geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s), color='black') + + if (missRect) { + NAdate = df_data_code$Date[is.na(df_data_code$Qm3s)] + dNAdate = diff(NAdate) + NAdate_Down = NAdate[append(Inf, dNAdate) != 1] + NAdate_Up = NAdate[append(dNAdate, Inf) != 1] + + + p = p + + geom_rect(aes(xmin=NAdate_Down, + ymin=0, + xmax=NAdate_Up, + ymax=max(df_data_code$Qm3s, na.rm=TRUE)*1.1), + linetype=0, fill='Wheat', alpha=0.3) + } + if (!is.null(df_trend)) { if (df_trend[df_trend$code == code,]$p < p_threshold) { @@ -65,15 +82,52 @@ panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', fil }} p = p + - ggtitle(paste(variable, 'station', - as.character(code), sep=' ')) + + # ggtitle(paste(variable, 'station', + # as.character(code), sep=' ')) + ylab(expression(paste('débit [', m^{3}, '.', s^{-1}, ']', sep=''))) + xlab('date') + - scale_x_date(date_breaks=paste(as.character(datebreak), 'year', sep=' '), date_labels="%Y") + scale_x_date(date_breaks=paste(as.character(datebreak), + 'year', sep=' '), + date_labels="%Y", + limits=c(min(df_data_code$Date), + max(df_data_code$Date)), + expand=c(0, 0)) + + if (is_sqrt) { + p = p + + scale_y_continuous(breaks=seq(0, 100, 10), + minor_breaks=seq(0, 100, 5), + limits=c(0, + max(df_data_code$Qm3s, + na.rm=TRUE)*1.1), + expand=c(0, 0)) + } else { + p = p + + scale_y_continuous(breaks=seq(0, 10000, 1000), + minor_breaks=seq(0, 10000, 500), + limits=c(0, + max(df_data_code$Qm3s, + na.rm=TRUE)*1.1), + expand=c(0, 0)) + } + + df_meta_code = df_meta[df_meta$code == code,] + text = textGrob(paste( + 'station ', code, '\n', + 'nom : ', df_meta_code$nom, '\t', + 'territoire : ', df_meta_code$territoire, '\n', + 'position : (', df_meta_code$L93X, '; ', df_meta_code$L93Y, ')', '\t', + 'surface : ', df_meta_code$surface_km2, ' km2', + sep=''), + just='left', + gp=gpar(col="darkgrey", fontsize=10)) + + plot = grid.arrange(text, p, ncol=1, nrow=2, heights=c(1/4, 3/4)) # Saving - ggsave(path=outdirTmp, + ggsave(plot=plot, + path=outdirTmp, filename=paste(as.character(code), '.pdf', sep=''), width=29.7, height=21, units='cm', dpi=100) diff --git a/script.R b/script.R index e02c8e289f746dd477529ef9e4994046d8f5cfc1..cc1a9da3bf92039520d367b01081a0e82247579b 100644 --- a/script.R +++ b/script.R @@ -139,8 +139,8 @@ df_meta = df_join$meta # TIME PANEL # # Plot time panel of debit by stations -# panel(df_data, df_meta, figdir, "") -# panel(df_data, df_meta, figdir, "", is_sqrt=TRUE) +panel(df_data, df_meta, figdir, "", missRect=TRUE) +# panel(df_data, df_meta, figdir, "", missRect=TRUE, is_sqrt=TRUE) ### /!\ Removed 185 row(s) containing missing values (geom_path) -> remove NA ### @@ -150,12 +150,12 @@ df_meta = df_join$meta # QA TREND # -res_QAtrend = get_QAtrend(df_data, period) -panel(df_data=res_QAtrend$data, - df_meta=df_meta, - df_trend=res_QAtrend$trend, - p_threshold=0.1, - figdir=figdir, filedir_opt='QA', variable='QA') +# res_QAtrend = get_QAtrend(df_data, period) +# panel(df_data=res_QAtrend$data, +# df_meta=df_meta, +# df_trend=res_QAtrend$trend, +# p_threshold=0.1, +# figdir=figdir, filedir_opt='QA', variable='QA') # QMNA TREND # # res_QMNAtrend = get_QMNAtrend(df_data, period) diff --git a/script_install.R b/script_install.R index b520c2c24389cb7adce0edbc476ea9e40f20cde0..268717774b2c2c80f80c801804155e8aa65d82f5 100644 --- a/script_install.R +++ b/script_install.R @@ -8,6 +8,7 @@ install.packages("officer") install.packages("lubridate") install.packages('zoo') install.packages("qpdf") +install.packages("scales") library(devtools) install_github("https://github.com/benRenard/BFunk") #type '1'