diff --git a/plotting/panel.R b/plotting/panel.R index 49642ea83de0291048fe7d7d8ede141b298de14f..655f562a9bcd1f658faa8f9599fbc477aaef013a 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -19,14 +19,8 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR maxQ = max(df_data_code$Qm3s, na.rm=TRUE) - if (maxQ > 1) { - power = nchar(as.character(as.integer(maxQ))) - 1 - } else { - dec = gsub('0.', '', as.character(maxQ), fixed=TRUE) - ndec = nchar(dec) - nnum = nchar(as.character(as.numeric(dec))) - power = -(ndec - nnum + 1) - } + power = get_power(maxQ) + dbrk = 10^power df_data_code$Qm3sN = df_data_code$Qm3s / dbrk @@ -190,10 +184,10 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR # if (norm) { # p = p + - # ylab(bquote('débit ['*m^{3}*'.'*s^{-1}*'] x'~10^{.(as.character(power))})) + # ylab(bquote('débit ['*m^{3}*'.'*s^{-1}*'] x'~10^{.(as.character(power))})) # } else { # p = p + - # ylab(expression(paste('débit [', m^{3}, '.', + # ylab(expression(paste('débit [', m^{3}, '.', # s^{-1}, ']', sep=''))) # } @@ -224,7 +218,7 @@ text_panel = function(code, df_meta) { text = paste( "<span style='font-size:18pt'> station <b>", code, "</b></span><br>", "nom : ", df_meta_code$nom, "<br>", - "région hydrographique : ", df_meta_code$region_hydro, "<br>", + "région hydrographique : ", df_meta_code$region_hydro, "<br>", "position : (", df_meta_code$L93X, "; ", df_meta_code$L93Y, ")", "<br>", "surface : ", df_meta_code$surface_km2, " km<sup>2</sup>", sep='') @@ -260,8 +254,15 @@ matrice_panel = function (list_df2plot, df_meta) { # Get all different stations code Code = levels(factor(df_meta$code)) - Type_mat = c() + # Type = vector(mode='list', length=nbp) + # for (i in 1:nbp) { + # Type[[i]] = + # } + + Type_mat = list() Code_mat = c() + Trend_mat = c() + Fill_mat = c() Color_mat = c() for (code in Code) { @@ -271,8 +272,8 @@ matrice_panel = function (list_df2plot, df_meta) { p_threshold = list_df2plot[[i]]$p_threshold type = list_df2plot[[i]]$type - Type_mat[i] = as.character(type) - Code_mat[i] = code + Type_mat = append(Type_mat, type) + Code_mat = append(Code_mat, code) df_trend_code = df_trend[df_trend$code == code,] @@ -283,18 +284,128 @@ matrice_panel = function (list_df2plot, df_meta) { palette_name='perso', reverse=FALSE) - color = color_res$color + trend = df_trend_code$trend + fill = color_res$color + color = 'white' + - } else { + } else { + trend = NA + fill = 'white' color = 'white' + } - Color_mat[i] = color + Trend_mat = append(Trend_mat, trend) + Fill_mat = append(Fill_mat, fill) + Color_mat = append(Color_mat, color) } } - + + X = as.integer(factor(as.character(Type_mat))) + Y = as.integer(factor(Code_mat)) + mat = ggplot() + - geom_tile(aes(x=Type_mat, y=Code_mat, fill=Color_mat)) + + theme( + panel.background=element_rect(fill='white'), + text=element_text(family='sans'), + panel.border=element_blank(), + + panel.grid.major.y=element_blank(), + panel.grid.major.x=element_blank(), + + axis.text.x=element_blank(), + axis.text.y=element_blank(), + + axis.ticks.y=element_blank(), + axis.ticks.x=element_blank(), + + ggh4x.axis.ticks.length.minor=rel(0.5), + axis.ticks.length=unit(1.5, 'mm'), + + plot.title=element_text(size=9, vjust=-3, + hjust=-1E-3, color='grey20'), + + axis.title.x=element_blank(), + axis.title.y=element_blank(), + + axis.line.x=element_blank(), + axis.line.y=element_blank(), + + plot.margin=margin(5, 5, 5, 5, unit="mm"), + ) + + + # geom_point(aes(x=X, y=Y), + # shape=21, fill=Fill_mat, color=Color_mat, + # size=15, stroke=1) + + + for (i in 1:length(X)) { + mat = mat + + gg_circle(r=0.5, xc=X[i], yc=Y[i], fill=Fill_mat[i], color=Color_mat[i]) + } + + + mat = mat + + + coord_fixed() + + + scale_x_continuous(limits=c(min(c(X, Y)) - rel(1.5), + max(c(X, Y)) + rel(0.5)), + expand=c(0, 0)) + + + scale_y_continuous(limits=c(min(c(X, Y)) - rel(0.5), + max(c(X, Y)) + rel(1)), + expand=c(0, 0)) + + # scale_x_continuous(limits=c(min(X)-0.4, max(X)+0.2), + # expand=c(0, 0)) + + + # scale_y_continuous(limits=c(min(Y)-0.2, max(Y)+0.4), + # expand=c(0, 0)) + + for (i in 1:length(Code)) { + mat = mat + + annotate('text', x=-0.5, y=i, + label=Code[i], + hjust=0, vjust=0.5, + size=3.5, color='grey40') + } + + for (i in 1:nbp) { + type = list_df2plot[[i]]$type + mat = mat + + annotate('text', x=i, y=max(Y) + 0.6, + label=bquote(.(type)), + hjust=0.5, vjust=0, + size=3.5, color='grey40') + } + + + for (i in 1:length(Trend_mat)) { + trend = Trend_mat[i] + if (!is.na(trend)) { + power = get_power(trend) + dbrk = 10^power + trendN = round(trend / dbrk, 2) + trendC1 = as.character(trendN) + trendC2 = bquote('x '*10^{.(as.character(power))}) + } else { + trendC1 = '' + trendC2 = '' + } + mat = mat + + annotate('text', x=X[i], y=Y[i], + label=trendC1, + hjust=0.5, vjust=0, + size=3, color='white') + + annotate('text', x=X[i], y=Y[i], + label=trendC2, + hjust=0.5, vjust=1.3, + size=2, color='white') + + } return (mat) } @@ -383,3 +494,26 @@ palette_tester = function () { } # palette_teste() + + +get_power = function (value) { + + if (value > 1) { + power = nchar(as.character(as.integer(value))) - 1 + } else { + dec = gsub('0.', '', as.character(value), fixed=TRUE) + ndec = nchar(dec) + nnum = nchar(as.character(as.numeric(dec))) + power = -(ndec - nnum + 1) + } + + return(power) +} + + +gg_circle = function(r, xc, yc, color="black", fill=NA, ...) { + x = xc + r*cos(seq(0, pi, length.out=100)) + ymax = yc + r*sin(seq(0, pi, length.out=100)) + ymin = yc + r*sin(seq(0, -pi, length.out=100)) + annotate("ribbon", x=x, ymin=ymin, ymax=ymax, color=color, fill=fill, ...) +} diff --git a/processing/analyse.R b/processing/analyse.R index 417719a73bb060ea15f1854f7221fc5eeb1460be..e83f5bdb5e1336dbd7148d768d143ba0c8fac946 100644 --- a/processing/analyse.R +++ b/processing/analyse.R @@ -4,7 +4,7 @@ library(zoo) library(StatsAnalysisTrend) # Sourcing R file -source('processing/format.R') +source('processing/format.R', encoding='latin1') # Compute the time gap by station diff --git a/script.R b/script.R index 93ba19fc7ed0c348a1cf94a887d93b2899ef2e74..b684f2732af24004fdb046ca7ec286c77da90746 100644 --- a/script.R +++ b/script.R @@ -22,9 +22,13 @@ BHfiledir = ## Manual selection ## # Name of the file that will be analysed from the BH directory BHfilename = - # "" - c("S2235610_HYDRO_QJM.txt", "P1712910_HYDRO_QJM.txt", "P0885010_HYDRO_QJM.txt") - # "all" + "" + # c("S2235610_HYDRO_QJM.txt", + # "P1712910_HYDRO_QJM.txt", + # "P0885010_HYDRO_QJM.txt", + # "A1000030_HYDRO_QJM.txt", + # "A2250310_HYDRO_QJM.txt" + # ) ## Or list selection ## # Path to the list file of BH data that will be analysed @@ -32,8 +36,8 @@ BHlistdir = "" BHlistname = - "" - # "Liste-station_RRSE.docx" + # "" + "Liste-station_RRSE.docx" ### NIVALE ### @@ -116,6 +120,12 @@ if (BHlistname != ""){ BHfilename = df_selec[df_selec$ok,]$filename } + +###### +BHfilename = BHfilename[1:10] +###### + + # Extract metadata about selected stations df_meta_BH = extractBH_meta(computer_data_path, BHfiledir, BHfilename) @@ -166,14 +176,29 @@ res_VCN10trend = get_VCN10trend(df_data, df_meta, period) # figdir=figdir, # filename_opt='time') -panels_layout(list(res_QAtrend$data, res_QMNAtrend$data, - res_VCN10trend$data), - layout_matrix=c(1, 2, 3), +# panels_layout(list(res_QAtrend$data, res_QMNAtrend$data, + # res_VCN10trend$data), + # layout_matrix=c(1, 2, 3), + # df_meta=df_meta, + # df_trend=list(res_QAtrend$trend, res_QMNAtrend$trend, + # res_VCN10trend$trend), + # type=list(bquote(Q[A]), bquote(Q[MNA]), bquote(V[CN10])), + # missRect=list(TRUE, TRUE, TRUE), + # period=period, + # info_header=TRUE, + # time_header=df_data, + # header_ratio=2, + # figdir=figdir, + # filename_opt='') + + +panels_layout(list(res_QAtrend$data, res_VCN10trend$data), + layout_matrix=c(1, 2), df_meta=df_meta, - df_trend=list(res_QAtrend$trend, res_QMNAtrend$trend, + df_trend=list(res_QAtrend$trend, res_VCN10trend$trend), - type=list('Q[A]', 'Q[MNA]', 'V[CN10]'), - missRect=list(TRUE, TRUE, TRUE), + type=list(bquote(Q[A]), bquote(V[CN10])), + missRect=list(TRUE, TRUE), period=period, info_header=TRUE, time_header=df_data, @@ -181,6 +206,7 @@ panels_layout(list(res_QAtrend$data, res_QMNAtrend$data, figdir=figdir, filename_opt='') + ### /!\ Removed 185 row(s) containing missing values (geom_path) -> remove NA ###