From fa1aa7a1432592a47bce0dc34cd33a55b6760c39 Mon Sep 17 00:00:00 2001 From: "louis.heraut" <louis.heraut@inrae.fr> Date: Wed, 8 Dec 2021 11:53:24 +0100 Subject: [PATCH] Mean period aes --- plotting/layout.R | 1 + plotting/panel.R | 144 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 101 insertions(+), 44 deletions(-) diff --git a/plotting/layout.R b/plotting/layout.R index 54d399d..186bf29 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -285,6 +285,7 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op p = time_panel(df_data_code, df_trend_code, type=type, p_threshold=p_threshold, missRect=missRect, + trend_period=trend_period, mean_period=mean_period, unit2day=unit2day, last=(i > nbp-nbcol), color=color) diff --git a/plotting/panel.R b/plotting/panel.R index 22d3325..dcdbe68 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -60,7 +60,8 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR fill=NA, size=0.7), - panel.grid.major.y=element_line(color='grey85', size=0.15), + # panel.grid.major.y=element_line(color='grey85', size=0.15), + panel.grid.major.y=element_blank(), panel.grid.major.x=element_blank(), axis.ticks.y=element_line(color='grey75', size=0.3), @@ -100,8 +101,31 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR } } - if ((type == 'sqrt(Q)' | type == 'Q') & !is.null(trend_period)) { + ### Sub period background ### + if (!is.null(trend_period)) { + # trend_period = as.list(trend_period) + # Imin = 10^99 + # for (per in trend_period) { + # I = interval(per[1], per[2]) + # if (I < Imin) { + # Imin = I + # trend_period_min = as.Date(per) + # } + # } + # p = p + + # geom_rect(aes(xmin=min(df_data_code$Date), + # ymin=0, + # xmax=trend_period_min[1], + # ymax= maxQ*1.1), + # linetype=0, fill='grey97') + + + # geom_rect(aes(xmin=trend_period_min[2], + # ymin=0, + # xmax=max(df_data_code$Date), + # ymax= maxQ*1.1), + # linetype=0, fill='grey97') + trend_period = as.list(trend_period) Imin = 10^99 for (per in trend_period) { @@ -112,20 +136,85 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR } } + idMinPer = which.min(abs(df_data_code$Date - trend_period_min[1])) + idMaxPer = which.min(abs(df_data_code$Date - trend_period_min[2])) + minPer = df_data_code$Date[idMinPer] + maxPer = df_data_code$Date[idMaxPer] + p = p + - geom_rect(aes(xmin=min(df_data_code$Date), - ymin=0, - xmax=trend_period_min[1], - ymax= maxQ*1.1), - linetype=0, fill='grey85', alpha=0.3) + - - geom_rect(aes(xmin=trend_period_min[2], + geom_rect(aes(xmin=minPer, ymin=0, - xmax=max(df_data_code$Date), + xmax=maxPer, ymax= maxQ*1.1), - linetype=0, fill='grey85', alpha=0.3) + linetype=0, fill='grey97') } + ### Mean step ### + if (!is.null(mean_period)) { + mean_period = as.list(mean_period) + nPeriod_mean = length(mean_period) + + plot_mean = tibble() + plot_meanL = tibble() + for (j in 1:nPeriod_mean) { + Start_mean = mean_period[[j]][1] + End_mean = mean_period[[j]][2] + + df_data_code_per = + df_data_code[df_data_code$Date >= Start_mean + & df_data_code$Date <= End_mean,] + + xmin = min(df_data_code_per$Date) + if (xmin > min(df_data_code$Date) & j != 1) { + xmin = xmin - months(6) + } + + xmax = max(df_data_code_per$Date) + if (xmax < max(df_data_code$Date) & j != nPeriod_mean) { + xmax = xmax + months(6) + } + + ymax = mean(df_data_code_per$Qm3s, na.rm=TRUE) + + plot_meantmp = tibble(xmin=xmin, xmax=xmax, + ymin=0, ymax=ymax, period=j) + plot_mean = bind_rows(plot_mean, plot_meantmp) + } + p = p + + geom_rect(data=plot_mean, + aes(xmin=xmin, ymin=ymin, + xmax=xmax, ymax=ymax), + linetype=0, fill='grey93') + + for (i in 1:(nPeriod_mean-1)) { + yLim = max(c(plot_mean$ymax[i], plot_mean$ymax[i+1])) + xLim = plot_mean$xmax[i] + plot_lim = tibble(x=c(xLim, xLim), y=c(0, yLim)) + p = p + + geom_line(data=plot_lim, aes(x=x, y=y), + linetype='b', color='grey85') + } + + } + + ### Grid ### + xmin = min(df_data_code$Date) + xmax = max(df_data_code$Date) + ygrid = seq(0, maxQ*10, dbrk) + ord = c() + abs = c() + for (i in 1:length(ygrid)) { + ord = c(ord, rep(ygrid[i], times=2)) + abs = c(abs, xmin, xmax) + } + plot_grid = tibble(abs=as.Date(abs), ord=ord) + p = p + + geom_line(data=plot_grid, + aes(x=abs, y=ord, group=ord), + color='grey85', + size=0.15) + + if (type == 'sqrt(Q)' | type == 'Q') { p = p + geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s), @@ -151,39 +240,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR linetype=0, fill='Wheat', alpha=0.4) } - if (!is.null(mean_period)) { - mean_period = as.list(mean_period) - nPeriod_mean = length(mean_period) - - for (j in 1:nPeriod_mean) { - Start_mean = mean_period[[j]][1] - End_mean = mean_period[[j]][2] - - df_data_code_per = - df_data_code[df_data_code$Date >= Start_mean - & df_data_code$Date <= End_mean,] - - absMin = min(df_data_code_per$Date) - if (absMin > min(df_data_code$Date)) { - absMin = absMin - months(6) - } - - absMax = max(df_data_code_per$Date) - if (absMax < max(df_data_code$Date)) { - absMax = absMax + months(6) - } - - abs = c(absMin, absMax) - ord = rep(mean(df_data_code_per$Qm3s, na.rm=TRUE), times=2) - plot_mean = tibble(abs=abs, ord=ord) - - p = p + - geom_line(data=plot_mean, aes(x=abs, y=ord), - color='grey40', linetype='solid', - size=0.5) - } - } - if (!is.null(df_trend_code)) { Start = df_trend_code$period_start -- GitLab