Commit 426d5392 authored by Heraut Louis's avatar Heraut Louis
Browse files

Plotting

parent 47a0d3dc
No related merge requests found
Showing with 75 additions and 28 deletions
+75 -28
# Usefull library # Usefull library
library(ggplot2) library(ggplot2)
library(qpdf)
library(gridExtra) library(gridExtra)
width = 30
height = 14
dpi = 100
# Time panel # Time panel
panel = function (df_data, df_info, figdir, filedir, is_sqrt=FALSE) { panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', filename_opt='', variable='', df_trend=NULL, unit2day=365.25, is_sqrt=FALSE) {
outfile = "Panels" outfile = "Panels"
if (filename_opt != '') {
outfile = paste(outfile, '_', filename_opt, sep='')
}
if (is_sqrt) { if (is_sqrt) {
df_data[, 'Qm3s'] = apply(df_data[, 'Qm3s'], 1, sqrt) df_data[, 'Qm3s'] = apply(df_data[, 'Qm3s'], 1, sqrt)
outfile = paste(outfile, '_sqrt', sep='') outfile = paste(outfile, '_sqrt', sep='')
} }
outfile = paste(outfile, '.pdf', sep='') outfile = paste(outfile, '.pdf', sep='')
# If there is not a dedicated figure directory it creats one # If there is not a dedicated figure directory it creats one
outdir = file.path(figdir, filedir, sep='') outdir = file.path(figdir, filedir_opt, sep='')
if (!(file.exists(outdir))) { if (!(file.exists(outdir))) {
dir.create(outdir) dir.create(outdir)
} }
outdirTmp = file.path(outdir, 'tmp')
if (!(file.exists(outdirTmp))) {
dir.create(outdirTmp)
}
# Get all different stations code # Get all different stations code
Code = levels(factor(df_info$code)) Code = levels(factor(df_meta$code))
# Create a blank list in order to store plots
plots = list()
for (code in Code) { for (code in Code) {
# Print code of the station for the current plotting # Print code of the station for the current plotting
print(paste("Plotting for sation :", code)) print(paste("Plotting for sation :", code))
df_data_code = df_data[df_data$code == code,]
dDate = df_data_code$Date[length(df_data_code$Date)] -
df_data_code$Date[1]
datebreak = round(as.numeric(dDate) / unit2day / 12 , 0)
df_data_code = df_data[df_data$code==code,] p = ggplot() +
# df_data_code_NoNA = df_data_code[!is.na(df_data_code$Qm3s),] geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s),
color='black')
# Plot
p = ggplot(df_data_code, aes(x=Date, y=Qm3s)) + if (!is.null(df_trend)) {
geom_line() if (df_trend[df_trend$code == code,]$p < p_threshold) {
abs = c(df_data_code$Date[1],
df_data_code$Date[length(df_data_code$Date)])
# Store abs_num = as.numeric(abs)/unit2day
plots = append(plots, list(p))
ord = abs_num * df_trend$trend[df_trend$code == code] +
df_trend$intercept[df_trend$code == code]
p = p +
geom_line(aes(x=abs, y=ord),
color='cornflowerblue')
}}
} p = p +
ggtitle(paste(variable, 'station',
as.character(code), sep=' ')) +
ylab(expression(paste('débit [', m^{3}, '.',
s^{-1}, ']', sep=''))) +
xlab('date') +
scale_x_date(date_breaks=paste(as.character(datebreak), 'year', sep=' '), date_labels="%Y")
# Saving # Saving
ggsave(path=outdir, ggsave(path=outdirTmp,
filename=outfile, filename=paste(as.character(code), '.pdf', sep=''),
plot=marrangeGrob(plots, nrow=1, ncol=1), width=29.7, height=21, units='cm', dpi=100)
width=29.7, height=21, units='cm', dpi=100
)
}
# print(list.files(outdirTmp))
print(file.path(outdir, outfile))
pdf_combine(input=file.path(outdirTmp, list.files(outdirTmp)),
output=file.path(outdir, outfile))
unlink(outdirTmp, recursive=TRUE)
} }
......
...@@ -83,6 +83,13 @@ clean = function (df_Xtrend, df_XEx, df_Xlist) { ...@@ -83,6 +83,13 @@ clean = function (df_Xtrend, df_XEx, df_Xlist) {
df_Xlist = reprepare(df_XEx, df_Xlist, colnamegroup=c('code')) df_Xlist = reprepare(df_XEx, df_Xlist, colnamegroup=c('code'))
df_Xlist$data$code = NA
for (g in df_Xlist$info$group) {
df_Xlist$data$code[which(df_Xlist$data$group == g)] = df_Xlist$info$code[df_Xlist$info$group == g]
}
# df_Xlist$data = df_Xlist$data[, !names(df_Xlist$data) == "group")]
df_Xtrend = bind_cols(df_Xtrend, df_Xtrend = bind_cols(df_Xtrend,
df_Xlist$info[df_Xtrend$group1, df_Xlist$info[df_Xtrend$group1,
2:ncol(df_Xlist$info)]) 2:ncol(df_Xlist$info)])
......
...@@ -23,7 +23,7 @@ BHfiledir = ...@@ -23,7 +23,7 @@ BHfiledir =
# Name of the file that will be analysed from the BH directory # Name of the file that will be analysed from the BH directory
BHfilename = BHfilename =
# "" # ""
c("H5920011_HYDRO_QJM.txt")#, "K4470010_HYDRO_QJM.txt") c("H5920011_HYDRO_QJM.txt", "K4470010_HYDRO_QJM.txt")
# "all" # "all"
## Or list selection ## ## Or list selection ##
...@@ -151,9 +151,18 @@ df_meta = df_join$meta ...@@ -151,9 +151,18 @@ df_meta = df_join$meta
# QA TREND # # QA TREND #
res_QAtrend = get_QAtrend(df_data, period) res_QAtrend = get_QAtrend(df_data, period)
panel(df_data=res_QAtrend$data,
df_meta=df_meta,
df_trend=res_QAtrend$trend,
p_threshold=0.1,
figdir=figdir, filedir_opt='QA', variable='QA')
# QMNA TREND # # QMNA TREND #
# res_QMNAtrend = get_QMNAtrend(df_data, period) # res_QMNAtrend = get_QMNAtrend(df_data, period)
# VCN10 TREND # # VCN10 TREND #
res_VCN10trend = get_VCN10trend(df_data, df_meta, period) # res_VCN10trend = get_VCN10trend(df_data, df_meta, period)
# panel(df_data=res_VCN10trend$data,
# df_meta=df_meta,
# df_trend=res_VCN10trend$trend,
# figdir=figdir, filedir="VCN10")
...@@ -7,6 +7,7 @@ install.packages("ggplot2") ...@@ -7,6 +7,7 @@ install.packages("ggplot2")
install.packages("officer") install.packages("officer")
install.packages("lubridate") install.packages("lubridate")
install.packages('zoo') install.packages('zoo')
install.packages("qpdf")
library(devtools) library(devtools)
install_github("https://github.com/benRenard/BFunk") #type '1' install_github("https://github.com/benRenard/BFunk") #type '1'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment