diff --git a/plotting/layout.R b/plotting/layout.R index 0e5fa154acdda56d3cf89866330c4206e77ddc1b..69345c84c841e05fcc9c55947127320ce14b9da9 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -211,6 +211,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op P = vector(mode='list', length=nbg) if (info_header) { + time_header_code = time_header[time_header$code == code,] + Hinfo = info_panel(list_df2plot, df_meta, computer_data_path=computer_data_path, @@ -220,7 +222,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op bs_shpname=bs_shpname, rv_shpdir=rv_shpdir, rv_shpname=rv_shpname, - codeLight=code) + codeLight=code, + df_data_code=time_header_code) P[[1]] = Hinfo } @@ -400,7 +403,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op bs_shpname=bs_shpname, rv_shpdir=rv_shpdir, rv_shpname=rv_shpname, - outdirTmp=outdirTmp) + outdirTmp=outdirTmp, + margin=margin(t=5, r=0, b=5, l=5, unit="mm")) # PDF combine diff --git a/plotting/panel.R b/plotting/panel.R index aaabd49af2f81960c4e1fd683eb43807f9c269a4..995642ec7f015e4339744f22f203e246902b20c9 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -123,7 +123,7 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR } # Open new plot - p = ggplot() + theme_ash + p = ggplot() + theme_ash # If it is the lats plot of the pages or not if (last) { @@ -1101,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_shpname, rv_shpdir, rv_shpname, idPer=1, outdirTmp='', codeLight=NULL, margin=TRUE) { +map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_shpname, bs_shpdir, bs_shpname, rv_shpdir, rv_shpname, idPer=1, outdirTmp='', codeLight=NULL, margin=NULL, showSea=TRUE) { fr_shppath = file.path(computer_data_path, fr_shpdir, fr_shpname) rv_shppath = file.path(computer_data_path, rv_shpdir, rv_shpname) @@ -1244,14 +1244,10 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s type = list_df2plot[[i]]$type - map = ggplot() + theme_ash + + map = ggplot() + theme_void() + - theme( - # panel.border=element_blank(), - axis.text.x=element_blank(), - axis.text.y=element_blank(), - axis.ticks.x=element_blank(), - axis.ticks.y=element_blank()) + + # theme(plot.background=element_rect(fill=NA, + # color="#EC4899")) + coord_fixed() + @@ -1271,9 +1267,13 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s aes(x=long, y=lat, group=group), color="grey40", fill=NA, size=0.2) - xlim = c(280000, 790000) - ylim = c(6110000, 6600000) - + if (showSea) { + xlim = c(280000, 790000) + ylim = c(6110000, 6600000) + } else { + xlim = c(305000, 790000) + ylim = c(6135000, 6600000) + } if (is.null(codeLight)) { xmin = gpct(7, xlim, shift=TRUE) @@ -1306,13 +1306,13 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s map = map + coord_sf(xlim=xlim, ylim=ylim, expand=FALSE) - - if (margin) { + + if (is.null(margin)) { map = map + - theme(plot.margin=margin(t=5, r=0, b=5, l=5, unit="mm")) + theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) } else { map = map + - theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) + theme(plot.margin=margin) } lon = c() @@ -1401,13 +1401,13 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s geom_point(data=plot_map_codeNo, aes(x=lon, y=lat), - shape=21, size=1, stroke=1, - color='grey70', fill='grey70') + + shape=21, size=0.5, stroke=0.5, + color='grey70', fill='grey97') + geom_point(data=plot_map_code, aes(x=lon, y=lat), - shape=21, size=2, stroke=1, - color='#00A3A8', fill='#00A3A8') + shape=21, size=1.5, stroke=0.5, + color='grey70', fill='grey70') } idTick = color_res$idTick @@ -1543,11 +1543,71 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s } +hydrogramme = function (df_data_code, margin=NULL) { + + monthData = as.numeric(format(df_data_code$Date, "%m")) + + monthMean = c() + for (i in 1:12) { + data = df_data_code$Qm3s[monthData == i] + monthMean[i] = mean(data, na.rm=TRUE) + } + + monthNum = 1:12 + monthName = c("jan", "fév", "mar", "avr", "mai", "jun", + "jul", "aoû", "sep", "oct", "nov", "déc") + monthName = factor(monthName, levels=monthName) + + hyd = ggplot() + theme_ash + + theme( + # plot.background=element_rect(fill=NA, color="#EC4899"), + panel.border=element_blank(), + axis.text.x=element_text(margin=unit(c(0, 0, 0, 0), "mm"), + angle=90, vjust=0.5, hjust=1), + axis.ticks.x=element_blank(), + axis.line.y=element_line(color='grey85', size=0.3)) + + if (is.null(margin)) { + hyd = hyd + + theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) + } else { + hyd = hyd + + theme(plot.margin=margin) + } + + hyd = hyd + + + geom_bar(aes(x=monthNum, y=monthMean), + stat='identity', + fill="grey70", + width=0.75, size=0.2) + + + scale_x_continuous(breaks=monthNum, + labels=monthName, + limits=c(0, max(monthNum)+0.5), + expand=c(0, 0)) + + + scale_y_continuous(limits=c(0, max(monthMean)), + expand=c(0, 0)) + + return (hyd) +} + + +info_panel = function(list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_shpname, bs_shpdir, bs_shpname, rv_shpdir, rv_shpname, codeLight, df_data_code=NULL) { + if (!is.null(df_data_code)) { + hyd = hydrogramme(df_data_code, + margin=margin(t=3, r=0, b=0, l=5, unit="mm")) + } else { + hyd = void + } -info_panel = function(list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_shpname, bs_shpdir, bs_shpname, rv_shpdir, rv_shpname, codeLight) { + # yearLast = format(databin$Date[nrow(databin)], "%Y") + # yearFirst = format(databin$Date[1], "%Y") + # Nyear = yearLast - yearFirst + 1 - map = map_panel(list_df2plot, + map = map_panel(list_df2plot, df_meta, computer_data_path=computer_data_path, fr_shpdir=fr_shpdir, @@ -1557,18 +1617,22 @@ info_panel = function(list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s rv_shpdir=rv_shpdir, rv_shpname=rv_shpname, codeLight=codeLight, - margin=FALSE) + margin=margin(t=5, r=2, b=0, l=0, unit="mm"), + showSea=FALSE) df_meta_code = df_meta[df_meta$code == codeLight,] + nom = df_meta_code$nom + nom = gsub("-", "- ", nom) + text1 = paste( - "<b>", codeLight, '</b> - ', df_meta_code$nom, ' (', - df_meta_code$region_hydro, ')', + "<b>", codeLight, '</b> - ', nom, sep='') text2 = paste( "<b>", - "Gestionnaire : ", df_meta_code$gestionnaire, "<br>", + "Gestionnaire : ", df_meta_code$gestionnaire, "<br>", + "Région hydro : ", df_meta_code$region_hydro, "</b>", sep='') @@ -1588,38 +1652,46 @@ info_panel = function(list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s gtext1 = richtext_grob(text1, x=0, y=1, - margin=unit(c(t=5, r=5, b=0, l=5), "mm"), + margin=unit(c(t=5, r=5, b=10, l=5), "mm"), hjust=0, vjust=1, gp=gpar(col="#00A3A8", fontsize=14)) - + gtext2 = richtext_grob(text2, x=0, y=0.55, - margin=unit(c(t=0, r=5, b=0, l=5), "mm"), + margin=unit(c(t=0, r=0, b=0, l=5), "mm"), hjust=0, vjust=1, gp=gpar(col="grey20", fontsize=8)) gtext3 = richtext_grob(text3, x=0, y=1, - margin=unit(c(t=0, r=5, b=5, l=5), "mm"), + margin=unit(c(t=0, r=0, b=0, l=5), "mm"), hjust=0, vjust=1, gp=gpar(col="grey20", fontsize=9)) gtext4 = richtext_grob(text4, x=0, y=1, - margin=unit(c(t=0, r=5, b=5, l=5), "mm"), + margin=unit(c(t=0, r=0, b=0, l=0), "mm"), hjust=0, vjust=1, gp=gpar(col="grey20", fontsize=9)) - P = list(gtext1, gtext2, gtext3, gtext4, map) + + void = void + + theme(plot.background=element_rect(fill=NA, color="#EC4899"), + plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) + + P = list(gtext1, gtext2, gtext3, gtext4, hyd, map) + # P = list(void, void, void, void, void, void) + plot = grid.arrange(grobs=P, - layout_matrix=matrix(c(1, 1, 1, - 2, 2, 5, - 2, 2, 5, - 3, 4, 5, - 3, 4, 5), + layout_matrix=matrix(c(1, 1, 1, 6, + 2, 2, 5, 6, + 2, 2, 5, 6, + 3, 4, 5, 6, + 3, 4, 5, 6), nrow=5, byrow=TRUE)) + return(plot) } diff --git a/script.R b/script.R index 210bcd281ba49889e79daa16d143a7165d043612..2ca28638ed3ad8027203eca4d6f086f8d724c6a5 100644 --- a/script.R +++ b/script.R @@ -264,7 +264,7 @@ panels_layout(list(res_QAtrend$data, res_QMNAtrend$data, # map_panel(computer_data_path, # fr_shpdir, fr_shpname, # bs_shpdir, bs_shpname, - # rv_shpdir, rv_shpname) + # rv_shpdir, rv_s hpname) ### /!\ Removed 185 row(s) containing missing values (geom_path) -> remove NA ###