Skip to content

Commit

Permalink
#134 : fix variable2Axes + ylab2
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Thieurmel committed Apr 23, 2018
1 parent 1297598 commit ac3b37f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
6 changes: 5 additions & 1 deletion R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ tsPlot <- function(x, table = NULL, variable = NULL, elements = NULL,
)

uni_ele <- unique(dt$element)
variable2Axe <- uni_ele[grepl(paste(paste0("(", variable2Axe, ")"), collapse = "|"), uni_ele)]
if(!is.null(variable2Axe) && length(variable2Axe) > 0){
label_variable2Axe <- variable2Axe
variable2Axe <- uni_ele[grepl(paste(paste0("(", variable2Axe, ")"), collapse = "|"), uni_ele)]
}

# BP 2017
# if(length(main) > 0){
Expand All @@ -379,6 +382,7 @@ tsPlot <- function(x, table = NULL, variable = NULL, elements = NULL,
timeStep = timeStep,
variable = variable,
variable2Axe = variable2Axe,
label_variable2Axe = label_variable2Axe,
typeConfInt = typeConfInt,
confInt = confInt,
minValue = minValue,
Expand Down
41 changes: 29 additions & 12 deletions R/plot_stats.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.plotMonotone <- function(dt, timeStep, variable, variable2Axe = NULL, typeConfInt = FALSE, confInt = NULL, maxValue,
main = NULL, ylab = NULL, highlight = FALSE, stepPlot = FALSE, drawPoints = FALSE, language = "en", ...) {
main = NULL, ylab = NULL, highlight = FALSE, stepPlot = FALSE, drawPoints = FALSE, language = "en",
label_variable2Axe = NULL, ...) {

uniqueElements <- as.character(sort(unique(dt$element)))
plotConfInt <- FALSE
Expand Down Expand Up @@ -37,13 +38,21 @@
main <- paste(.getLabelLanguage("Monotone of", language), variable)
}

if(!is.null(variable2Axe) && length(variable2Axe) > 0){
ylab2 <- paste0(label_variable2Axe, collapse = " ; ")
} else {
ylab2 <- NULL
}

.plotStat(dt, ylab = ylab, main = main, uniqueElements = uniqueElements, variable2Axe = variable2Axe,
plotConfInt = plotConfInt, highlight = highlight, stepPlot = stepPlot, drawPoints = drawPoints, ...)
plotConfInt = plotConfInt, highlight = highlight, stepPlot = stepPlot, drawPoints = drawPoints,
ylab2 = ylab2, ...)

}

.density <- function(dt, timeStep, variable, variable2Axe = NULL, minValue = NULL, maxValue = NULL,
main = NULL, ylab = NULL, highlight = FALSE, stepPlot = FALSE, drawPoints = FALSE, language = "en", ...) {
main = NULL, ylab = NULL, highlight = FALSE, stepPlot = FALSE, drawPoints = FALSE,
language = "en", label_variable2Axe = NULL, ...) {

uniqueElements <- as.character(sort(unique(dt$element)))

Expand All @@ -59,15 +68,22 @@

variable <- paste0(variable, collapse = " ; ")
if (is.null(ylab)){
ylab <- .getLabelLanguage("Density", language)
# ylab <- .getLabelLanguage("Density", language)
ylab <- variable
}

if(!is.null(variable2Axe) && length(variable2Axe) > 0){
ylab2 <- paste0(label_variable2Axe, collapse = " ; ")
} else {
ylab2 <- NULL
}

if (is.null(main) | isTRUE(all.equal("", main))){
main <- paste(.getLabelLanguage("Density of", language), variable)
}

.plotStat(dt, ylab = ylab, main = main, uniqueElements = uniqueElements,variable2Axe = variable2Axe,
highlight = highlight, stepPlot = stepPlot, drawPoints = drawPoints,...)
highlight = highlight, stepPlot = stepPlot, drawPoints = drawPoints, ylab2 = ylab2, ...)

}

Expand Down Expand Up @@ -128,7 +144,7 @@
.plotStat <- function(dt, ylab, main, colors, uniqueElements,
legend, legendItemsPerRow, width, height,
plotConfInt = FALSE, highlight = FALSE,
stepPlot = FALSE, drawPoints = FALSE,variable2Axe = NULL, language = "en", ...) {
stepPlot = FALSE, drawPoints = FALSE,variable2Axe = NULL, language = "en", ylab2 = NULL, ...) {
dt <- dcast(dt, x ~ element, value.var = "y")

if (is.null(colors)) {
Expand Down Expand Up @@ -170,18 +186,19 @@
}
}

if(highlight)
{
if(highlight){
g <- g %>% dyHighlight(highlightSeriesOpts = list(strokeWidth = 2))
}

if(!is.null(ylab2)){
g <- g %>% dyAxis("y2", label = ylab2)
}

if (plotConfInt) {
for (v in uniqueElements) {
axis = NULL
if(length(variable2Axe)>0)
{
if(v%in%variable2Axe)
{
if(length(variable2Axe)>0){
if(v%in%variable2Axe){
axis <- "y2"
}
}
Expand Down
47 changes: 28 additions & 19 deletions R/plot_ts.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
#'
#' @noRd
#'
.plotTS <- function(dt, timeStep, variable, variable2Axe = NULL, typeConfInt = FALSE,
.plotTS <- function(dt, timeStep, variable, variable2Axe = NULL,
typeConfInt = FALSE,
confInt = 0, maxValue,
colors = NULL,
main = NULL,
ylab = NULL,
legend = TRUE,
legendItemsPerRow = 5,
group = NULL,
width = NULL, height = NULL, highlight = FALSE, stepPlot = FALSE, drawPoints = FALSE, language = language, ...) {
width = NULL, height = NULL, highlight = FALSE, stepPlot = FALSE, drawPoints = FALSE,
language = language, label_variable2Axe = NULL, ...) {

uniqueElements <- as.character(sort(unique(dt$element)))
plotConfInt <- FALSE
Expand All @@ -34,11 +36,11 @@
# If dt contains several Monte-Carlo scenario, compute aggregate statistics
if (!is.null(dt$mcYear)) {
if (confInt == 0) {

dt <- dt[, .(value = mean(value)), by = .(element, time)]

} else {

plotConfInt <- TRUE
alpha <- (1 - confInt) / 2
dt <- dt[, .(value = c(mean(value), quantile(value, c(alpha, 1 - alpha))),
Expand All @@ -48,19 +50,26 @@
}
}
}

dt <- dcast(dt, time ~ element, value.var = "value")

# Graphical parameters
if(length(uniqueElements)> 1)
{
variable <- paste0(uniqueElements, collapse = " ; ")
}else{
if(is.null(ylab)){
ylab <- paste0(variable, collapse = " ; ")
}

ind_2_axes <- FALSE
if(!is.null(variable2Axe) && length(variable2Axe) > 0){
ind_2_axes <- TRUE
ylab_2 <- paste0(label_variable2Axe, collapse = " ; ")
}

if(length(uniqueElements)> 1){
variable <- paste0(uniqueElements, collapse = " ; ")
} else {
variable <- paste0(uniqueElements, " - ", variable)

}

if (is.null(ylab)) ylab <- variable
if (is.null(main) | isTRUE(all.equal("", main))){
main <- paste(.getLabelLanguage("Evolution of", language), variable)
}
Expand Down Expand Up @@ -106,20 +115,20 @@
}
}

if(highlight)
{
if(highlight){
g <- g %>% dyHighlight(highlightSeriesOpts = list(strokeWidth = 2))
}

if(ind_2_axes){
g <- g %>% dyAxis("y2", label = ylab_2)
}
if (plotConfInt) {
for (v in uniqueElements) {
axis = NULL
if(length(variable2Axe)>0)
{
if(v%in%variable2Axe)
{
axis <- "y2"
}
if(length(variable2Axe) > 0){
if(v %in% variable2Axe){
axis <- "y2"
}
}
g <- g %>% dySeries(paste0(v, c("_l", "", "_u")), axis = axis)
}
Expand Down

0 comments on commit ac3b37f

Please sign in to comment.