From d4de6dccc368251b6127b63b638ead74548e1e19 Mon Sep 17 00:00:00 2001 From: "louis.heraut" <louis.heraut@inrae.fr> Date: Wed, 24 Nov 2021 21:03:13 +0100 Subject: [PATCH] Plot aes --- plotting/panel.R | 99 ++++++++++++++++++++++++++++++++++-------------- script.R | 2 +- script_install.R | 4 +- 3 files changed, 73 insertions(+), 32 deletions(-) diff --git a/plotting/panel.R b/plotting/panel.R index 062625c..7f94a6a 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -7,6 +7,7 @@ library(gridtext) library(dplyr) library(grid) library(ggh4x) +library(RColorBrewer) # Time panel @@ -79,9 +80,17 @@ panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', fi if (!(file.exists(outdirTmp))) { dir.create(outdirTmp) } - + + # Get all different stations code Code = levels(factor(df_meta$code)) + nCode = length(Code) + + + # minTrend = min(list_df2plot$trend$p, na.rm=TRUE) + # maxTrend = max(list_df2plot$trend$p, na.rm=TRUE) + # get_color(value, minTrend, maxTrend, nCode, "RdYlBu") + for (code in Code) { @@ -99,9 +108,12 @@ panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', fi } if (!is.null(time_header)) { - Htime = time_panel(code, time_header, df_trend=NULL, + time_header_code = time_header[time_header$code == code,] + + Htime = time_panel(time_header_code, df_trend_code=NULL, period=period, missRect=TRUE, unit2day=365.25, type='Q') + P[[2]] = Htime } @@ -115,12 +127,14 @@ panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', fi missRect = list_df2plot[[i]]$missRect type = list_df2plot[[i]]$type - p = time_panel(code, df_data, df_trend, missRect, - p_threshold, unit2day, type, - last=(i > nbp-nbcol)) + df_data_code = df_data[df_data$code == code,] + df_trend_code = df_trend[df_trend$code == code,] - P[[i+nbh]] = p + p = time_panel(df_data_code, df_trend_code, type=type, + p_threshold=p_threshold, missRect=missRect, + unit2day=unit2day, last=(i > nbp-nbcol)) + P[[i+nbh]] = p } layout_matrix = as.matrix(layout_matrix) @@ -174,38 +188,56 @@ panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', fi -time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, type, period=NULL, norm=TRUE, last=FALSE) { - - df_data_code = df_data[df_data$code == code,] +time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missRect=FALSE, unit2day=365.25, period=NULL, norm=TRUE, last=FALSE) { + if (type == 'sqrt(Q)') { df_data_code$Qm3s = sqrt(df_data_code$Qm3s) } - maxQ = max(df_data_code$Qm3s, na.rm=TRUE) - power = nchar(as.character(as.integer(maxQ))) - 1 + + if (maxQ > 1) { + power = nchar(as.character(as.integer(maxQ))) - 1 + } else { + dec = gsub('0.', '', as.character(maxQ), fixed=TRUE) + ndec = nchar(dec) + nnum = nchar(as.character(as.numeric(dec))) + power = -(ndec - nnum + 1) + } dbrk = 10^power - + ### /!\ PROBLĂME entre 11 et 10 ### if (norm) { df_data_code$Qm3s = df_data_code$Qm3s / dbrk + + if (!is.null(df_trend_code)) { + df_trend_code$p = df_trend_code$p / dbrk + df_trend_code$intercept = df_trend_code$intercept / dbrk + } + maxQ = max(df_data_code$Qm3s, na.rm=TRUE) + if (maxQ >= 5) { dbrk = 1.0 + accuracy = 0.1 } else if (maxQ < 5 & maxQ >= 3) { dbrk = 0.5 + accuracy = 0.1 } else if (maxQ < 3 & maxQ >= 2) { dbrk = 0.4 + accuracy = 0.1 } else if (maxQ < 2 & maxQ >= 1) { - dbrk = 0.3 - } else if (maxQ < 1) { dbrk = 0.2 + accuracy = 0.1 + } else if (maxQ < 1) { + dbrk = 0.1 + accuracy = 0.1 } } - + dDate = as.numeric(df_data_code$Date[length(df_data_code$Date)] - - df_data_code$Date[1]) / unit2day + df_data_code$Date[1]) / unit2day # datebreak = round(as.numeric(dDate) / unit2day / 11 , 0) @@ -224,7 +256,7 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, # theme_bw() + - ggtitle(bquote(.(type)~'['*m^{3}*'.'*s^{-1}*'] x'~10^{.(as.character(power))})) + + ggtitle(bquote(.(type)~' ['*m^{3}*'.'*s^{-1}*'] x'~10^{.(as.character(power))})) + theme(panel.background=element_rect(fill='white'), text=element_text(family='sans'), @@ -242,8 +274,8 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, ggh4x.axis.ticks.length.minor=rel(0.5), axis.ticks.length=unit(1.5, 'mm'), - plot.title=element_text(size=10, vjust=-4, - hjust=0, color='grey20'), + plot.title=element_text(size=9, vjust=-3, + hjust=-1E-3, color='grey20'), axis.title.x=element_blank(), axis.title.y=element_blank(), # axis.title.y=element_text(size=8, color='grey20'), @@ -263,11 +295,12 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, if (type == 'sqrt(Q)' | type == 'Q') { p = p + geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s), - color='grey20') + color='grey20', + size=0.3) } else { p = p + - geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s), - color='grey65') + + # geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s), + # color='grey70') + geom_point(aes(x=df_data_code$Date, y=df_data_code$Qm3s), shape=1, color='grey20', size=1) } @@ -302,16 +335,16 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, linetype=0, fill='grey85', alpha=0.3) } - if (!is.null(df_trend)) { - if (df_trend[df_trend$code == code,]$p < p_threshold) { + if (!is.null(df_trend_code)) { + if (df_trend_code$p < p_threshold) { abs = c(df_data_code$Date[1], df_data_code$Date[length(df_data_code$Date)]) - abs_num = as.numeric(abs)/unit2day + abs_num = as.numeric(abs) / unit2day - ord = abs_num * df_trend$trend[df_trend$code == code] + - df_trend$intercept[df_trend$code == code] + ord = abs_num * df_trend_code$trend + + df_trend_code$intercept p = p + geom_line(aes(x=abs, y=ord), @@ -342,7 +375,8 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, scale_y_continuous(breaks=seq(0, maxQ*10, dbrk), limits=c(0, maxQ*1.1), - expand=c(0, 0)) + expand=c(0, 0), + labels=label_number(accuracy=accuracy)) return(p) } @@ -368,6 +402,15 @@ text_panel = function(code, df_meta) { } + +get_color = function (value, min, max, ncolor, palette_name="RdYlBu") { + palette = colorRampPalette(brewer.pal(11, palette_name))(ncolor) + idNorm = (value - min) / (max - min) + id = as.integer(idNorm*ncolor) + color = palette[id] + return(color) +} + void = ggplot() + geom_blank(aes(1,1)) + theme( plot.background = element_blank(), diff --git a/script.R b/script.R index 5ad4206..2a0fb33 100644 --- a/script.R +++ b/script.R @@ -23,7 +23,7 @@ BHfiledir = # Name of the file that will be analysed from the BH directory BHfilename = # "" - c("H5920011_HYDRO_QJM.txt", "K4470010_HYDRO_QJM.txt") + c("S2235610_HYDRO_QJM.txt", "P1712910_HYDRO_QJM.txt") # "all" ## Or list selection ## diff --git a/script_install.R b/script_install.R index ee6c2be..055e3dc 100644 --- a/script_install.R +++ b/script_install.R @@ -12,9 +12,7 @@ install.packages("gridtext") install.packages("lubridate") install.packages('ggh4x') install.packages('extrafont') - - - +install.packages("RColorBrewer") library(devtools) install_github("https://github.com/benRenard/BFunk") #type '1' -- GitLab