diff --git a/plotting/datasheet.R b/plotting/datasheet.R index e2c2b797f0182df63a6529c92eecf392f54beea4..61ab26389b859679c93080e6eb568de5fc204519 100644 --- a/plotting/datasheet.R +++ b/plotting/datasheet.R @@ -432,11 +432,11 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti P[[i+nbh]] = p } - Foot = foot_panel(k, nCode, resources_path, + foot = foot_panel('fiche station', k, nCode, resources_path, AEAGlogo_file, INRAElogo_file, FRlogo_file, foot_height) - P[[nbg]] = Foot + P[[nbg]] = foot # Convert the 'layout_matrix' to a matrix if it is not already @@ -481,11 +481,11 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti LM = cbind(rep(99, times=LMrow), LM, rep(99, times=LMrow)) LMcol = ncol(LM) - margin = 0.5 + margin_height = 0.5 height = 29.7 width = 21 - Norm_ratio = height * (info_ratio + time_ratio + var_ratio*nbp) / (height - 2*margin - foot_height) + Norm_ratio = height * (info_ratio + time_ratio + var_ratio*nbp) / (height - 2*margin_height - foot_height) info_height = height * info_ratio / Norm_ratio time_height = height * time_ratio / Norm_ratio @@ -498,13 +498,13 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti heightLM[Hcut == 2] = time_height heightLM[Hcut > 2 & Hcut < id_foot] = var_height heightLM[Hcut == id_foot] = foot_height - heightLM[Hcut == 99] = margin + heightLM[Hcut == 99] = margin_height - col_width = (width - 2*margin) / (LMcol - 2) + col_width = (width - 2*margin_height) / (LMcol - 2) Wcut = LM[(nrow(LM)-1),] widthLM = rep(col_width, times=LMcol) - widthLM[Wcut == 99] = margin + widthLM[Wcut == 99] = margin_height # Plot the graph as the layout plot = grid.arrange(grobs=P, layout_matrix=LM, @@ -1578,71 +1578,3 @@ hydrograph_panel = function (df_data_code, period, margin=NULL) { # Returns the plot return (hyd) } - - -foot_panel = function (n_page, N_page, resources_path, AEAGlogo_file, INRAElogo_file, FRlogo_file, foot_height) { - - text_page = paste( - "fiche station <b>p. ", n_page, "/", N_page, "</b>", - sep='') - - text_date = paste ( - format(Sys.Date(), "%B %Y"), - sep='') - - # Converts all texts to graphical object in the right position - gtext_page = richtext_grob(text_page, - x=1, y=0, - margin=unit(c(t=0, r=0, b=0, l=0), "mm"), - hjust=1, vjust=0.5, - gp=gpar(col="#00A3A8", fontsize=8)) - - gtext_date = richtext_grob(text_date, - x=1, y=0.4, - margin=unit(c(t=0, r=0, b=0, l=0), "mm"), - hjust=1, vjust=0.5, - gp=gpar(col="#00A3A8", fontsize=6)) - - AEAGlogo_path = file.path(resources_path, AEAGlogo_file) - INRAElogo_path = file.path(resources_path, INRAElogo_file) - FRlogo_path = file.path(resources_path, FRlogo_file) - - AEAGlogo_img = readPNG(AEAGlogo_path) - AEAGlogo_grob = rasterGrob(AEAGlogo_img, - width=unit(0.7*foot_height, "cm")) - - INRAElogo_img = readPNG(INRAElogo_path) - INRAElogo_grob = rasterGrob(INRAElogo_img, - y=0.57, - vjust=0.5, - width=unit(1.1*foot_height, "cm")) - - FRlogo_img = readPNG(FRlogo_path) - FRlogo_grob = rasterGrob(FRlogo_img, - x=0, hjust=0, - width=unit(1*foot_height, "cm")) - - P = list(void, - FRlogo_grob, INRAElogo_grob, AEAGlogo_grob, - gtext_page, gtext_date) - - # Creates the matrix layout - LM = matrix(c(1, 2, 3, 4, 5, - 1, 2, 3, 4, 6), - nrow=2, - byrow=TRUE) - - # And sets the relative width of each plot - widths = rep(1, times=ncol(LM)) - widths[2] = 0.18 - widths[3] = 0.25 - widths[4] = 0.2 - - # Arranges all the graphical objetcs - plot = grid.arrange(grobs=P, - layout_matrix=LM, - widths=widths) - - # Return the plot object - return (plot) -} diff --git a/plotting/layout.R b/plotting/layout.R index 458f2f791858de357f438b9fa6de61a3d8a6740f..b382be40746e0d9f92fadec577afa6b35614f6c8 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -261,12 +261,20 @@ datasheet_layout = function (df_data, df_meta, layout_matrix, df_meta, idPer=length(trend_period), df_shapefile=df_shapefile, - outdirTmp=outdirTmp, - margin=margin(t=5, r=0, b=5, l=5, unit="mm")) + foot_note=foot_note, + foot_height=foot_height, + resources_path=resources_path, + AEAGlogo_file=AEAGlogo_file, + INRAElogo_file=INRAElogo_file, + FRlogo_file=FRlogo_file, + outdirTmp=outdirTmp) } # Combine independant pages into one PDF - pdf_combine(input=file.path(outdirTmp, list.files(outdirTmp)), + details = file.info(list.files(outdirTmp, full.names=TRUE)) + details = details[with(details, order(as.POSIXct(mtime))),] + listfile_path = rownames(details) + pdf_combine(input=listfile_path, output=file.path(outdir, outfile)) } @@ -434,6 +442,75 @@ palette_tester = function (n=256) { } +### Foot note panel +foot_panel = function (name, n_page, N_page, resources_path, AEAGlogo_file, INRAElogo_file, FRlogo_file, foot_height) { + + text_page = paste( + name, " <b>p. ", n_page, "/", N_page, "</b>", + sep='') + + text_date = paste ( + format(Sys.Date(), "%B %Y"), + sep='') + + # Converts all texts to graphical object in the right position + gtext_page = richtext_grob(text_page, + x=1, y=0, + margin=unit(c(t=0, r=0, b=0, l=0), "mm"), + hjust=1, vjust=0.5, + gp=gpar(col="#00A3A8", fontsize=8)) + + gtext_date = richtext_grob(text_date, + x=1, y=0.4, + margin=unit(c(t=0, r=0, b=0, l=0), "mm"), + hjust=1, vjust=0.5, + gp=gpar(col="#00A3A8", fontsize=6)) + + AEAGlogo_path = file.path(resources_path, AEAGlogo_file) + INRAElogo_path = file.path(resources_path, INRAElogo_file) + FRlogo_path = file.path(resources_path, FRlogo_file) + + AEAGlogo_img = readPNG(AEAGlogo_path) + AEAGlogo_grob = rasterGrob(AEAGlogo_img, + width=unit(0.7*foot_height, "cm")) + + INRAElogo_img = readPNG(INRAElogo_path) + INRAElogo_grob = rasterGrob(INRAElogo_img, + y=0.57, + vjust=0.5, + width=unit(1.1*foot_height, "cm")) + + FRlogo_img = readPNG(FRlogo_path) + FRlogo_grob = rasterGrob(FRlogo_img, + x=0, hjust=0, + width=unit(1*foot_height, "cm")) + + P = list(void, + FRlogo_grob, INRAElogo_grob, AEAGlogo_grob, + gtext_page, gtext_date) + + # Creates the matrix layout + LM = matrix(c(1, 2, 3, 4, 5, + 1, 2, 3, 4, 6), + nrow=2, + byrow=TRUE) + + # And sets the relative width of each plot + widths = rep(1, times=ncol(LM)) + widths[2] = 0.18 + widths[3] = 0.25 + widths[4] = 0.2 + + # Arranges all the graphical objetcs + plot = grid.arrange(grobs=P, + layout_matrix=LM, + widths=widths) + + # Return the plot object + return (plot) +} + + ## 5. OTHER TOOLS ### 5.1. Number formatting # Returns the power of ten of the scientific expression of a value diff --git a/plotting/map.R b/plotting/map.R index 3c48c4b94218e7c9e3f5783d4c07503916d3e9de..174d1d953642c9c4c05b2ba0e1e18868c4974d0f 100644 --- a/plotting/map.R +++ b/plotting/map.R @@ -28,7 +28,12 @@ ## 1. MAP PANEL # Generates a map plot of the tendancy of a hydrological variable -map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='', codeLight=NULL, margin=NULL, showSea=TRUE, verbose=TRUE) { +map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='', codeLight=NULL, margin=NULL, showSea=TRUE, + foot_note=FALSE, + foot_height=0, resources_path=NULL, + AEAGlogo_file=NULL, INRAElogo_file=NULL, + FRlogo_file=NULL, + verbose=TRUE) { # Extract shapefiles df_france = df_shapefile$france @@ -270,8 +275,8 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' # If the sea needs to be shown if (showSea) { # Leaves space around the France - xlim = c(280000, 790000) - ylim = c(6110000, 6600000) + xlim = c(295000, 790000) + ylim = c(6125000, 6600000) # Otherwise } else { # Leaves minimal space around France @@ -282,7 +287,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' # If there is no specified station code to highlight (mini map) if (is.null(codeLight)) { # Sets a legend scale start - xmin = gpct(7, xlim, shift=TRUE) + xmin = gpct(4, xlim, shift=TRUE) # Sets graduations xint = c(0, 10*1E3, 50*1E3, 100*1E3) # Sets the y postion @@ -521,7 +526,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' # New plot with void theme title = ggplot() + theme_void() + # Plots separation line - geom_line(aes(x=c(-0.3, 3.3), y=c(0.05, 0.05)), + geom_line(aes(x=c(-0.3, 3.7), y=c(0.05, 0.05)), size=0.6, color="#00A3A8") + # Writes title geom_shadowtext(data=tibble(x=-0.3, y=0.2, @@ -532,13 +537,13 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' bg.colour="white", hjust=0, vjust=0, size=10) + # X axis - scale_x_continuous(limits=c(-1, 1 + 3), + scale_x_continuous(limits=c(-0.3, 1 + 3), expand=c(0, 0)) + # Y axis scale_y_continuous(limits=c(0, 10), expand=c(0, 0)) + # Margin - theme(plot.margin=margin(t=5, r=5, b=0, l=0, unit="mm")) + theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) # New plot with void theme pal = ggplot() + theme_void() + @@ -560,7 +565,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' # Name of the colorbar annotate('text', x=-0.3, y= valNorm + 23, - label="Tendance", + label="Tendance observée", hjust=0, vjust=0.5, size=6, color='grey40') + # Unit legend of the colorbar @@ -659,25 +664,6 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' # current cell the number of times it has been counted yTrend = c(yTrend, rep(mids[ii], times=counts[ii])) } - - ## No touch distribution ## - # start_hist = 1.25 - # min_xsep = 0.15 - # min_ysep = 4 - - # xTrend = rep(start_hist, times=length(yTrend)) - - # for (ii in 1:length(yTrend)) { - - # yTrendtmp = yTrend - # yTrendtmp[ii] = 1E99 - - # isinf_ysep = abs(yTrendtmp - yTrend[ii]) < min_ysep - - # if (any(isinf_ysep) & !all(xTrend[which(isinf_ysep)] > start_hist)) { - # xTrend[ii] = max(xTrend[which(isinf_ysep)]) + min_xsep - # } - # } # Makes a tibble to plot the trend distribution plot_trend = tibble(xTrend=xTrend, yTrend=yTrend) @@ -690,40 +676,89 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' # color="grey20", fill="grey20") alpha=0.4) - # If it is a flow variable if (type == 'sévérité') { - pal = pal + - # Arrow to show a worsening of the situation - geom_segment(aes(x=2.7, y=valNorm*0.75, - xend=2.7, yend=valNorm*0.25), - color='grey50', size=0.3, - arrow=arrow(length=unit(2, "mm"))) + - # Text associated to the arrow - annotate('text', - x=2.8, y=valNorm*0.5, - label= "Plus sévère", - angle=90, - hjust=0.5, vjust=1, - size=3, color='grey50') + labelArrow = 'Plus sévère' + } else if (type == 'saisonnalité') { + labelArrow = 'Plus tôt' } + + xArrow = 3.2 + + pal = pal + + # Arrow to show a worsening of the situation + geom_segment(aes(x=xArrow, y=valNorm*0.75, + xend=xArrow, yend=valNorm*0.25), + color='grey50', size=0.3, + arrow=arrow(length=unit(2, "mm"))) + + # Text associated to the arrow + annotate('text', + x=xArrow+0.1, y=valNorm*0.5, + label=labelArrow, + angle=90, + hjust=0.5, vjust=1, + size=3, color='grey50') pal = pal + # X axis of the colorbar - scale_x_continuous(limits=c(-1, 1 + 3), + scale_x_continuous(limits=c(-0.3, 1 + 3), expand=c(0, 0)) + # Y axis of the colorbar scale_y_continuous(limits=c(-60, valNorm + 35), expand=c(0, 0)) + # Margin of the colorbar - theme(plot.margin=margin(t=0, r=5, b=5, l=0, unit="mm")) + theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) + + # If there is a foot note + if (foot_note) { + foot = foot_panel('carte de tendance observée', + i, nbp, resources_path, + AEAGlogo_file, INRAElogo_file, + FRlogo_file, foot_height) + + # Stores the map, the title and the colorbar in a list + P = list(map, title, pal, foot) + LM = matrix(c(1, 1, 1, 2, + 1, 1, 1, 3, + 4, 4, 4, 4), + nrow=3, byrow=TRUE) + } else { + foot_height = 0 + # Stores the map, the title and the colorbar in a list + P = list(map, title, pal) + LM = matrix(c(1, 1, 1, 2, + 1, 1, 1, 3), + nrow=2, byrow=TRUE) + } + + LMcol = ncol(LM) + LMrow = nrow(LM) + + LM = rbind(rep(99, times=LMcol), LM, rep(99, times=LMcol)) + LMrow = nrow(LM) + LM = cbind(rep(99, times=LMrow), LM, rep(99, times=LMrow)) + LMcol = ncol(LM) + + margin_height = 0.5 + height = 21 + width = 29.7 + + row_height = (height - 2*margin_height - foot_height) / (LMrow - 3) + + Hcut = LM[, 2] + heightLM = rep(row_height, times=LMrow) + heightLM[Hcut == 4] = foot_height + heightLM[Hcut == 99] = margin_height + + col_width = (width - 2*margin_height) / (LMcol - 2) + + Wcut = LM[(nrow(LM)-1),] + widthLM = rep(col_width, times=LMcol) + widthLM[Wcut == 99] = margin_height - # Stores the map, the title and the colorbarin a list - Map = list(map, title, pal) # Arranges the graphical object - plot = grid.arrange(grobs=Map, layout_matrix= - matrix(c(1, 1, 1, 2, - 1, 1, 1, 3), - nrow=2, byrow=TRUE)) + plot = grid.arrange(grobs=P, layout_matrix=LM, + heights=heightLM, widths=widthLM) + # If there is no specified station code to highlight (mini map) if (is.null(codeLight)) { @@ -731,7 +766,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' ggsave(plot=plot, path=outdirTmp, filename=paste(outname, '.pdf', sep=''), - width=29.7, height=21, units='cm', dpi=100) + width=width, height=height, units='cm', dpi=100) } } # Returns the map object diff --git a/script.R b/script.R index 83a1108624253c2c2d84b87f8e6030de2c96b0f9..e93ac8c4946b64c6f10a5be5c1d74d73435709f9 100644 --- a/script.R +++ b/script.R @@ -65,7 +65,7 @@ filename = # "O0384010_HYDRO_QJM.txt", # "S4214010_HYDRO_QJM.txt", "Q7002910_HYDRO_QJM.txt" - # "O3035210_HYDRO_QJM.txt", + # "O3035210_HYDRO_QJM.txt" # "O0554010_HYDRO_QJM.txt", # "O1584610_HYDRO_QJM.txt" ) @@ -306,9 +306,9 @@ df_shapefile = ini_shapefile(computer_data_path, ### 4.2. Analysis layout datasheet_layout(toplot=c( - 'datasheet' + 'datasheet', # 'matrix', - # 'map' + 'map' ), df_meta=df_meta,