diff --git a/plotting/layout.R b/plotting/layout.R index 186bf29c16b84abdd2d7542b5c323e80c3d04e30..6d7f5406c72e01103bcf2700665da74d8f478e65 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -14,7 +14,7 @@ library(RColorBrewer) source('plotting/panel.R', encoding='latin1') -panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', filename_opt='', variable='', df_trend=NULL, p_threshold=0.1, unit2day=365.25, type='', trend_period=NULL, mean_period=NULL, missRect=FALSE, time_header=NULL, info_header=TRUE, time_ratio=2, var_ratio=3) { +panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', filename_opt='', variable='', df_trend=NULL, p_threshold=0.1, unit2day=365.25, type='', trend_period=NULL, mean_period=NULL, axis_xlim=NULL, missRect=FALSE, time_header=NULL, info_header=TRUE, time_ratio=2, var_ratio=3) { outfile = "Panels" if (filename_opt != '') { @@ -218,6 +218,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op if (!is.null(time_header)) { time_header_code = time_header[time_header$code == code,] + axis_xlim = c(min(time_header_code$Date), + max(time_header_code$Date)) Htime = time_panel(time_header_code, df_trend_code=NULL, trend_period=trend_period, missRect=TRUE, @@ -241,6 +243,7 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op color = c() # for (j in 1:nrow(df_trend_code)) { + grey = 85 for (j in 1:nPeriod_max) { if (df_trend_code$p[j] <= p_threshold){ # color_res = get_color(df_trend_code$trend[j], @@ -277,7 +280,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op colortmp = color_res$color } else { - colortmp = NA + colortmp = paste('grey', grey, sep='') + grey = grey - 10 } color = append(color, colortmp) @@ -286,8 +290,9 @@ 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) + mean_period=mean_period, axis_xlim=axis_xlim, + unit2day=unit2day, last=(i > nbp-nbcol), + color=color) P[[i+nbh]] = p diff --git a/plotting/panel.R b/plotting/panel.R index dcdbe68c47b2b515ff589451eba53b8750d773f0..5102b5610aa96dbeb39c2c9cbab03d85bfeb41e6 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -10,7 +10,7 @@ library(ggh4x) library(RColorBrewer) -time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missRect=FALSE, unit2day=365.25, trend_period=NULL, mean_period=NULL, last=FALSE, first=FALSE, color=NULL) { +time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missRect=FALSE, unit2day=365.25, trend_period=NULL, mean_period=NULL, axis_xlim=NULL, last=FALSE, first=FALSE, color=NULL) { if (type == 'sqrt(Q)') { df_data_code$Qm3s = sqrt(df_data_code$Qm3s) @@ -141,6 +141,10 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR minPer = df_data_code$Date[idMinPer] maxPer = df_data_code$Date[idMaxPer] + if (type != 'sqrt(Q)' & type != 'Q') { + maxPer = maxPer + years(1) + } + p = p + geom_rect(aes(xmin=minPer, ymin=0, @@ -155,7 +159,7 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR nPeriod_mean = length(mean_period) plot_mean = tibble() - plot_meanL = tibble() + plot_line = tibble() for (j in 1:nPeriod_mean) { Start_mean = mean_period[[j]][1] End_mean = mean_period[[j]][2] @@ -174,17 +178,35 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR xmax = xmax + months(6) } + if (type != 'sqrt(Q)' & type != 'Q' & j == nPeriod_mean) { + xmax = xmax + years(1) + } + 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) + + abs = c(xmin, xmax) + ord = c(ymax, ymax) + + plot_linetmp = tibble(abs=abs, ord=ord, period=j) + plot_line = bind_rows(plot_line, plot_linetmp) } + p = p + geom_rect(data=plot_mean, aes(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax), linetype=0, fill='grey93') + p = p + + geom_line(data=plot_line, + aes(x=abs, y=ord, group=period), + color='grey85', + size=0.15) + + # plot_grid = tibble(abs=as.Date(abs), ord=ord) for (i in 1:(nPeriod_mean-1)) { yLim = max(c(plot_mean$ymax[i], plot_mean$ymax[i+1])) @@ -192,14 +214,19 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR 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') + linetype='dashed', size=0.15, color='grey85') } } ### Grid ### - xmin = min(df_data_code$Date) - xmax = max(df_data_code$Date) + if (is.null(axis_xlim)) { + xmin = min(df_data_code$Date) + xmax = max(df_data_code$Date) + } else { + xmin = axis_xlim[1] + xmax = axis_xlim[2] + } ygrid = seq(0, maxQ*10, dbrk) ord = c() abs = c() @@ -214,7 +241,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR 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), @@ -249,10 +275,8 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR nPeriod_trend = max(length(UStart), length(UEnd)) - ltype = c('solid', 'dashed', 'dotted', 'twodash') - lty = c('solid', '22', 'dotted', 'twodash') - - # ii = 0 + plot_trend = tibble() + leg_trend = tibble() for (i in 1:nPeriod_trend) { df_trend_code_per = @@ -261,27 +285,25 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR iStart = which.min(abs(df_data_code$Date - Start[i])) iEnd = which.min(abs(df_data_code$Date - End[i])) + + xmin = df_data_code$Date[iStart] + xmax = df_data_code$Date[iEnd] + if (!is.null(axis_xlim)) { + if (xmin < axis_xlim[1]) { + xmin = axis_xlim[1] + } + if (xmax > axis_xlim[2]) { + xmax = axis_xlim[2] + } + } - abs = c(df_data_code$Date[iStart], - df_data_code$Date[iEnd]) + abs = c(xmin, xmax) abs_num = as.numeric(abs) / unit2day ord = abs_num * df_trend_code_per$trend + df_trend_code_per$intercept - plot_trend = tibble(abs=abs, ord=ord) - - if (df_trend_code_per$p <= p_threshold) { - p = p + - geom_line(data=plot_trend, aes(x=abs, y=ord), - color=color[i], - linetype=ltype[i], size=0.7) - colortxt = color[i] - } else { - p = p + - geom_line(data=plot_trend, aes(x=abs, y=ord), - color='grey85', linetype=ltype[i], - size=0.5) - colortxt = 'grey85' - } + + plot_trendtmp = tibble(abs=abs, ord=ord, period=i) + plot_trend = bind_rows(plot_trend, plot_trendtmp) codeDate = df_data_code$Date codeQ = df_data_code$Qm3s @@ -291,45 +313,122 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR dy = gpct(7, codeQ, ref=0) y = gpct(100, codeQ, ref=0) - (i-1)*dy - + yend = y + xt = xend + gpct(1, codeDate) + xminR = x - gpct(1, codeDate) + yminR = y - gpct(4, codeQ, ref=0) + xmaxR = x + gpct(24, codeDate) + ymaxR = y + gpct(5, codeQ, ref=0) + trend = df_trend_code_per$trend power = power = get_power(trend) powerC = as.character(power) brk = 10^power trendC = as.character(round(trend / brk, 2)) - label = bquote(bold(.(trendC)~'x'~'10'^{.(powerC)})~'['*m^{3}*'.'*s^{-1}*'.'*an^{-1}*']') + leg_trendtmp = tibble(x=x, xend=xend, + y=y, yend=yend, + xt=xt, + trendC=trendC, + powerC=powerC, + xminR=xminR, yminR=yminR, + xmaxR=xmaxR, ymaxR=ymaxR, + period=i) + + leg_trend = bind_rows(leg_trend, leg_trendtmp) + } + + for (i in 1:nPeriod_trend) { + leg_trend_per = leg_trend[leg_trend$period == i,] + p = p + + geom_rect(data=leg_trend_per, + aes(xmin=xminR, + ymin=yminR, + xmax=xmaxR, + ymax=ymaxR), + linetype=0, fill='white', alpha=0.5) + } + + for (i in 1:nPeriod_trend) { + leg_trend_per = leg_trend[leg_trend$period == i,] + + trendC = leg_trend_per$trendC + powerC = leg_trend_per$powerC + + label = bquote(bold(.(trendC)~'x'~'10'^{.(powerC)})~'['*m^{3}*'.'*s^{-1}*'.'*an^{-1}*']') + + p = p + + annotate("segment", - x=x, xend=xend, - y=y, yend=y, - color=colortxt, - lty=lty[i], lwd=1) + - - annotate("text", + x=leg_trend_per$x, xend=leg_trend_per$xend, + y=leg_trend_per$y, yend=leg_trend_per$yend, + color=color[i], + linetype='solid', + lwd=1) + + + annotate("text", label=label, size=3, - x=xt, y=y, + x=leg_trend_per$xt, y=leg_trend_per$y, hjust=0, vjust=0.4, - color=colortxt) + color=color[i]) } + + for (i in 1:nPeriod_trend) { + plot_trend_per = plot_trend[plot_trend$period == i,] + p = p + + geom_line(data=plot_trend_per, + aes(x=abs, y=ord), + color='white', + linetype='solid', + size=1) + } + + for (i in 1:nPeriod_trend) { + plot_trend_per = plot_trend[plot_trend$period == i,] + p = p + + geom_line(data=plot_trend_per, + aes(x=abs, y=ord), + color=color[i], + linetype='solid', + size=0.5) + } + + + } p = p + - ggtitle(bquote(bold(.(type))~~'['*m^{3}*'.'*s^{-1}*']')) + - - scale_x_date(date_breaks=paste(as.character(datebreak), - 'year', sep=' '), - date_minor_breaks=paste(as.character(dateminbreak), - 'year', sep=' '), - guide='axis_minor', - date_labels="%Y", - limits=c(min(df_data_code$Date), - max(df_data_code$Date)), - expand=c(0, 0)) + ggtitle(bquote(bold(.(type))~~'['*m^{3}*'.'*s^{-1}*']')) + if (is.null(axis_xlim)) { + p = p + + scale_x_date(date_breaks=paste(as.character(datebreak), + 'year', sep=' '), + date_minor_breaks=paste(as.character(dateminbreak), + 'year', sep=' '), + guide='axis_minor', + date_labels="%Y", + limits=c(min(df_data_code$Date), + max(df_data_code$Date)), + expand=c(0, 0)) + } else { + p = p + + scale_x_date(date_breaks=paste( + as.character(datebreak), + 'year', sep=' '), + date_minor_breaks=paste( + as.character(dateminbreak), + 'year', sep=' '), + guide='axis_minor', + date_labels="%Y", + limits=axis_xlim, + expand=c(0, 0)) + } + p = p + scale_y_continuous(breaks=seq(0, maxQ*10, dbrk), limits=c(0, maxQ*1.1), @@ -1014,24 +1113,24 @@ get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse # '#fdb147', # '#fd4659' - # '#0f3b57', - # '#1d7881', - # '#80c4a9', - # '#e2dac6', #mid - # '#fadfad', - # '#d08363', - # '#7e392f' - - '#193830', - '#2A6863', - '#449C93', - '#7ACEB9', - '#BCE6DB', - '#EFE0B0', - '#D4B86A', - '#B3762A', - '#7F4A23', - '#452C1A' + '#0f3b57', + '#1d7881', + '#80c4a9', + '#e2dac6', #mid + '#fadfad', + '#d08363', + '#7e392f' + + # '#193830', + # '#2A6863', + # '#449C93', + # '#7ACEB9', + # '#BCE6DB', + # '#EFE0B0', + # '#D4B86A', + # '#B3762A', + # '#7F4A23', + # '#452C1A' ))(ncolor) @@ -1103,24 +1202,24 @@ palette_tester = function () { # '#01665e', # '#003c30' - # '#0f3b57', - # '#1d7881', - # '#80c4a9', - # '#e2dac6', #mid - # '#fadfad', - # '#d08363', - # '#7e392f' - - '#193830', - '#2A6863', - '#449C93', - '#7ACEB9', - '#BCE6DB', - '#EFE0B0', - '#D4B86A', - '#B3762A', - '#7F4A23', - '#452C1A' + '#0f3b57', + '#1d7881', + '#80c4a9', + '#e2dac6', #mid + '#fadfad', + '#d08363', + '#7e392f' + + # '#193830', + # '#2A6863', + # '#449C93', + # '#7ACEB9', + # '#BCE6DB', + # '#EFE0B0', + # '#D4B86A', + # '#B3762A', + # '#7F4A23', + # '#452C1A' ))(n)