Commit 66d60608 authored by Heraut Louis's avatar Heraut Louis
Browse files

Matrix

parent 0e2c036f
No related merge requests found
Showing with 251 additions and 126 deletions
+251 -126
...@@ -183,8 +183,6 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op ...@@ -183,8 +183,6 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
matrix(rep(rep(i, times=LMcol), matrix(rep(rep(i, times=LMcol),
times=time_ratio), times=time_ratio),
ncol=LMcol, byrow=TRUE)) ncol=LMcol, byrow=TRUE))
# if (i <= nbh) {
# LM = rbind(LM, rep(i, times=LMcol))
} else { } else {
LM = rbind(LM, LM = rbind(LM,
...@@ -206,13 +204,13 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op ...@@ -206,13 +204,13 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
} }
# mat = matrice_panel(list_df2plot, df_meta) mat = matrice_panel(list_df2plot, df_meta)
# # Saving matrix plot # Saving matrix plot
# ggsave(plot=mat, ggsave(plot=mat,
# path=outdirTmp, path=outdirTmp,
# filename=paste('matrix', '.pdf', sep=''), filename=paste('matrix', '.pdf', sep=''),
# width=21, height=29.7, units='cm', dpi=100) width=21, height=29.7, units='cm', dpi=100)
# PDF combine # PDF combine
pdf_combine(input=file.path(outdirTmp, list.files(outdirTmp)), pdf_combine(input=file.path(outdirTmp, list.files(outdirTmp)),
......
...@@ -173,7 +173,7 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -173,7 +173,7 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
# } # }
ltype = c('solid', 'dashed', 'dotted', 'twodash') ltype = c('solid', 'dashed', 'dotted', 'twodash')
lty = c('solid', '22') lty = c('solid', '22', 'dotted', 'twodash')
ii = 0 ii = 0
for (i in 1:nPeriod) { for (i in 1:nPeriod) {
...@@ -235,12 +235,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -235,12 +235,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
x=xt, y=y, x=xt, y=y,
hjust=0, vjust=0.4, hjust=0, vjust=0.4,
color=color[i]) color=color[i])
# bquote(bold('tendance')~.(format(df_trend_code$trend, scientific=TRUE, digits=3))~'['*m^{3}*'.'*s^{-1}*'.'*an^{-1}*']')
} }
} }
} }
...@@ -248,7 +242,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -248,7 +242,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
p = p + p = p +
ggtitle(bquote(bold(.(type))~~'['*m^{3}*'.'*s^{-1}*']')) + ggtitle(bquote(bold(.(type))~~'['*m^{3}*'.'*s^{-1}*']')) +
# xlab('date') +
scale_x_date(date_breaks=paste(as.character(datebreak), scale_x_date(date_breaks=paste(as.character(datebreak),
'year', sep=' '), 'year', sep=' '),
date_minor_breaks=paste(as.character(dateminbreak), date_minor_breaks=paste(as.character(dateminbreak),
...@@ -345,7 +338,6 @@ text_panel = function(code, df_meta) { ...@@ -345,7 +338,6 @@ text_panel = function(code, df_meta) {
3, 4, 5), 3, 4, 5),
nrow=3, nrow=3,
byrow=TRUE)) byrow=TRUE))
return(gtext_merge) return(gtext_merge)
} }
...@@ -371,103 +363,204 @@ matrice_panel = function (list_df2plot, df_meta) { ...@@ -371,103 +363,204 @@ matrice_panel = function (list_df2plot, df_meta) {
# Get all different stations code # Get all different stations code
Code = levels(factor(df_meta$code)) Code = levels(factor(df_meta$code))
nCode = length(Code)
nPeriod_max = 0
Start_code = vector(mode='list', length=nCode)
End_code = vector(mode='list', length=nCode)
Code_code = vector(mode='list', length=nCode)
Period_code = vector(mode='list', length=nCode)
df_trend = list_df2plot[[1]]$trend
for (j in 1:nCode) {
code = Code[j]
df_trend_code = df_trend[df_trend$code == code,]
Start = df_trend_code$period_start
UStart = levels(factor(Start))
End = df_trend_code$period_end
UEnd = levels(factor(End))
nPeriod = max(length(UStart), length(UEnd))
Periods = vector(mode='list', length=nPeriod)
for (i in 1:nPeriod) {
Periods[[i]] = paste(Start[i], End[i], sep=' / ')
}
Start_code[[j]] = Start
End_code[[j]] = End
Code_code[[j]] = code
Period_code[[j]] = Periods
if (nPeriod > nPeriod_max) {
nPeriod_max = nPeriod
}
}
print(Code_code)
print(Period_code)
######
Period_mat = c() ###
Type_mat = list() Type_mat = list()
Code_mat = c() Code_mat = c()
Trend_mat = c() Trend_mat = c()
Fill_mat = c() Fill_mat = c()
Color_mat = c() Color_mat = c()
for (code in Code) { for (j in 1:nPeriod_max) {
for (i in 1:nbp) { for (code in Code) {
df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold
type = list_df2plot[[i]]$type
Type_mat = append(Type_mat, type) for (i in 1:nbp) {
Code_mat = append(Code_mat, code) df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold
type = list_df2plot[[i]]$type
print(code)
df_trend_code = df_trend[df_trend$code == code,] df_trend_code = df_trend[df_trend$code == code,]
if (df_trend_code$p <= p_threshold){ print(df_trend_code)
color_res = get_color(df_trend_code$trend,
minTrend[i],
maxTrend[i],
palette_name='perso',
reverse=FALSE)
trend = df_trend_code$trend df_trend_code_per =
fill = color_res$color df_trend_code[df_trend_code$period_start == Start[j]
color = 'white' & df_trend_code$period_end == End[j],]
print(Periods[[j]])
print(df_trend_code_per)
} else {
trend = NA
fill = 'white'
color = 'white'
}
Trend_mat = append(Trend_mat, trend) if (df_trend_code_per$p <= p_threshold){
Fill_mat = append(Fill_mat, fill) color_res = get_color(df_trend_code_per$trend,
Color_mat = append(Color_mat, color) minTrend[i],
maxTrend[i],
palette_name='perso',
reverse=FALSE)
fill = color_res$color
color = 'white'
} else {
fill = 'white'
color = 'grey40'
}
trend = df_trend_code_per$trend
Period_mat[[j]] = Periods[[j]]
Type_mat = append(Type_mat, type)
Code_mat = append(Code_mat, code)
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))) height = length(Code)
Y = as.integer(factor(Code_mat)) width = nbp * 2 * nPeriod_max
options(repr.plot.width=X, repr.plot.height=Y) options(repr.plot.width=width, repr.plot.height=height)
mat = ggplot() + mat = ggplot() +
theme( theme(
panel.background=element_rect(fill='white'), panel.background=element_rect(fill='white'),
text=element_text(family='sans'), text=element_text(family='sans'),
panel.border=element_blank(), panel.border=element_blank(),
panel.grid.major.y=element_blank(), panel.grid.major.y=element_blank(),
panel.grid.major.x=element_blank(), panel.grid.major.x=element_blank(),
axis.text.x=element_blank(), axis.text.x=element_blank(),
axis.text.y=element_blank(), axis.text.y=element_blank(),
axis.ticks.y=element_blank(), axis.ticks.y=element_blank(),
axis.ticks.x=element_blank(), axis.ticks.x=element_blank(),
ggh4x.axis.ticks.length.minor=rel(0.5), ggh4x.axis.ticks.length.minor=rel(0.5),
axis.ticks.length=unit(1.5, 'mm'), axis.ticks.length=unit(1.5, 'mm'),
plot.title=element_text(size=9, vjust=-3, plot.title=element_text(size=9, vjust=-3,
hjust=-1E-3, color='grey20'), 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"),
)
X = c(0)
Y = c(0)
for (j in 1:nPeriod_max) {
axis.title.x=element_blank(), per = Periods[[j]]
axis.title.y=element_blank(),
axis.line.x=element_blank(), Type_mat_per = Type_mat[Period_mat == per]
axis.line.y=element_blank(), Code_mat_per = Code_mat[Period_mat == per]
Trend_mat_per = Trend_mat[Period_mat == per]
plot.margin=margin(5, 5, 5, 5, unit="mm"), Fill_mat_per = Fill_mat[Period_mat == per]
) Color_mat_per = Color_mat[Period_mat == per]
for (i in 1:length(X)) { Xtmp = as.integer(factor(as.character(Type_mat_per)))
mat = mat + X = Xtmp + X[length(X)]
gg_circle(r=0.5, xc=X[i], yc=Y[i], fill=Fill_mat[i], color=Color_mat[i])
}
mat = mat + Ytmp = as.integer(factor(Code_mat_per))
Y = Ytmp + Y[length(XY)]
coord_fixed() + for (i in 1:length(X)) {
mat = mat +
gg_circle(r=0.5, xc=X[i], yc=Y[i],
fill=Fill_mat_per[i], color=Color_mat_per[i])
}
scale_x_continuous(limits=c(min(X) - rel(1.5), for (i in 1:nbp) {
max(X) + rel(0.5)), type = list_df2plot[[i]]$type
expand=c(0, 0)) + mat = mat +
annotate('text', x=i, y=max(Y) + 0.6,
label=bquote(.(type)),
hjust=0.5, vjust=0,
size=3.5, color='grey40')
}
scale_y_continuous(limits=c(min(Y) - rel(0.5), for (i in 1:length(Trend_mat_per)) {
max(Y) + rel(1)), trend = Trend_mat_per[i]
expand=c(0, 0)) 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')
}
}
for (i in 1:length(Code)) { for (i in 1:length(Code)) {
mat = mat + mat = mat +
...@@ -477,38 +570,17 @@ matrice_panel = function (list_df2plot, df_meta) { ...@@ -477,38 +570,17 @@ matrice_panel = function (list_df2plot, df_meta) {
size=3.5, color='grey40') size=3.5, color='grey40')
} }
for (i in 1:nbp) { mat = mat +
type = list_df2plot[[i]]$type
mat = mat + coord_fixed() +
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)) { scale_x_continuous(limits=c(1 - rel(1.5),
trend = Trend_mat[i] width + rel(0.5)),
if (!is.na(trend)) { expand=c(0, 0)) +
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')
} scale_y_continuous(limits=c(1 - rel(0.5),
height + rel(1)),
expand=c(0, 0))
return (mat) return (mat)
} }
......
...@@ -138,7 +138,53 @@ get_period = function (per, df_Xtrend, df_XEx, df_Xlist) { ...@@ -138,7 +138,53 @@ get_period = function (per, df_Xtrend, df_XEx, df_Xlist) {
} }
get_QAtrend = function (df_data, period) { # get_QAtrend = function (df_data, period, p_thresold) {
# # AVERAGE ANNUAL FLOW : QA #
# period = as.list(period)
# for (p_thr in p_thresold) {
# Imax = 0
# df_QAtrendB = tibble()
# for (per in period) {
# df_QAlist = prepare(df_data, colnamegroup=c('code'))
# df_QAEx = extract.Var(data.station=df_QAlist,
# funct=mean,
# timestep='year',
# period=per,
# pos.datetime=1,
# na.rm=TRUE)
# df_QAtrend = Estimate.stats(data.extract=df_QAEx)
# I = interval(per[1], per[2])
# if (I > Imax) {
# Imax = I
# df_QAlistB = df_QAlist
# df_QAExB = df_QAEx
# }
# df_QAtrend = get_period(per, df_QAtrend, df_QAEx, df_QAlist)
# df_QAtrendB = bind_rows(df_QAtrendB, df_QAtrend)
# }
# }
# res_QAtrend = clean(df_QAtrendB, df_QAExB, df_QAlistB)
# return (res_QAtrend)
# }
get_QAtrend = function (df_data, period, p_thresold) {
# AVERAGE ANNUAL FLOW : QA # # AVERAGE ANNUAL FLOW : QA #
period = as.list(period) period = as.list(period)
...@@ -167,10 +213,12 @@ get_QAtrend = function (df_data, period) { ...@@ -167,10 +213,12 @@ get_QAtrend = function (df_data, period) {
} }
df_QAtrend = get_period(per, df_QAtrend, df_QAEx, df_QAlist) df_QAtrend = get_period(per, df_QAtrend, df_QAEx, df_QAlist)
df_QAtrendB = bind_rows(df_QAtrendB, df_QAtrend) df_QAtrendB = bind_rows(df_QAtrendB, df_QAtrend)
} }
res_QAtrend = clean(df_QAtrendB, df_QAExB, df_QAlistB) res_QAtrend = clean(df_QAtrendB, df_QAExB, df_QAlistB)
...@@ -178,7 +226,9 @@ get_QAtrend = function (df_data, period) { ...@@ -178,7 +226,9 @@ get_QAtrend = function (df_data, period) {
} }
get_QMNAtrend = function (df_data, period) {
get_QMNAtrend = function (df_data, period, p_thresold) {
# MONTHLY MINIMUM FLOW IN THE YEAR : QMNA # # MONTHLY MINIMUM FLOW IN THE YEAR : QMNA #
period = as.list(period) period = as.list(period)
...@@ -228,7 +278,7 @@ get_QMNAtrend = function (df_data, period) { ...@@ -228,7 +278,7 @@ get_QMNAtrend = function (df_data, period) {
} }
get_VCN10trend = function (df_data, df_meta, period) { get_VCN10trend = function (df_data, df_meta, period, p_thresold) {
# MINIMUM 10 DAY AVERAGE FLOW OVER THE YEAR : VCN10 # # MINIMUM 10 DAY AVERAGE FLOW OVER THE YEAR : VCN10 #
# Get all different stations code # Get all different stations code
......
...@@ -63,6 +63,10 @@ NVlistname = ...@@ -63,6 +63,10 @@ NVlistname =
# Time period to analyse # Time period to analyse
period_all = c("1800-01-01", "2019-12-31") period_all = c("1800-01-01", "2019-12-31")
period2 = c("1968-01-01", "2019-12-31") period2 = c("1968-01-01", "2019-12-31")
period = list(period_all, period2)
# p value
p_thresold = 0.1 #c(0.01, 0.05, 0.1)
######################## ########################
...@@ -169,14 +173,15 @@ df_meta = df_join$meta ...@@ -169,14 +173,15 @@ df_meta = df_join$meta
# QA TREND # # QA TREND #
res_QAtrend = get_QAtrend(df_data, period=list(period_all, period2)) res_QAtrend = get_QAtrend(df_data, period=period, p_thresold=p_thresold)
# QMNA TREND # # QMNA TREND #
# res_QMNAtrend = get_QMNAtrend(df_data, period=list(period_all, period2)) # res_QMNAtrend = get_QMNAtrend(df_data, period=period,
# p_thresold=p_thresold)
# VCN10 TREND # # VCN10 TREND #
res_VCN10trend = get_VCN10trend(df_data, df_meta, res_VCN10trend = get_VCN10trend(df_data, df_meta,
period=list(period_all, period2)) period=period, p_thresold=p_thresold)
# TIME PANEL # # TIME PANEL #
...@@ -200,7 +205,7 @@ res_VCN10trend = get_VCN10trend(df_data, df_meta, ...@@ -200,7 +205,7 @@ res_VCN10trend = get_VCN10trend(df_data, df_meta,
# res_VCN10trend$trend), # res_VCN10trend$trend),
# type=list(bquote(Q[A]), bquote(Q[MNA]), bquote(V[CN10])), # type=list(bquote(Q[A]), bquote(Q[MNA]), bquote(V[CN10])),
# missRect=list(TRUE, TRUE, TRUE), # missRect=list(TRUE, TRUE, TRUE),
# period=list(period_all, period2), # period=period,
# info_header=TRUE, # info_header=TRUE,
# time_header=df_data, # time_header=df_data,
# time_ratio=2, # time_ratio=2,
...@@ -216,7 +221,7 @@ panels_layout(list(res_QAtrend$data, res_VCN10trend$data), ...@@ -216,7 +221,7 @@ panels_layout(list(res_QAtrend$data, res_VCN10trend$data),
res_VCN10trend$trend), res_VCN10trend$trend),
type=list(bquote(Q[A]), bquote(V[CN10])), type=list(bquote(Q[A]), bquote(V[CN10])),
missRect=list(TRUE, TRUE), missRect=list(TRUE, TRUE),
period=list(period_all, period2), period=period,
info_header=TRUE, info_header=TRUE,
time_header=df_data, time_header=df_data,
time_ratio=2, time_ratio=2,
...@@ -232,7 +237,7 @@ panels_layout(list(res_QAtrend$data, res_VCN10trend$data), ...@@ -232,7 +237,7 @@ panels_layout(list(res_QAtrend$data, res_VCN10trend$data),
# df_trend=list(res_QAtrend$trend), # df_trend=list(res_QAtrend$trend),
# type=list(bquote(Q[A])), # type=list(bquote(Q[A])),
# missRect=list(TRUE), # missRect=list(TRUE),
# period=list(period_all, period2), # period=period,
# info_header=TRUE, # info_header=TRUE,
# time_header=df_data, # time_header=df_data,
# time_ratio=2, # time_ratio=2,
......
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