Commit d15e12b3 authored by Heraut Louis's avatar Heraut Louis
Browse files

Reform code

parent 709cea16
No related merge requests found
Showing with 269 additions and 2951 deletions
+269 -2951
...@@ -36,12 +36,65 @@ library(dplyr) ...@@ -36,12 +36,65 @@ library(dplyr)
library(grid) library(grid)
library(ggh4x) library(ggh4x)
library(RColorBrewer) library(RColorBrewer)
library(rgdal)
library(shadowtext)
# Sourcing R file # Sourcing R file
source('plotting/panel.R', encoding='latin1') source('plotting/time.R', encoding='latin1')
source('plotting/info.R', encoding='latin1')
source('plotting/map.R', encoding='latin1')
panels_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet', 'matrix', 'map'), 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, info_ratio=1, time_ratio=2, var_ratio=3, df_shapefile=NULL) { source('plotting/matrix.R', encoding='latin1')
source('plotting/other.R', encoding='latin1')
## 1. PERSONALISATION
### 1.1. Personal theme
theme_ash =
theme(
# White background
panel.background=element_rect(fill='white'),
# Font
text=element_text(family='sans'),
# Border of plot
panel.border = element_rect(color="grey85",
fill=NA,
size=0.7),
# Grid
panel.grid.major.x=element_blank(),
panel.grid.major.y=element_blank(),
# Ticks marker
axis.ticks.x=element_line(color='grey75', size=0.3),
axis.ticks.y=element_line(color='grey75', size=0.3),
# Ticks label
axis.text.x=element_text(color='grey40'),
axis.text.y=element_text(color='grey40'),
# Ticks length
axis.ticks.length=unit(1.5, 'mm'),
# Ticks minor
ggh4x.axis.ticks.length.minor=rel(0.5),
# Title
plot.title=element_text(size=9, vjust=-2,
hjust=-1E-3, color='grey20'),
# Axis title
axis.title.x=element_blank(),
axis.title.y=element_blank(),
# Axis line
axis.line.x=element_blank(),
axis.line.y=element_blank(),
)
### 1.2. Color palette
palette_perso = c('#0f3b57',
'#1d7881',
'#80c4a9',
'#e2dac6', #mid
'#fadfad',
'#d08363',
'#7e392f')
## 2. LAYOUT
datasheet_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet', 'matrix', 'map'), 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, info_ratio=1, time_ratio=2, var_ratio=3, df_shapefile=NULL) {
outfile = "Panels" outfile = "Panels"
if (filename_opt != '') { if (filename_opt != '') {
...@@ -99,34 +152,6 @@ panels_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet', ...@@ -99,34 +152,6 @@ panels_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet',
missRect = replicate(nbp, missRect) missRect = replicate(nbp, missRect)
}} }}
# Get all different stations code
Code = levels(factor(df_meta$code))
nCode = length(Code)
# print(df_trend)
df_trendtmp = df_trend[[1]]
# print(df_trendtmp)
nPeriod_max = 0
for (code in Code) {
df_trend_code = df_trendtmp[df_trendtmp$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))
if (nPeriod > nPeriod_max) {
nPeriod_max = nPeriod
}
}
list_df2plot = vector(mode='list', length=nbp) list_df2plot = vector(mode='list', length=nbp)
# minTrend = c() # minTrend = c()
# maxTrend = c() # maxTrend = c()
...@@ -150,279 +175,15 @@ panels_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet', ...@@ -150,279 +175,15 @@ panels_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet',
if ('datasheet' %in% isplot) { if ('datasheet' %in% isplot) {
time_panel(list_df2plot, df_meta, trend_period, info_header=info_header, time_header=time_header, layout_matrix=layout_matrix, info_ratio=info_ratio, time_ratio=time_ratio, var_ratio=var_ratio, outdirTmp=outdirTmp)
Start_code = vector(mode='list', length=nCode)
End_code = vector(mode='list', length=nCode)
Code_code = vector(mode='list', length=nCode)
Periods_code = vector(mode='list', length=nCode)
for (j in 1:nCode) {
code = Code[j]
df_trend_code = df_trendtmp[df_trendtmp$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 = c()
for (i in 1:nPeriod_max) {
Periods = append(Periods,
paste(substr(Start[i], 1, 4),
substr(End[i], 1, 4),
sep=' / '))
}
Start_code[[j]] = Start
End_code[[j]] = End
Code_code[[j]] = code
Periods_code[[j]] = Periods
}
TrendMean_code = array(rep(1, nPeriod_max*nbp*nCode),
dim=c(nPeriod_max, nbp, nCode))
for (j in 1:nPeriod_max) {
for (k in 1:nCode) {
code = Code[k]
for (i in 1:nbp) {
df_data = list_df2plot[[i]]$data
df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold
df_data_code = df_data[df_data$code == code,]
df_trend_code = df_trend[df_trend$code == code,]
Start = Start_code[Code_code == code][[1]][j]
End = End_code[Code_code == code][[1]][j]
Periods = Periods_code[Code_code == code][[1]][j]
df_data_code_per =
df_data_code[df_data_code$Date >= Start
& df_data_code$Date <= End,]
df_trend_code_per =
df_trend_code[df_trend_code$period_start == Start
& df_trend_code$period_end == End,]
Ntrend = nrow(df_trend_code_per)
if (Ntrend > 1) {
df_trend_code_per = df_trend_code_per[1,]
}
dataMean = mean(df_data_code_per$Qm3s, na.rm=TRUE)
trendMean = df_trend_code_per$trend / dataMean
if (df_trend_code_per$p <= p_threshold){
TrendMean_code[j, i, k] = trendMean
} else {
TrendMean_code[j, i, k] = NA
}
}
}
}
minTrendMean = apply(TrendMean_code, c(1, 2), min, na.rm=TRUE)
maxTrendMean = apply(TrendMean_code, c(1, 2), max, na.rm=TRUE)
for (code in Code) {
# Print code of the station for the current plotting
print(paste("Datasheet for station :", code))
nbh = as.numeric(info_header) + as.numeric(!is.null(time_header))
nbg = nbp + nbh
P = vector(mode='list', length=nbg)
if (info_header) {
time_header_code = time_header[time_header$code == code,]
Hinfo = info_panel(list_df2plot,
df_meta,
df_shapefile=df_shapefile,
codeLight=code,
df_data_code=time_header_code)
P[[1]] = Hinfo
# P[[1]] = void
}
if (!is.null(time_header)) {
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,
trend_period=trend_period, missRect=TRUE,
unit2day=365.25, type='Q', grid=TRUE, first=FALSE)
P[[2]] = Htime
}
# map = map_panel()
nbcol = ncol(as.matrix(layout_matrix))
for (i in 1:nbp) {
df_data = list_df2plot[[i]]$data
df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold
unit2day = list_df2plot[[i]]$unit2day
missRect = list_df2plot[[i]]$missRect
type = list_df2plot[[i]]$type
df_data_code = df_data[df_data$code == code,]
df_trend_code = df_trend[df_trend$code == code,]
color = c()
# for (j in 1:nrow(df_trend_code)) {
grey = 85
for (j in 1:nPeriod_max) {
if (df_trend_code$p[j] <= p_threshold){
# color_res = get_color(df_trend_code$trend[j],
# minTrend[i],
# maxTrend[i],
# palette_name='perso',
# reverse=TRUE)
Start = Start_code[Code_code == code][[1]][j]
End = End_code[Code_code == code][[1]][j]
Periods = Periods_code[Code_code == code][[1]][j]
df_data_code_per =
df_data_code[df_data_code$Date >= Start
& df_data_code$Date <= End,]
df_trend_code_per =
df_trend_code[df_trend_code$period_start == Start
& df_trend_code$period_end == End,]
Ntrend = nrow(df_trend_code_per)
if (Ntrend > 1) {
df_trend_code_per = df_trend_code_per[1,]
}
dataMean = mean(df_data_code$Qm3s, na.rm=TRUE)
trendMean = df_trend_code_per$trend / dataMean
color_res = get_color(trendMean,
minTrendMean[j, i],
maxTrendMean[j, i],
palette_name='perso',
reverse=TRUE)
colortmp = color_res
} else {
colortmp = paste('grey', grey, sep='')
grey = grey - 10
}
color = append(color, colortmp)
}
p = time_panel(df_data_code, df_trend_code, type=type,
p_threshold=p_threshold, missRect=missRect,
trend_period=trend_period,
mean_period=mean_period, axis_xlim=axis_xlim,
unit2day=unit2day, grid=FALSE, last=(i > nbp-nbcol),
color=color)
P[[i+nbh]] = p
}
layout_matrix = as.matrix(layout_matrix)
nel = nrow(layout_matrix)*ncol(layout_matrix)
idNA = which(is.na(layout_matrix), arr.ind=TRUE)
layout_matrix[idNA] = seq(max(layout_matrix, na.rm=TRUE) + 1,
max(layout_matrix, na.rm=TRUE) + 1 +
nel)
layout_matrix_H = layout_matrix + nbh
info_ratio_scale = info_ratio
time_ratio_scale = time_ratio
var_ratio_scale = var_ratio
ndec_info = 0
ndec_time = 0
ndec_var = 0
if (info_ratio_scale != round(info_ratio_scale)) {
ndec_info = nchar(gsub('^[0-9]+.', '',
as.character(info_ratio_scale)))
}
if (time_ratio_scale != round(time_ratio_scale)) {
ndec_time = nchar(gsub('^[0-9]+.', '',
as.character(time_ratio_scale)))
}
if (var_ratio_scale != round(var_ratio_scale)) {
ndec_var = nchar(gsub('^[0-9]+.', '',
as.character(var_ratio_scale)))
}
ndec = max(c(ndec_info, ndec_time, ndec_var))
info_ratio_scale = info_ratio_scale * 10^ndec
time_ratio_scale = time_ratio_scale * 10^ndec
var_ratio_scale = var_ratio_scale * 10^ndec
LM = c()
LMcol = ncol(layout_matrix_H)
LMrow = nrow(layout_matrix_H)
for (i in 1:(LMrow+nbh)) {
if (info_header & i == 1) {
# LM = rbind(LM, rep(i, times=LMcol))
LM = rbind(LM,
matrix(rep(rep(i, times=LMcol),
times=info_ratio_scale),
ncol=LMcol, byrow=TRUE))
} else if (!is.null(time_header) & i == 2) {
LM = rbind(LM,
matrix(rep(rep(i, times=LMcol),
times=time_ratio_scale),
ncol=LMcol, byrow=TRUE))
} else {
LM = rbind(LM,
matrix(rep(layout_matrix_H[i-nbh,],
times=var_ratio_scale),
ncol=LMcol, byrow=TRUE))
}}
plot = grid.arrange(grobs=P, layout_matrix=LM)
# plot = grid.arrange(rbind(cbind(ggplotGrob(P[[2]]), ggplotGrob(P[[2]])), cbind(ggplotGrob(P[[3]]), ggplotGrob(P[[3]]))), heights=c(1/3, 2/3))
# Saving
ggsave(plot=plot,
path=outdirTmp,
filename=paste(as.character(code), '.pdf', sep=''),
width=21, height=29.7, units='cm', dpi=100)
}
} }
if ('matrix' %in% isplot) { if ('matrix' %in% isplot) {
matrice_panel(list_df2plot, df_meta, trend_period, mean_period, matrix_panel(list_df2plot, df_meta, trend_period, mean_period,
slice=12, outdirTmp=outdirTmp, A3=TRUE) slice=12, outdirTmp=outdirTmp, A3=TRUE)
} }
if ('map' %in% isplot) { if ('map' %in% isplot) {
map_panel(list_df2plot, map_panel(list_df2plot,
df_meta, df_meta,
...@@ -436,5 +197,168 @@ panels_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet', ...@@ -436,5 +197,168 @@ panels_layout = function (df_data, df_meta, layout_matrix, isplot=c('datasheet',
pdf_combine(input=file.path(outdirTmp, list.files(outdirTmp)), pdf_combine(input=file.path(outdirTmp, list.files(outdirTmp)),
output=file.path(outdir, outfile)) output=file.path(outdir, outfile))
# unlink(outdirTmp, recursive=TRUE) # unlink(outdirTmp, recursive=TRUE)
} }
## 3. COLOR MANAGEMENT
### 3.1. Color on colorbar
get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE) {
if (palette_name == 'perso') {
colorList = palette_perso
} else {
colorList = brewer.pal(11, palette_name)
}
nSample = length(colorList)
palette = colorRampPalette(colorList)(ncolor)
Sample_hot = 1:(as.integer(nSample/2)+1)
Sample_cold = (as.integer(nSample/2)+1):nSample
palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor)
palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor)
if (reverse) {
palette = rev(palette)
palette_hot = rev(palette_hot)
palette_cold = rev(palette_cold)
}
if (is.na(value)) {
return (NA)
}
maxAbs = max(abs(max), abs(min))
if (value < 0) {
idNorm = (value + maxAbs) / maxAbs
id = round(idNorm*(ncolor - 1) + 1, 0)
color = palette_cold[id]
} else {
idNorm = value / maxAbs
id = round(idNorm*(ncolor - 1) + 1, 0)
color = palette_hot[id]
}
return(color)
}
### 3.2. Colorbar
get_palette = function (min, max, ncolor=256, palette_name='perso', reverse=FALSE, nbTick=10) {
if (palette_name == 'perso') {
colorList = palette_perso
} else {
colorList = brewer.pal(11, palette_name)
}
nSample = length(colorList)
palette = colorRampPalette(colorList)(ncolor)
Sample_hot = 1:(as.integer(nSample/2)+1)
Sample_cold = (as.integer(nSample/2)+1):nSample
palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor)
palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor)
if (reverse) {
palette = rev(palette)
palette_hot = rev(palette_hot)
palette_cold = rev(palette_cold)
}
if (min < 0 & max < 0) {
paletteShow = palette_cold
} else if (min > 0 & max > 0) {
paletteShow = palette_hot
} else {
paletteShow = palette
}
posTick = seq(0, 1, length.out=nbTick)
labTick = c()
colTick = c()
for (i in 1:nbTick) {
lab = (i-1)/(nbTick-1) * (max - min) + min
labTick = c(labTick, lab)
col = get_color(lab, min=min, max=max,
ncolor=ncolor,
palette_name=palette_name,
reverse=reverse)
colTick = c(colTick, col)
}
return(list(palette=paletteShow, posTick=posTick,
labTick=labTick, colTick=colTick))
}
### 3.3. Palette tester
palette_tester = function (n=256) {
X = 1:n
Y = rep(0, times=n)
palette = colorRampPalette(palette_perso)(n)
p = ggplot() +
theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank()
) +
geom_line(aes(x=X, y=Y), color=palette[X], size=60) +
scale_y_continuous(expand=c(0, 0))
ggsave(plot=p,
filename=paste('palette_test', '.pdf', sep=''),
width=10, height=10, units='cm', dpi=100)
}
## 4. OTHER
### 4.1. Number formatting
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)
}
### 4.2. Pourcentage of variable
gpct = function (pct, L, ref=NULL, shift=FALSE) {
if (is.null(ref)) {
minL = min(L, na.rm=TRUE)
} else {
minL = ref
}
maxL = max(L, na.rm=TRUE)
spanL = maxL - minL
xL = pct/100 * as.numeric(spanL)
if (shift) {
xL = xL + minL
}
return (xL)
}
This diff is collapsed.
...@@ -92,7 +92,7 @@ reprepare = function(df_XEx, df_Xlist, colnamegroup=NULL) { ...@@ -92,7 +92,7 @@ reprepare = function(df_XEx, df_Xlist, colnamegroup=NULL) {
df_XEx$Date = paste(df_XEx$Date, '01', '01', sep='-') df_XEx$Date = paste(df_XEx$Date, '01', '01', sep='-')
# If there is more than 2 dashes # If there is more than 2 dashes
} else if (nbt != 2) { } else if (nbt != 2) {
This is not a classical date # This is not a classical date
stop('erreur of date format') stop('erreur of date format')
} }
# Recreates the outing of the 'extract.Var' function nicer # Recreates the outing of the 'extract.Var' function nicer
......
...@@ -57,17 +57,17 @@ filedir = ...@@ -57,17 +57,17 @@ filedir =
filename = filename =
# "" # ""
c( # c(
"S2235610_HYDRO_QJM.txt", # "S2235610_HYDRO_QJM.txt",
"P1712910_HYDRO_QJM.txt", # "P1712910_HYDRO_QJM.txt",
"P0885010_HYDRO_QJM.txt", # "P0885010_HYDRO_QJM.txt",
"O5055010_HYDRO_QJM.txt", # "O5055010_HYDRO_QJM.txt",
"O0384010_HYDRO_QJM.txt" # "O0384010_HYDRO_QJM.txt"
) # )
# c("S4214010_HYDRO_QJM.txt", c("S4214010_HYDRO_QJM.txt",
# "O0384010_HYDRO_QJM.txt", "O0384010_HYDRO_QJM.txt",
# "Q7002910_HYDRO_QJM.txt") "Q7002910_HYDRO_QJM.txt")
...@@ -130,7 +130,6 @@ setwd(computer_work_path) ...@@ -130,7 +130,6 @@ setwd(computer_work_path)
source('processing/extract.R', encoding='latin1') source('processing/extract.R', encoding='latin1')
source('processing/format.R', encoding='latin1') source('processing/format.R', encoding='latin1')
source('processing/analyse.R', encoding='latin1') source('processing/analyse.R', encoding='latin1')
source('plotting/panel.R', encoding='latin1')
source('plotting/layout.R', encoding='latin1') source('plotting/layout.R', encoding='latin1')
# Result directory # Result directory
...@@ -256,39 +255,39 @@ df_shapefile = ini_shapefile(computer_data_path, fr_shpdir, fr_shpname, bs_shpdi ...@@ -256,39 +255,39 @@ df_shapefile = ini_shapefile(computer_data_path, fr_shpdir, fr_shpname, bs_shpdi
### 4.1. Simple time panel to criticize station data ### 4.1. Simple time panel to criticize station data
# Plot time panel of debit by stations # Plot time panel of debit by stations
# panels_layout(list(df_data, df_data), # datasheet_layout(list(df_data, df_data),
# layout_matrix=c(1, 2), # layout_matrix=c(1, 2),
# df_meta=df_meta, # df_meta=df_meta,
# missRect=list(TRUE, TRUE), # missRect=list(TRUE, TRUE),
# type=list('Q', 'sqrt(Q)'), # type=list('Q', 'sqrt(Q)'),
# info_header=TRUE, # info_header=TRUE,
# time_header=NULL, # time_header=NULL,
# var_ratio=3, # var_ratio=3,
# figdir=figdir, # figdir=figdir,
# filename_opt='time') # filename_opt='time')
### 4.2. Analysis layout ### 4.2. Analysis layout
panels_layout(isplot=c( datasheet_layout(isplot=c(
'datasheet', 'datasheet',
'matrix', 'matrix',
'map' 'map'
), ),
df_data=list(res_QAtrend$data, res_QMNAtrend$data, df_data=list(res_QAtrend$data, res_QMNAtrend$data,
res_VCN10trend$data), res_VCN10trend$data),
layout_matrix=c(1, 2, 3), layout_matrix=c(1, 2, 3),
df_meta=df_meta, df_meta=df_meta,
df_trend=list(res_QAtrend$trend, res_QMNAtrend$trend, df_trend=list(res_QAtrend$trend, res_QMNAtrend$trend,
res_VCN10trend$trend), res_VCN10trend$trend),
type=list('QA', 'QMNA', 'VCN10'), type=list('QA', 'QMNA', 'VCN10'),
missRect=list(TRUE, TRUE, TRUE), missRect=list(TRUE, TRUE, TRUE),
trend_period=trend_period, trend_period=trend_period,
mean_period=mean_period, mean_period=mean_period,
info_header=TRUE, info_header=TRUE,
time_header=df_data, time_header=df_data,
info_ratio=2, info_ratio=2,
time_ratio=2, time_ratio=2,
var_ratio=3, var_ratio=3,
df_shapefile=df_shapefile, df_shapefile=df_shapefile,
figdir=figdir, figdir=figdir,
filename_opt='') filename_opt='')
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