Skip to content
Snippets Groups Projects
Commit 45366a3c authored by Gael's avatar Gael
Browse files

fun_gg_boxplot() with new return.gtable argument and output

parent b0a3e335
No related branches found
No related tags found
No related merge requests found
...@@ -168,16 +168,48 @@ add = "+ggplot2::facet_wrap(facets = 'Categ2', labeller = 'label_both') + ggplot ...@@ -168,16 +168,48 @@ add = "+ggplot2::facet_wrap(facets = 'Categ2', labeller = 'label_both') + ggplot
### Other parameters ### Other parameters
fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", res <- fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1",
return = TRUE, return = TRUE,
return.ggplot = TRUE, return.ggplot = TRUE,
return.gtable = FALSE,
plot = FALSE, plot = FALSE,
warn.print = FALSE, warn.print = FALSE,
lib.path = NULL lib.path = NULL
) )
# plot the result
fun_open(pdf = FALSE)
res$ggplot
# display the output (manipulation of res triggers plotting because of the presence of non NULL $ggplot, which is annoying, as explain in the function description)
fun_open(pdf = FALSE)
res
# The advantage of $ggplot is that it is easy to update the plot
### Notes about the gtable output
res2 <- fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1",
return = TRUE,
return.ggplot = FALSE,
return.gtable = TRUE,
plot = TRUE, # plot must be TRUE to have a non NULL $gtable output
warn.print = FALSE,
lib.path = NULL
)
# display the results (does not plot the graph, contrary to $ggplot)
fun_open(pdf = FALSE)
res2
# replot
fun_open(pdf = FALSE)
gridExtra::grid.arrange(res2$gtable)
# plot the first grob
fun_open(pdf = FALSE)
gridExtra::grid.arrange(res2$gtable[1,1])
# plot the second grob
fun_open(pdf = FALSE)
gridExtra::grid.arrange(res2$gtable[1,2])
......
...@@ -7919,6 +7919,7 @@ article = TRUE, ...@@ -7919,6 +7919,7 @@ article = TRUE,
grid = FALSE, grid = FALSE,
return = FALSE, return = FALSE,
return.ggplot = FALSE, return.ggplot = FALSE,
return.gtable = TRUE,
plot = TRUE, plot = TRUE,
add = NULL, add = NULL,
warn.print = TRUE, warn.print = TRUE,
...@@ -7998,6 +7999,7 @@ lib.path = NULL ...@@ -7998,6 +7999,7 @@ lib.path = NULL
# grid: logical. Draw lines in the background to better read the box values? Not considered if article == FALSE (grid systematically present) # grid: logical. Draw lines in the background to better read the box values? Not considered if article == FALSE (grid systematically present)
# return: logical. Return the graph parameters? # return: logical. Return the graph parameters?
# return.ggplot: logical. Return the ggplot object in the output list? Ignored if return argument is FALSE. WARNING: always assign the fun_gg_boxplot() function (e.g., a <- fun_gg_boxplot()) if return.ggplot argument is TRUE, otherwise, double plotting is performed. See $ggplot in the RETURN section below for more details # return.ggplot: logical. Return the ggplot object in the output list? Ignored if return argument is FALSE. WARNING: always assign the fun_gg_boxplot() function (e.g., a <- fun_gg_boxplot()) if return.ggplot argument is TRUE, otherwise, double plotting is performed. See $ggplot in the RETURN section below for more details
# return.gtable: logical. Return the ggplot object as gtable of grobs in the output list? Ignored if plot argument is FALSE. Indeed, the graph must be plotted to get the grobs dispositions. See $gtable in the RETURN section below for more details
# plot: logical. Plot the graphic? If FALSE and return argument is TRUE, graphical parameters and associated warnings are provided without plotting # plot: logical. Plot the graphic? If FALSE and return argument is TRUE, graphical parameters and associated warnings are provided without plotting
# add: character string allowing to add more ggplot2 features (dots, lines, themes, facet, etc.). Ignored if NULL # add: character string allowing to add more ggplot2 features (dots, lines, themes, facet, etc.). Ignored if NULL
# WARNING: (1) the string must start with "+", (2) the string must finish with ")" and (3) each function must be preceded by "ggplot2::". Example: "+ ggplot2::coord_flip() + ggplot2::theme_bw()" # WARNING: (1) the string must start with "+", (2) the string must finish with ")" and (3) each function must be preceded by "ggplot2::". Example: "+ ggplot2::coord_flip() + ggplot2::theme_bw()"
...@@ -8044,6 +8046,7 @@ lib.path = NULL ...@@ -8044,6 +8046,7 @@ lib.path = NULL
# $axes: the x-axis and y-axis info # $axes: the x-axis and y-axis info
# $warn: the warning messages. Use cat() for proper display. NULL if no warning. WARNING: warning messages delivered by the internal ggplot2 functions are not apparent when using the argument plot = FALSE # $warn: the warning messages. Use cat() for proper display. NULL if no warning. WARNING: warning messages delivered by the internal ggplot2 functions are not apparent when using the argument plot = FALSE
# $ggplot: ggplot object that can be used for reprint (use print(...$ggplot) or update (use ...$ggplot + ggplot2::...). NULL if return.ggplot argument is FALSE. Of note, a non-NULL $ggplot in the output list is sometimes annoying as the manipulation of this list prints the plot # $ggplot: ggplot object that can be used for reprint (use print(...$ggplot) or update (use ...$ggplot + ggplot2::...). NULL if return.ggplot argument is FALSE. Of note, a non-NULL $ggplot in the output list is sometimes annoying as the manipulation of this list prints the plot
# $gtable: gtable object that can be used for reprint (use gridExtra::grid.arrange(...$ggplot) or with additionnal grobs (use ...$ggplot + ggplot2::...). NULL if return.ggplot argument is FALSE. Contrary to $ggplot, a non-NULL $gtable in the output list is not annoying as the manipulation of this list does not print the plot
# EXAMPLE # EXAMPLE
# DEBUGGING # DEBUGGING
# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10), stringsAsFactors = TRUE) ; set.seed(NULL) ; obs1$Time[1:10] <- NA ; data1 = obs1 ; y = "Time" ; categ = c("Group1") ; categ.class.order = NULL ; box.legend.name = NULL ; categ.color = c("green") ; box.fill = FALSE ; box.width = 0.5 ; box.space = 0.1 ; box.notch = FALSE ; box.line.size = 0.5 ; box.alpha = 0.5 ; box.mean = TRUE ; box.whisker.kind = "std" ; box.whisker.width = 0.5 ; dot.color = "black" ; dot.categ = "Group1"; dot.categ.class.order = c("G", "H") ; dot.legend.name = NULL ; dot.tidy = TRUE ; dot.tidy.bin.nb = 50 ; dot.jitter = 0.25 ; dot.size = 3 ; dot.alpha = 0.5 ; dot.border.size = 0.5 ; dot.border.color = NULL ; y.lim = NULL ; y.log = "no" ; y.tick.nb = NULL ; y.second.tick.nb = NULL ; y.include.zero = FALSE ; y.top.extra.margin = 0.05 ; y.bottom.extra.margin = 0.05 ; stat.disp = NULL ; stat.disp.mean = FALSE ; stat.size = 4 ; stat.dist = 2 ; x.lab = NULL ; y.lab = NULL ; vertical = TRUE ; text.size = 12 ; title = "" ; title.text.size = 8 ; legend.show = TRUE ; legend.width = 0.5 ; text.angle = 0 ; article = FALSE ; grid = FALSE ; return = TRUE ; return.ggplot = FALSE ; plot = TRUE ; add = NULL ; warn.print = FALSE ; lib.path = NULL # set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10), stringsAsFactors = TRUE) ; set.seed(NULL) ; obs1$Time[1:10] <- NA ; data1 = obs1 ; y = "Time" ; categ = c("Group1") ; categ.class.order = NULL ; box.legend.name = NULL ; categ.color = c("green") ; box.fill = FALSE ; box.width = 0.5 ; box.space = 0.1 ; box.notch = FALSE ; box.line.size = 0.5 ; box.alpha = 0.5 ; box.mean = TRUE ; box.whisker.kind = "std" ; box.whisker.width = 0.5 ; dot.color = "black" ; dot.categ = "Group1"; dot.categ.class.order = c("G", "H") ; dot.legend.name = NULL ; dot.tidy = TRUE ; dot.tidy.bin.nb = 50 ; dot.jitter = 0.25 ; dot.size = 3 ; dot.alpha = 0.5 ; dot.border.size = 0.5 ; dot.border.color = NULL ; y.lim = NULL ; y.log = "no" ; y.tick.nb = NULL ; y.second.tick.nb = NULL ; y.include.zero = FALSE ; y.top.extra.margin = 0.05 ; y.bottom.extra.margin = 0.05 ; stat.disp = NULL ; stat.disp.mean = FALSE ; stat.size = 4 ; stat.dist = 2 ; x.lab = NULL ; y.lab = NULL ; vertical = TRUE ; text.size = 12 ; title = "" ; title.text.size = 8 ; legend.show = TRUE ; legend.width = 0.5 ; text.angle = 0 ; article = FALSE ; grid = FALSE ; return = TRUE ; return.ggplot = FALSE ; plot = TRUE ; add = NULL ; warn.print = FALSE ; lib.path = NULL
...@@ -9678,6 +9681,7 @@ warn <- paste0(ifelse(is.null(warn), tempo.warn, paste0(warn, "\n\n", tempo.warn ...@@ -9678,6 +9681,7 @@ warn <- paste0(ifelse(is.null(warn), tempo.warn, paste0(warn, "\n\n", tempo.warn
   
# drawing # drawing
fin.plot <- suppressMessages(suppressWarnings(eval(parse(text = paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + "))))) fin.plot <- suppressMessages(suppressWarnings(eval(parse(text = paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + ")))))
grob.save <- NULL
if(plot == TRUE){ if(plot == TRUE){
# following lines inactivated because of problem in warn.recov and message.recov # following lines inactivated because of problem in warn.recov and message.recov
# assign("env_fun_get_message", new.env()) # assign("env_fun_get_message", new.env())
...@@ -9689,9 +9693,9 @@ if(plot == TRUE){ ...@@ -9689,9 +9693,9 @@ if(plot == TRUE){
# message.recov <- fun_get_message('print(eval(parse(text = paste(paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + "), if(is.null(add)){NULL}else{add}))))', kind = "message", header = FALSE, print.no = FALSE, env = env_fun_get_message) # for recovering messages printed by ggplot() functions # message.recov <- fun_get_message('print(eval(parse(text = paste(paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + "), if(is.null(add)){NULL}else{add}))))', kind = "message", header = FALSE, print.no = FALSE, env = env_fun_get_message) # for recovering messages printed by ggplot() functions
# if( ! (return == TRUE & return.ggplot == TRUE)){ # because return() plots when return.ggplot is TRUE # finally not used -> see return.ggplot description # if( ! (return == TRUE & return.ggplot == TRUE)){ # because return() plots when return.ggplot is TRUE # finally not used -> see return.ggplot description
if(is.null(legend.width)){ if(is.null(legend.width)){
suppressMessages(suppressWarnings(print(fin.plot))) grob.save <- suppressMessages(suppressWarnings(gridExtra::grid.arrange(fin.plot)))
}else{ }else{
suppressMessages(suppressWarnings(gridExtra::grid.arrange(fin.plot, legend.final, ncol=2, widths=c(1, legend.width)))) grob.save <-suppressMessages(suppressWarnings(gridExtra::grid.arrange(fin.plot, legend.final, ncol=2, widths=c(1, legend.width))))
} }
# } # }
# suppressMessages(suppressWarnings(print(eval(parse(text = paste(paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + "), if(is.null(add)){NULL}else{add})))))) # suppressMessages(suppressWarnings(print(eval(parse(text = paste(paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + "), if(is.null(add)){NULL}else{add}))))))
...@@ -9747,7 +9751,8 @@ y.labels = if(is.null(attributes(tempo$y$breaks))){tempo$y$breaks}else{tempo$y$s ...@@ -9747,7 +9751,8 @@ y.labels = if(is.null(attributes(tempo$y$breaks))){tempo$y$breaks}else{tempo$y$s
y.positions = if(is.null(attributes(tempo$y$breaks))){tempo$y$breaks}else{unlist(attributes(tempo$y$breaks))} y.positions = if(is.null(attributes(tempo$y$breaks))){tempo$y$breaks}else{unlist(attributes(tempo$y$breaks))}
), ),
warn = paste0("\n", warn, "\n\n"), warn = paste0("\n", warn, "\n\n"),
ggplot = if(return.ggplot == TRUE){fin.plot}else{NULL} # fin.plot plots the graph if return == TRUE ggplot = if(return.ggplot == TRUE){fin.plot}else{NULL}, # fin.plot plots the graph if return == TRUE
gtable = if(return.gtable == TRUE){grob.save}else{NULL} # fin.plot plots the graph if return == TRUE
) )
return(output) # this plots the graph if return.ggplot is TRUE and if no assignment return(output) # this plots the graph if return.ggplot is TRUE and if no assignment
} }
...@@ -9760,8 +9765,6 @@ return(output) # this plots the graph if return.ggplot is TRUE and if no assignm ...@@ -9760,8 +9765,6 @@ return(output) # this plots the graph if return.ggplot is TRUE and if no assignm
   
   
   
fun_gg_scatter <- function( fun_gg_scatter <- function(
data1, data1,
x, x,
...@@ -11889,3 +11892,5 @@ return(output) # this plots the graph if return.ggplot is TRUE and if no assignm ...@@ -11889,3 +11892,5 @@ return(output) # this plots the graph if return.ggplot is TRUE and if no assignm
   
   
   
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment