diff --git a/plotting/layout.R b/plotting/layout.R index 7f10a41c483ce87ca5dfe279180f7733c2d0cff0..c9cbee5e0690e97ee251200e17e8815339827b39 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -343,46 +343,13 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op } - # # By - # slice = 12 - # nMat = as.integer(nCode/slice) + 1 - # sublist_df2plot = list_df2plot - - # for (i in 1:nMat) { - - # subdf_meta = df_meta[(slice*(i-1)+1):(slice*i),] - # subdf_meta = subdf_meta[!is.na(subdf_meta$code),] - # subCode = subdf_meta$code - - - # for (j in 1:nbp) { - # df_datatmp = list_df2plot[[j]]$data - # df_trendtmp = list_df2plot[[j]]$trend - - # subdf_data = df_datatmp[(df_datatmp$code %in% subCode),] - # subdf_trend = df_trendtmp[(df_trendtmp$code %in% subCode),] - - # sublist_df2plot[[j]]$data = subdf_data - # sublist_df2plot[[j]]$trend = subdf_trend - # } - - # mat = matrice_panel(sublist_df2plot, - # subdf_meta, - # trend_period=trend_period) - - # Saving matrix plot - # ggsave(plot=mat, - # path=outdirTmp, - # filename=paste('matrix_', i, '.pdf', sep=''), - # width=29.7, height=21, units='cm', dpi=100) - - # } - matrice_panel(list_df2plot, df_meta, trend_period, slice=12, outdirTmp=outdirTmp) + map_panel(list_df2plot, df_meta, + idPer=length(trend_period), computer_data_path=computer_data_path, fr_shpdir=fr_shpdir, fr_shpname=fr_shpname, diff --git a/plotting/panel.R b/plotting/panel.R index ba0d72b2e88b69a0e07ec4b8fb574220433d029f..98c10403138294a8a34210f967f17b6f0154c7d1 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -11,6 +11,34 @@ library(RColorBrewer) library(rgdal) + + +display_type = function (type, bold=FALSE) { + + if (type == "QA") { + if (bold) { + disp = bquote(Q[A]) + } else { + disp = bquote(bold(Q[A])) + } + } else if (type == "QMNA") { + if (bold) { + disp = bquote(Q[MNA]) + } else { + disp = bquote(bold(Q[MNA])) + } + } else if (type == "VCN10") { + if (bold) { + disp = bquote(V[CN10]) + } else { + disp = bquote(bold(V[CN10])) + } + } + + return (disp) +} + + # Personal theme theme_ash = theme( @@ -1073,7 +1101,7 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi -map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_shpname, bs_shpdir=bs_shpdir, bs_shpname=bs_shpname, rv_shpdir, rv_shpname, outdirTmp) { +map_panel = function (list_df2plot, df_meta, idPer, computer_data_path, fr_shpdir, fr_shpname, bs_shpdir=bs_shpdir, bs_shpname=bs_shpname, rv_shpdir, rv_shpname, outdirTmp) { fr_shppath = file.path(computer_data_path, fr_shpdir, fr_shpname) rv_shppath = file.path(computer_data_path, rv_shpdir, rv_shpname) @@ -1202,6 +1230,8 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s minTrendMean = apply(TrendMean_code, c(1, 2), min, na.rm=TRUE) maxTrendMean = apply(TrendMean_code, c(1, 2), max, na.rm=TRUE) + ncolor = 256 + nbTick = 10 for (i in 1:nbp) { @@ -1233,16 +1263,13 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s color="grey40", fill=NA, size=0.2) map = map + - # scale_x_continuous(expand=c(0, 0)) + - - # scale_y_continuous(expand=c(0, 0)) + - + coord_sf(xlim=c(280000, 790000), ylim=c(6110000, 6600000), expand=FALSE) map = map + - theme(plot.margin=margin(0, 0, 0, 5, unit="mm")) + theme(plot.margin=margin(t=5, r=0, b=5, l=5, unit="mm")) lon = c() lat = c() @@ -1256,20 +1283,9 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s df_data_code = df_data[df_data$code == code,] df_trend_code = df_trend[df_trend$code == code,] - - Imin = 10^99 - for (j in 1:nPeriod_max) { - Start = Start_code[Code_code == code][[1]][j] - End = End_code[Code_code == code][[1]][j] - - I = interval(Start, End) - if (I < Imin) { - idImin = j - } - } - Start = Start_code[Code_code == code][[1]][idImin] - End = End_code[Code_code == code][[1]][idImin] + Start = Start_code[Code_code == code][[1]][idPer] + End = End_code[Code_code == code][[1]][idPer] df_data_code_per = df_data_code[df_data_code$Date >= Start @@ -1289,12 +1305,15 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s if (df_trend_code_per$p <= p_threshold){ color_res = get_color(trendMean, - minTrendMean[idImin, i], - maxTrendMean[idImin, i], + minTrendMean[idPer, i], + maxTrendMean[idPer, i], palette_name='perso', - reverse=TRUE) + reverse=TRUE, + ncolor=ncolor, + nbTick=nbTick) filltmp = color_res$color + palette = color_res$palette } else { filltmp = 'grey97' } @@ -1312,18 +1331,106 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s } - for (i in nCode) { + for (k in nCode) { map = map + geom_point(data=plot_map, aes(x=lon, y=lat), - shape=21, size=3, + shape=21, size=5, stroke=1, color='grey50', fill=fill) } - Map = list(map) + + idTick = color_res$idTick + labTick = color_res$labTick + colTick = color_res$colTick + ncolorShow = length(color_res$palette) + + labTick = as.character(round(labTick*100, 2)) + + xtick = rep(0, times=nbTick) + + plot_palette = tibble(xtick=xtick, ytick=idTick, + colTick=colTick, labTick=labTick) + + + title = ggplot() + theme_void() + + + annotate('text', + x=-0.3, y=0.15, + label=bquote(bold(.(type))), + hjust=0, vjust=0, + size=10, color="#00A3A8") + + + geom_line(aes(x=c(-0.3, 3), y=c(0.05, 0.05)), + size=0.5, color="#00A3A8") + + + scale_x_continuous(limits=c(-1, 1 + 3), + expand=c(0, 0)) + + + scale_y_continuous(limits=c(0, 10), + expand=c(0, 0)) + + + theme(plot.margin=margin(t=5, r=5, b=0, l=0, unit="mm")) + + + pal = ggplot() + theme_void() + + + geom_point(data=plot_palette, + aes(x=xtick, y=ytick), + shape=21, size=5, stroke=1, + color='grey50', fill=colTick) + + for (j in 1:nbTick) { + pal = pal + + annotate('text', x=xtick[j]+0.3, + y=idTick[j], + label=bquote(bold(.(labTick[j]))), + hjust=0, vjust=0.7, + size=3, color='grey40') + } + + pal = pal + + + geom_point(aes(x=0, y=-20), + shape=21, size=5, stroke=1, + color='grey50', fill='grey97') + + + annotate('text', + x=0.3, y=-20, + label=bquote(bold("non significatif à 10%")), + hjust=0, vjust=0.7, + size=3, color='grey40') + + pal = pal + + + annotate('text', + x=-0.3, y=ncolorShow + 29, + label="Tendance", + hjust=0, vjust=0.5, + size=6, color='grey40') + + + annotate('text', + x=-0.2, y=ncolorShow + 16, + label=bquote(bold("% par an")), + hjust=0, vjust=0.5, + size=4, color='grey40') + + pal = pal + + + scale_x_continuous(limits=c(-1, 1 + 3), + expand=c(0, 0)) + + + scale_y_continuous(limits=c(-55, ncolorShow + 45), + expand=c(0, 0)) + + + theme(plot.margin=margin(t=0, r=5, b=5, l=0, unit="mm")) + + + Map = list(map, title, pal) plot = grid.arrange(grobs=Map, layout_matrix= - matrix(c(1, 1, 2, 1, 1, 3), + matrix(c(1, 1, 1, 2, + 1, 1, 1, 3), nrow=2, byrow=TRUE)) # Saving matrix plot @@ -1556,7 +1663,7 @@ cumulative = function (data_bin, df_meta, dyear=10, figdir='', filedir_opt='') { } -get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE) { +get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE, nbTick=10) { if (palette_name == 'perso') { palette = colorRampPalette(c( @@ -1609,11 +1716,32 @@ get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse idNorm = (value - 0) / (max - 0) id = round(idNorm*(ncolor_hot - 1) + 1, 0) color = palette_hot[id] + } + + if (min < 0 & max < 0) { + paletteShow = palette_cold + } else if (min > 0 & max > 0) { + paletteShow = palette_hot + } else { + paletteShow = palette } + + ncolorShow = length(paletteShow) - return(list(color=color, palette=palette)) + idTick = c() + for (i in 1:nbTick) { + id = round((ncolorShow-1)/(nbTick-1)*(i-1)) + 1 + idTick = c(idTick, id) + } + + labTick = seq(min, max, length.out=nbTick) + colTick = paletteShow[idTick] + + return(list(color=color, palette=paletteShow, + idTick=idTick, labTick=labTick, colTick=colTick)) } + void = ggplot() + geom_blank(aes(1,1)) + theme( plot.background = element_blank(), @@ -1630,9 +1758,8 @@ void = ggplot() + geom_blank(aes(1,1)) + ) -palette_tester = function () { +palette_tester = function (n=256) { - n = 300 X = 1:n Y = rep(0, times=n) diff --git a/script.R b/script.R index 32297036ca5b63d598b42c7a680d3bee4a04d25c..fd979cb07746020bbff96f7b140122705cda5f5d 100644 --- a/script.R +++ b/script.R @@ -22,7 +22,7 @@ filedir = ### MANUAL SELECTION ### # Name of the file that will be analysed from the AG directory filename = - # "" + "" # c( # "S2235610_HYDRO_QJM.txt", @@ -32,9 +32,9 @@ filename = # "A2250310_HYDRO_QJM.txt" # ) - c("S4214010_HYDRO_QJM.txt", - "Q6332510_HYDRO_QJM.txt", - "Q7002910_HYDRO_QJM.txt") + # c("S4214010_HYDRO_QJM.txt", + # "Q6332510_HYDRO_QJM.txt", + # "Q7002910_HYDRO_QJM.txt") @@ -44,8 +44,8 @@ AGlistdir = "" AGlistname = - "" - # "Liste-station_RRSE.docx" + # "" + "Liste-station_RRSE.docx" ### NIVALE SELECTION ###