Commit 015d3e20 authored by Heraut Louis's avatar Heraut Louis
Browse files

Graph aes

parent fa1aa7a1
No related merge requests found
Showing with 192 additions and 88 deletions
+192 -88
...@@ -14,7 +14,7 @@ library(RColorBrewer) ...@@ -14,7 +14,7 @@ library(RColorBrewer)
source('plotting/panel.R', encoding='latin1') source('plotting/panel.R', encoding='latin1')
panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', filename_opt='', variable='', df_trend=NULL, p_threshold=0.1, unit2day=365.25, type='', trend_period=NULL, mean_period=NULL, missRect=FALSE, time_header=NULL, info_header=TRUE, time_ratio=2, var_ratio=3) { panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', filename_opt='', variable='', df_trend=NULL, p_threshold=0.1, unit2day=365.25, type='', trend_period=NULL, mean_period=NULL, axis_xlim=NULL, missRect=FALSE, time_header=NULL, info_header=TRUE, time_ratio=2, var_ratio=3) {
outfile = "Panels" outfile = "Panels"
if (filename_opt != '') { if (filename_opt != '') {
...@@ -218,6 +218,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op ...@@ -218,6 +218,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
if (!is.null(time_header)) { if (!is.null(time_header)) {
time_header_code = time_header[time_header$code == code,] time_header_code = time_header[time_header$code == code,]
axis_xlim = c(min(time_header_code$Date),
max(time_header_code$Date))
Htime = time_panel(time_header_code, df_trend_code=NULL, Htime = time_panel(time_header_code, df_trend_code=NULL,
trend_period=trend_period, missRect=TRUE, trend_period=trend_period, missRect=TRUE,
...@@ -241,6 +243,7 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op ...@@ -241,6 +243,7 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
color = c() color = c()
# for (j in 1:nrow(df_trend_code)) { # for (j in 1:nrow(df_trend_code)) {
grey = 85
for (j in 1:nPeriod_max) { for (j in 1:nPeriod_max) {
if (df_trend_code$p[j] <= p_threshold){ if (df_trend_code$p[j] <= p_threshold){
# color_res = get_color(df_trend_code$trend[j], # color_res = get_color(df_trend_code$trend[j],
...@@ -277,7 +280,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op ...@@ -277,7 +280,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
colortmp = color_res$color colortmp = color_res$color
} else { } else {
colortmp = NA colortmp = paste('grey', grey, sep='')
grey = grey - 10
} }
color = append(color, colortmp) color = append(color, colortmp)
...@@ -286,8 +290,9 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op ...@@ -286,8 +290,9 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
p = time_panel(df_data_code, df_trend_code, type=type, p = time_panel(df_data_code, df_trend_code, type=type,
p_threshold=p_threshold, missRect=missRect, p_threshold=p_threshold, missRect=missRect,
trend_period=trend_period, trend_period=trend_period,
mean_period=mean_period, unit2day=unit2day, mean_period=mean_period, axis_xlim=axis_xlim,
last=(i > nbp-nbcol), color=color) unit2day=unit2day, last=(i > nbp-nbcol),
color=color)
P[[i+nbh]] = p P[[i+nbh]] = p
......
...@@ -10,7 +10,7 @@ library(ggh4x) ...@@ -10,7 +10,7 @@ library(ggh4x)
library(RColorBrewer) library(RColorBrewer)
time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missRect=FALSE, unit2day=365.25, trend_period=NULL, mean_period=NULL, last=FALSE, first=FALSE, color=NULL) { time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missRect=FALSE, unit2day=365.25, trend_period=NULL, mean_period=NULL, axis_xlim=NULL, last=FALSE, first=FALSE, color=NULL) {
if (type == 'sqrt(Q)') { if (type == 'sqrt(Q)') {
df_data_code$Qm3s = sqrt(df_data_code$Qm3s) df_data_code$Qm3s = sqrt(df_data_code$Qm3s)
...@@ -141,6 +141,10 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -141,6 +141,10 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
minPer = df_data_code$Date[idMinPer] minPer = df_data_code$Date[idMinPer]
maxPer = df_data_code$Date[idMaxPer] maxPer = df_data_code$Date[idMaxPer]
if (type != 'sqrt(Q)' & type != 'Q') {
maxPer = maxPer + years(1)
}
p = p + p = p +
geom_rect(aes(xmin=minPer, geom_rect(aes(xmin=minPer,
ymin=0, ymin=0,
...@@ -155,7 +159,7 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -155,7 +159,7 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
nPeriod_mean = length(mean_period) nPeriod_mean = length(mean_period)
plot_mean = tibble() plot_mean = tibble()
plot_meanL = tibble() plot_line = tibble()
for (j in 1:nPeriod_mean) { for (j in 1:nPeriod_mean) {
Start_mean = mean_period[[j]][1] Start_mean = mean_period[[j]][1]
End_mean = mean_period[[j]][2] End_mean = mean_period[[j]][2]
...@@ -174,17 +178,35 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -174,17 +178,35 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
xmax = xmax + months(6) xmax = xmax + months(6)
} }
if (type != 'sqrt(Q)' & type != 'Q' & j == nPeriod_mean) {
xmax = xmax + years(1)
}
ymax = mean(df_data_code_per$Qm3s, na.rm=TRUE) ymax = mean(df_data_code_per$Qm3s, na.rm=TRUE)
plot_meantmp = tibble(xmin=xmin, xmax=xmax, plot_meantmp = tibble(xmin=xmin, xmax=xmax,
ymin=0, ymax=ymax, period=j) ymin=0, ymax=ymax, period=j)
plot_mean = bind_rows(plot_mean, plot_meantmp) plot_mean = bind_rows(plot_mean, plot_meantmp)
abs = c(xmin, xmax)
ord = c(ymax, ymax)
plot_linetmp = tibble(abs=abs, ord=ord, period=j)
plot_line = bind_rows(plot_line, plot_linetmp)
} }
p = p + p = p +
geom_rect(data=plot_mean, geom_rect(data=plot_mean,
aes(xmin=xmin, ymin=ymin, aes(xmin=xmin, ymin=ymin,
xmax=xmax, ymax=ymax), xmax=xmax, ymax=ymax),
linetype=0, fill='grey93') linetype=0, fill='grey93')
p = p +
geom_line(data=plot_line,
aes(x=abs, y=ord, group=period),
color='grey85',
size=0.15)
# plot_grid = tibble(abs=as.Date(abs), ord=ord)
for (i in 1:(nPeriod_mean-1)) { for (i in 1:(nPeriod_mean-1)) {
yLim = max(c(plot_mean$ymax[i], plot_mean$ymax[i+1])) yLim = max(c(plot_mean$ymax[i], plot_mean$ymax[i+1]))
...@@ -192,14 +214,19 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -192,14 +214,19 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
plot_lim = tibble(x=c(xLim, xLim), y=c(0, yLim)) plot_lim = tibble(x=c(xLim, xLim), y=c(0, yLim))
p = p + p = p +
geom_line(data=plot_lim, aes(x=x, y=y), geom_line(data=plot_lim, aes(x=x, y=y),
linetype='b', color='grey85') linetype='dashed', size=0.15, color='grey85')
} }
} }
### Grid ### ### Grid ###
xmin = min(df_data_code$Date) if (is.null(axis_xlim)) {
xmax = max(df_data_code$Date) xmin = min(df_data_code$Date)
xmax = max(df_data_code$Date)
} else {
xmin = axis_xlim[1]
xmax = axis_xlim[2]
}
ygrid = seq(0, maxQ*10, dbrk) ygrid = seq(0, maxQ*10, dbrk)
ord = c() ord = c()
abs = c() abs = c()
...@@ -214,7 +241,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -214,7 +241,6 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
color='grey85', color='grey85',
size=0.15) size=0.15)
if (type == 'sqrt(Q)' | type == 'Q') { if (type == 'sqrt(Q)' | type == 'Q') {
p = p + p = p +
geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s), geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s),
...@@ -249,10 +275,8 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -249,10 +275,8 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
nPeriod_trend = max(length(UStart), length(UEnd)) nPeriod_trend = max(length(UStart), length(UEnd))
ltype = c('solid', 'dashed', 'dotted', 'twodash') plot_trend = tibble()
lty = c('solid', '22', 'dotted', 'twodash') leg_trend = tibble()
# ii = 0
for (i in 1:nPeriod_trend) { for (i in 1:nPeriod_trend) {
df_trend_code_per = df_trend_code_per =
...@@ -261,27 +285,25 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -261,27 +285,25 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
iStart = which.min(abs(df_data_code$Date - Start[i])) iStart = which.min(abs(df_data_code$Date - Start[i]))
iEnd = which.min(abs(df_data_code$Date - End[i])) iEnd = which.min(abs(df_data_code$Date - End[i]))
xmin = df_data_code$Date[iStart]
xmax = df_data_code$Date[iEnd]
if (!is.null(axis_xlim)) {
if (xmin < axis_xlim[1]) {
xmin = axis_xlim[1]
}
if (xmax > axis_xlim[2]) {
xmax = axis_xlim[2]
}
}
abs = c(df_data_code$Date[iStart], abs = c(xmin, xmax)
df_data_code$Date[iEnd])
abs_num = as.numeric(abs) / unit2day abs_num = as.numeric(abs) / unit2day
ord = abs_num * df_trend_code_per$trend + ord = abs_num * df_trend_code_per$trend +
df_trend_code_per$intercept df_trend_code_per$intercept
plot_trend = tibble(abs=abs, ord=ord)
plot_trendtmp = tibble(abs=abs, ord=ord, period=i)
if (df_trend_code_per$p <= p_threshold) { plot_trend = bind_rows(plot_trend, plot_trendtmp)
p = p +
geom_line(data=plot_trend, aes(x=abs, y=ord),
color=color[i],
linetype=ltype[i], size=0.7)
colortxt = color[i]
} else {
p = p +
geom_line(data=plot_trend, aes(x=abs, y=ord),
color='grey85', linetype=ltype[i],
size=0.5)
colortxt = 'grey85'
}
codeDate = df_data_code$Date codeDate = df_data_code$Date
codeQ = df_data_code$Qm3s codeQ = df_data_code$Qm3s
...@@ -291,45 +313,122 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR ...@@ -291,45 +313,122 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
dy = gpct(7, codeQ, ref=0) dy = gpct(7, codeQ, ref=0)
y = gpct(100, codeQ, ref=0) - (i-1)*dy y = gpct(100, codeQ, ref=0) - (i-1)*dy
yend = y
xt = xend + gpct(1, codeDate) xt = xend + gpct(1, codeDate)
xminR = x - gpct(1, codeDate)
yminR = y - gpct(4, codeQ, ref=0)
xmaxR = x + gpct(24, codeDate)
ymaxR = y + gpct(5, codeQ, ref=0)
trend = df_trend_code_per$trend trend = df_trend_code_per$trend
power = power = get_power(trend) power = power = get_power(trend)
powerC = as.character(power) powerC = as.character(power)
brk = 10^power brk = 10^power
trendC = as.character(round(trend / brk, 2)) trendC = as.character(round(trend / brk, 2))
label = bquote(bold(.(trendC)~'x'~'10'^{.(powerC)})~'['*m^{3}*'.'*s^{-1}*'.'*an^{-1}*']') leg_trendtmp = tibble(x=x, xend=xend,
y=y, yend=yend,
xt=xt,
trendC=trendC,
powerC=powerC,
xminR=xminR, yminR=yminR,
xmaxR=xmaxR, ymaxR=ymaxR,
period=i)
leg_trend = bind_rows(leg_trend, leg_trendtmp)
}
for (i in 1:nPeriod_trend) {
leg_trend_per = leg_trend[leg_trend$period == i,]
p = p + p = p +
geom_rect(data=leg_trend_per,
aes(xmin=xminR,
ymin=yminR,
xmax=xmaxR,
ymax=ymaxR),
linetype=0, fill='white', alpha=0.5)
}
for (i in 1:nPeriod_trend) {
leg_trend_per = leg_trend[leg_trend$period == i,]
trendC = leg_trend_per$trendC
powerC = leg_trend_per$powerC
label = bquote(bold(.(trendC)~'x'~'10'^{.(powerC)})~'['*m^{3}*'.'*s^{-1}*'.'*an^{-1}*']')
p = p +
annotate("segment", annotate("segment",
x=x, xend=xend, x=leg_trend_per$x, xend=leg_trend_per$xend,
y=y, yend=y, y=leg_trend_per$y, yend=leg_trend_per$yend,
color=colortxt, color=color[i],
lty=lty[i], lwd=1) + linetype='solid',
lwd=1) +
annotate("text",
annotate("text",
label=label, size=3, label=label, size=3,
x=xt, y=y, x=leg_trend_per$xt, y=leg_trend_per$y,
hjust=0, vjust=0.4, hjust=0, vjust=0.4,
color=colortxt) color=color[i])
} }
for (i in 1:nPeriod_trend) {
plot_trend_per = plot_trend[plot_trend$period == i,]
p = p +
geom_line(data=plot_trend_per,
aes(x=abs, y=ord),
color='white',
linetype='solid',
size=1)
}
for (i in 1:nPeriod_trend) {
plot_trend_per = plot_trend[plot_trend$period == i,]
p = p +
geom_line(data=plot_trend_per,
aes(x=abs, y=ord),
color=color[i],
linetype='solid',
size=0.5)
}
} }
p = p + p = p +
ggtitle(bquote(bold(.(type))~~'['*m^{3}*'.'*s^{-1}*']')) + ggtitle(bquote(bold(.(type))~~'['*m^{3}*'.'*s^{-1}*']'))
scale_x_date(date_breaks=paste(as.character(datebreak),
'year', sep=' '),
date_minor_breaks=paste(as.character(dateminbreak),
'year', sep=' '),
guide='axis_minor',
date_labels="%Y",
limits=c(min(df_data_code$Date),
max(df_data_code$Date)),
expand=c(0, 0))
if (is.null(axis_xlim)) {
p = p +
scale_x_date(date_breaks=paste(as.character(datebreak),
'year', sep=' '),
date_minor_breaks=paste(as.character(dateminbreak),
'year', sep=' '),
guide='axis_minor',
date_labels="%Y",
limits=c(min(df_data_code$Date),
max(df_data_code$Date)),
expand=c(0, 0))
} else {
p = p +
scale_x_date(date_breaks=paste(
as.character(datebreak),
'year', sep=' '),
date_minor_breaks=paste(
as.character(dateminbreak),
'year', sep=' '),
guide='axis_minor',
date_labels="%Y",
limits=axis_xlim,
expand=c(0, 0))
}
p = p + p = p +
scale_y_continuous(breaks=seq(0, maxQ*10, dbrk), scale_y_continuous(breaks=seq(0, maxQ*10, dbrk),
limits=c(0, maxQ*1.1), limits=c(0, maxQ*1.1),
...@@ -1014,24 +1113,24 @@ get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse ...@@ -1014,24 +1113,24 @@ get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse
# '#fdb147', # '#fdb147',
# '#fd4659' # '#fd4659'
# '#0f3b57', '#0f3b57',
# '#1d7881', '#1d7881',
# '#80c4a9', '#80c4a9',
# '#e2dac6', #mid '#e2dac6', #mid
# '#fadfad', '#fadfad',
# '#d08363', '#d08363',
# '#7e392f' '#7e392f'
'#193830', # '#193830',
'#2A6863', # '#2A6863',
'#449C93', # '#449C93',
'#7ACEB9', # '#7ACEB9',
'#BCE6DB', # '#BCE6DB',
'#EFE0B0', # '#EFE0B0',
'#D4B86A', # '#D4B86A',
'#B3762A', # '#B3762A',
'#7F4A23', # '#7F4A23',
'#452C1A' # '#452C1A'
))(ncolor) ))(ncolor)
...@@ -1103,24 +1202,24 @@ palette_tester = function () { ...@@ -1103,24 +1202,24 @@ palette_tester = function () {
# '#01665e', # '#01665e',
# '#003c30' # '#003c30'
# '#0f3b57', '#0f3b57',
# '#1d7881', '#1d7881',
# '#80c4a9', '#80c4a9',
# '#e2dac6', #mid '#e2dac6', #mid
# '#fadfad', '#fadfad',
# '#d08363', '#d08363',
# '#7e392f' '#7e392f'
'#193830', # '#193830',
'#2A6863', # '#2A6863',
'#449C93', # '#449C93',
'#7ACEB9', # '#7ACEB9',
'#BCE6DB', # '#BCE6DB',
'#EFE0B0', # '#EFE0B0',
'#D4B86A', # '#D4B86A',
'#B3762A', # '#B3762A',
'#7F4A23', # '#7F4A23',
'#452C1A' # '#452C1A'
))(n) ))(n)
......
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