diff --git a/README.md b/README.md index 6f6c6f4f2eead422c4644ef41b47cb6b798e9649..3adb0b3282fc4facf4dc070d18209be931a9e6ca 100644 --- a/README.md +++ b/README.md @@ -1,174 +1,258 @@ -#### DESCRIPTION +[//]: # "#to make links in gitlab: example with racon https://github.com/isovic/racon" +[//]: # "tricks in markdown: https://openclassrooms.com/fr/courses/1304236-redigez-en-markdown" + +| usage | R dependencies | +| --- | --- | +| [](https://cran.r-project.org/mirrors.html) | [](https://cran.r-project.org/mirrors.html) | +| [-green?style=plastic)](https://www.gnu.org/licenses) | [](http://www.rforge.net/car/) | +| | [](http://www.rforge.net/carData/) | +| | [](http://www.rforge.net/emmeans/) | +| | [](https://github.com/tidyverse/ggplot2) | +| | [](http://www.rforge.net/lemon/) | +| | [](http://www.rforge.net/lme4/) | +| | [](http://www.rforge.net/lmerTest/) | +| | [](https://github.com/tidyverse/lubridate) | +| | [](http://www.rforge.net/Matrix/) | +| | [](https://gitlab.pasteur.fr/gmillot/cute_little_R_functions) | + + +## TABLE OF CONTENTS + + - [AIM](#aim) + - [REPOSITORY CONTENT](#repository-content) + - [DESCRIPTIONS OF THE FUNCTIONS](#description-of-the-functions) + - [HOW TO HOW TO LOAD AND MANIPULATE](#how-to-load-and-manipulate) + - [VERSIONS](#versions) + - [LICENCE](#licence) + - [CITATION](#citation) + - [CREDITS](#credits) + - [ACKNOWLEDGEMENTS](#Acknowledgements) + - [WHAT'S NEW IN](#what's-new-in) -Cute Little R Functions contains 42 functions for R/RStudio that facilitate basic procedures in 1) object analysis, 2) object modification, 3) graphic handling and 4) log file management. -The function names are: +## AIM -## Object analysis -fun_param_check() #### check class, type, length, etc., of objects -fun_object_info() #### recover object information -fun_1D_comp() #### comparison of two 1D datasets (vectors, factors, 1D tables) -fun_2D_comp() #### comparison of two 2D datasets (row & col names, dimensions, etc.) -fun_2D_head() #### head of the left or right of big 2D objects -fun_2D_tail() #### tail of the left or right of big 2D objects -fun_list_comp() #### comparison of two lists +Set of functions that facilitate basic procedures in 1) object analysis, 2) object modification, 3) graphic handling and 4) log file management. +The present repository of Cute Little R functions is for beta testing. Ultimately, functions will be provided as packages -## Object modification -fun_name_change() #### check a vector of character strings and modify any string if present in another vector -fun_dataframe_remodeling() #### remodeling a data frame to have column name as a qualitative values and vice-versa -fun_refactorization() #### remove classes that are not anymore present in factors or factor columns in data frames -fun_round() #### rounding number if decimal present -fun_90clock_matrix_rot() #### 90° clockwise matrix rotation -fun_num2color_mat() #### convert a numeric matrix into hexadecimal color matrix -fun_by_case_matrix_op() #### assemble several matrices with operation -fun_mat_inv() #### return the inverse of a square matrix -fun_mat_fill() #### fill the empty half part of a symmetric square matrix -fun_perm() #### progressively breaks a vector order -fun_perm_consec() #### progressively breaks a vector order +## REPOSITORY CONTENT -## Graphics management +**cute_little_R_functions.R** file that has to be sourced in R or RStudio -fun_window_width_resizing() #### window width depending on classes to plot -fun_open_window() #### open a GUI or pdf graphic window -fun_prior_plot() #### set graph param before plotting -fun_scale() #### select nice numbers when setting breaks on an axis -fun_post_plot() #### set graph param after plotting -fun_close_specif_window() #### close specific graphic windows +**cute_little_R_functions.docx** same as cute_little_R_functions.R but for better reading (interactive outline at the beginning of the file) +**examples.R** examples of all the functions, except fun_gg_boxplot() and fun_gg_scatter(), that can be sourced or copied-pasted -## Standard graphics +**boxplot_examples.R** examples of fun_gg_boxplot() that can be sourced or copied-pasted -fun_empty_graph() #### text to display for empty graphs +**scatter_examples.R** examples of fun_gg_scatter() that can be sourced or copied-pasted +**other** folder containing avorted developments -## gg graphics -fun_gg_palette() #### ggplot2 default color palette -fun_gg_just() #### ggplot2 justification of the axis labeling, depending on angle -fun_gg_scatter() #### ggplot2 scatterplot + lines (up to 6 overlays totally) -fun_gg_bar_mean() #### ggplot2 mean barplot + overlaid dots if required -fun_gg_boxplot() #### ggplot2 boxplot + background dots if required -fun_gg_bar_prop() #### ggplot2 proportion barplot -fun_gg_strip() #### ggplot2 stripchart + mean/median -fun_gg_violin() #### ggplot2 violins -fun_gg_line() #### ggplot2 lines + background dots and error bars -fun_gg_heatmap() #### ggplot2 heatmap + overlaid mask if required -fun_gg_empty_graph() #### text to display for empty graphs +## DESCRIPTIONS OF THE FUNCTIONS +| Object analysis | | +| --- | --- | +| **fun_check() | check class, type, length, etc., of objects | +| **fun_secu() | verif that local variables are not present in other envs | +| **fun_info() | recover object information | +| **fun_head() | head of the left or right of big 2D objects | +| **fun_tail() | tail of the left or right of big 2D objects | +| **fun_comp_1d() | comparison of two 1D datasets (vectors, factors, 1D tables) | +| **fun_comp_2d() | comparison of two 2D datasets (row & col names, dimensions, etc.) | +| **fun_comp_list() | comparison of two lists | +| **fun_test() | test combinations of argument values of a function and return errors (and graphs) | -## Graphic extraction +| Object modification | | +| --- | --- | +| **fun_name_change() | check a vector of character strings and modify any string if present in another vector | +| **fun_df_remod() | remodeling a data frame to have column name as a qualitative values and vice-versa | +| **fun_round() | rounding number if decimal present | +| **fun_mat_rotate() | 90° clockwise matrix rotation | +| **fun_mat_num2color() | convert a numeric matrix into hexadecimal color matrix | +| **fun_mat_op() | assemble several matrices with operation | +| **fun_mat_inv() | return the inverse of a square matrix | +| **fun_mat_fill() | fill the empty half part of a symmetric square matrix | +| **fun_permut() | progressively breaks a vector order | +| **fun_slide() | return a computation made on a vector using a sliding window | -fun_var_trim_display() #### display values from a quantitative variable and trim according to defined cut-offs -fun_segmentation() #### segment a dot cloud on a scatterplot and define the dots from another cloud outside the segmentation +| Graphics management | | +| --- | --- | +| **fun_width() | window width depending on classes to plot | +| **fun_open() | open a GUI or pdf graphic window | +| **fun_prior_plot() | set graph param before plotting (erase axes for instance) | +| **fun_scale() | select nice label numbers when setting number of ticks on an axis | +| **fun_inter_ticks() | define coordinates of secondary ticks | +| **fun_post_plot() | set graph param after plotting (axes redesign for instance) | +| **fun_close() | close specific graphic windows | +| Standard graphics | | +| --- | --- | +| **fun_empty_graph() | text to display for empty graphs | -## Import +| gg graphics | | +| --- | --- | +| **fun_gg_palette() | ggplot2 default color palette | +| **fun_gg_just() | ggplot2 justification of the axis labeling, depending on angle | +| **fun_gg_get_legend() | get the legend of ggplot objects | +| **fun_gg_point_rast() | ggplot2 raster scatterplot layer | +| **fun_gg_boxplot() | ggplot2 boxplot + background dots if required | +| **fun_gg_scatter() | ggplot2 scatterplot + lines (up to 6 overlays totally) | +| **fun_gg_empty_graph() | text to display for empty graphs | -fun_pack_import() #### check if R packages are present and import into the working environment -fun_python_pack_import() #### check if python packages are present +| Graphic extraction | | +| --- | --- | +| **fun_trim() | display values from a quantitative variable and trim according to defined cut-offs | +| **fun_segmentation() | segment a dot cloud on a scatterplot and define the dots from another cloud outside the segmentation | +| Import | | +| --- | --- | +| **fun_pack() | check if R packages are present and import into the working environment | +| **fun_python_pack() | check if python packages are present | -## Exporting results (text & tables) +| Print / Exporting results (text & tables) | | +| --- | --- | +| **fun_report() | print string or data object into output file | +| **fun_get_message() | return error/warning/other messages of an expression (that can be exported) | -fun_export_data() #### print string or data object into output file +## HOW TO LOAD AND MANIPULATE cute_little_R_functions +Download the desired Tagged version, never the current master, at https://gitlab.pasteur.fr/gmillot/cute_little_R_functions/-/tags -#### LICENCE + For that: -This package of scripts can be redistributed and/or modified under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. -Distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -See the GNU General Public License for more details at https://www.gnu.org/licenses. - - - - -#### CREDITS - -Gael A. Millot, Hub-C3BI, Institut Pasteur, USR 3756 IP CNRS, Paris, France - - - - -#### HOW TO USE IT - -1) Download the desired Tagged version, never the current master, at https://gitlab.pasteur.fr/gmillot/cute_little_R_functions/-/tags + 1) Select the desired tagged release + + 2) Browse files + + 3) Display the cute_little_R_functions.R as raw file + + 4) Source the file using the url address. Example + ` + source("https://gitlab.pasteur.fr/gmillot/cute_little_R_functions/-/raw/557976d693e37b499b89c76e4b2142846eb89d5b/cute_little_R_functions.R") + ` -2) Directly source the cute_little_R_functions.R into R/RStudio to have the functions available in the working environment, using for instance source("https://gitlab.pasteur.fr/gmillot/cute_little_R_functions/raw/v6.0.0/cute_little_R_functions.R") Description of the functions is at the beginning of the function body. To obtain it: Either read the cute_little_functions.docx (or open the cute_little_R_functions.R file). - Or in the R/RStudio console, type the name of the function without brackets. Example : fun_object_info. + Or in the R/RStudio console, type the name of the function without brackets. Example : `fun_info`. +## VERSIONS +The different releases are tagged [here](https://gitlab.pasteur.fr/gmillot/cute_little_R_functions/-/tags) -#### FILE DESCRIPTIONS -cute_little_R_functions.R file that has to be sourced -cute_little_R_functions.docx file that facilitate reading of the function description -examples_alone.txt compile all the examples of the functions into a single file +## LICENCE +This package of scripts can be redistributed and/or modified under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +Distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchandability or fitness for a particular purpose. +See the GNU General Public License for more details at https://www.gnu.org/licenses. +## CITATION -#### WEB LOCATION +Not yet published -Check for updated versions (most recent tags) at https://gitlab.pasteur.fr/gmillot/cute_little_R_functions/-/tags +## CREDITS +[Gael A. Millot](https://gitlab.pasteur.fr/gmillot), Hub-CBD, Institut Pasteur, USR 3756 IP CNRS, Paris, France -#### WHAT'S NEW IN +## ACKNOWLEDGEMENTS +R and R packages developers -## v6.0.0 +Gitlab developers + + +## WHAT'S NEW IN + +### v6.0.0 1) name of functions changed: + fun_param_check() fun_check() + fun_object_info() fun_info() + fun_1D_comp() fun_comp_1d() + fun_2D_comp() fun_comp_2d() + fun_list_comp() fun_comp_list() + fun_2D_head() fun_head() + fun_2D_tail() fun_tail() + fun_dataframe_remodeling() fun_df_remod() + fun_by_case_matrix_op() fun_mat_op() + fun_rounding() fun_round() + fun_90clock_matrix_rot() fun_mat_rotate() + fun_hexa_hsv_color_matrix() fun_mat_num2color() + fun_graph_param_prior_plot() fun_prior_plot() + fun_feature_post_plot() fun_post_plot() + fun_window_width_resizing() fun_width() + fun_open_window() fun_open() + fun_close_specif_window() fun_close() + fun_var_trim_display() fun_trim() + fun_export_data() fun_report() + + 2) new functions added: + fun_name_change() + fun_mat_fill() + fun_permut() + fun_permut_consec() + fun_empty_graph() + fun_gg_palette() + fun_gg_just() + fun_gg_point_rast() + fun_gg_scatter() -fun_gg_bar() -fun_gg_heatmap() + fun_gg_empty_graph() + fun_segmentation() + fun_pack() + fun_python_pack() + fun_warning() + + 3) text error modified in fun_head() and fun_tail(), + deals without all objects but use head() and tail() if not 2D object 4) in fun_param_check(): (1) has now the class = "vector", (2) argument fun.name added @@ -178,69 +262,72 @@ fun_warning() 6) Functions checked for R version 3.6.1 7) function deprecated: + fun_refactorization() -## v5.1.0 +### v5.1.0 1) bugs corrected in fun_2D_head() and fun_2D_tail() functions -## v5.0.0 +### v5.0.0 1) fun_2D_head() function added + 2) fun_2D_tail() function added + 3) argument "noquote" added in fun_export_data() -## v4.9.0 +### v4.9.0 1) fun_mat_inv() function added -## v4.8.0 +### v4.8.0 1) magnific argument removed in fun_feature_post_plot() because no need anymore -## v4.7.0 +### v4.7.0 1) check of numeric arguments: class = "numeric" -> mode = "numeric" to allow for integers values in these arguments -## v4.6.0 +### v4.6.0 1) fun_by_case_matrix_op() added -## v4.5.0 +### v4.5.0 1) fun_open_window() improved to deal with Linux systems 2) fun_graph_param_prior_plot() improved to deal with Linux systems -## v4.4.0 +### v4.4.0 1) fun_dataframe_remodeling() now add a ini_rowname column in the output data frame when it is coherent and when initial row names are available (not NULL) -## v4.3.0 +### v4.3.0 1) fun_object_info() now manages object class "ordered" "factor" -## v4.2.0 +### v4.2.0 1) bug in the fun_2D_comp() function fixed -## v4.1.0 +### v4.1.0 1) text.corner replaced by corner.text everywhere -## v4.0.0 +### v4.0.0 1) fun_var_trim_display() function added @@ -251,14 +338,14 @@ fun_refactorization() 4) error messages now mention the function that generates this message -## v3.1.0 +### v3.1.0 1) fun_export_data() function modified: argument data cannot be NULL 2) the debugging line containing r_debugging_tools-v1.2.R updated for the new position of this script -## v3.0.0 +### v3.0.0 1) cute_little_functions.R renamed cute_little_R_functions.R @@ -267,12 +354,12 @@ fun_refactorization() 3) more examples added, also draws added in the .docx file for fun_dataframe_remodeling() -## v2.0.1 +### v2.0.1 1) functions that use other functions from this toolset now check if these required functions are indeed present in the R environment -## v2.0.0 +### v2.0.0 1) fun_feature_post_plot() function improved: Now independent magnification for x axis/labels and y axis/labels. BEWARE: argument names have been modified @@ -281,7 +368,13 @@ fun_refactorization() 3) Bug corrected in fun_refactorization() function -## v1.3 +### v1.3 + +Everything + + + + 1) fun_1D_comp() function improved: provide the common elements, common names and common levels if exist diff --git a/cute_little_R_functions.R b/cute_little_R_functions.R index ebdb421e8ffef3161d3134e6017e3ef0d3842a55..fa0bcd327195991bf21966b51d6ecd97de590d9e 100644 --- a/cute_little_R_functions.R +++ b/cute_little_R_functions.R @@ -7966,7 +7966,6 @@ return(output) # do not use cat() because the idea is to reuse the message -# remain to solve the justification of the text # check s/lapply everywhere with get: done cute boxplot scatter # get() with env everywhere: done cute boxplot scatter # add the new NA and NULL check cute, boxplot, scatter: done boxplot, scatter @@ -8011,7 +8010,7 @@ y.bottom.extra.margin = 0.05, stat.disp = "top", stat.disp.mean = FALSE, stat.size = 4, -stat.dist = 2, +stat.dist = 5, stat.angle = 0, vertical = TRUE, text.size = 12, @@ -8091,7 +8090,7 @@ lib.path = NULL # stat.disp: add the median number above the corresponding box. Either NULL (no number shown), "top" (at the top of the plot region) or "above" (above each box) # stat.disp.mean: logical. Display mean numbers instead of median numbers? Ignored if stat.disp is NULL # stat.size: numeric value of the stat font size in mm. Ignored if stat.disp is NULL -# stat.dist: numeric value of the stat distance (in the unit of the hjust and vjust arguments of ggplot2::annotate() function). Increase the value to increase the distance from the box plot. Ignored if stat.disp is NULL or "top" +# stat.dist: numeric value of the stat distance in percentage of the y-axis range (stat.dist = 5 means move the number displayed at 5% of the y-axis range). Ignored if stat.disp is NULL or "top" # stat.angle: integer value of the angle of stat, using the same rules as in ggplot2. Positive values for counterclockwise rotation: 0 for horizontal, 90 for vertical, 180 for upside down etc. Negative values for clockwise rotation: 0 for horizontal, -90 for vertical, -180 for upside down etc. # vertical: logical. Vertical boxes? WARNING: will be automatically set to TRUE if y.log argument is other than "no". Indeed, not possible to have horizontal boxes with a log axis, due to a bug in ggplot2 (see https://github.com/tidyverse/ggplot2/issues/881) # text.size: numeric value of the font size of the (1) axis numbers, (2) axis labels and (3) texts in the graphic legend (in mm) @@ -8142,8 +8141,7 @@ lib.path = NULL # $sup.whisker.edge: coordinates of top whisker edges (x and xend) # $inf.whisker.edge: coordinates of bottom whisker edges(x and xend) # $mean: diamond mean coordinates (only if box.mean argument is TRUE) -# $stat.display.positive: coordinates of stat numbers when positive (only if stat.disp argument is TRUE) -# $stat.display.negative: coordinates of stat numbers when negative (only if stat.disp argument is TRUE) +# $stat.display: coordinates of stat numbers (only if stat.disp argument is not NULL) # y.second.tick.positions: coordinates of secondary ticks (only if y.second.tick.nb argument is non-NULL or if y.log argument is different from "no") # y.second.tick.values: values of secondary ticks. NULL except if y.second.tick.nb argument is non-NULL or if y.log argument is different from "no") # $panel: the variable names used for the panels (NULL if no panels). WARNING: NA can be present according to ggplot2 upgrade to v3.3.0 @@ -8155,7 +8153,7 @@ lib.path = NULL # DEBUGGING # set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Categ1 = rep(c("G", "H"), each = 10), stringsAsFactors = TRUE) ; set.seed(NULL) ; obs1$Time[1:10] <- NA ; data1 = obs1 ; y = "Time" ; categ = c("Categ1") ; 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 = "Categ1"; 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 ; stat.angle = 0 ; x.lab = NULL ; y.lab = NULL ; vertical = TRUE ; text.size = 12 ; title = "" ; title.text.size = 8 ; legend.show = TRUE ; legend.width = 0.5 ; x.angle = 0 ; article = FALSE ; grid = FALSE ; return = TRUE ; return.ggplot = FALSE ; return.gtable = FALSE ; plot = TRUE ; add = NULL ; warn.print = FALSE ; lib.path = NULL # set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Categ1 = rep(c("G", "H"), each = 10), Categ2 = rep(c("A", "B"), time = 10), Categ3 = rep(c("I", "J"), time = 10), stringsAsFactors = TRUE) ; set.seed(NULL) ; obs1$Time[1:10] <- NA ; data1 = obs1 ; y = "Time" ; categ = c("Categ1", "Categ2") ; categ.class.order = list(c("G", "H"), c("A", "B")); box.legend.name = NULL ; categ.color = c("green", "blue") ; 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 = "Categ1" ; dot.categ.class.order = NULL ; dot.legend.name = NULL ; dot.tidy = TRUE ; dot.tidy.bin.nb = 30 ; 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 ; stat.angle = 0 ; x.lab = NULL ; y.lab = NULL ; vertical = TRUE ; text.size = 12 ; title = "" ; title.text.size = 8 ; legend.show = TRUE ; legend.width = 0.5 ; x.angle = 0 ; article = FALSE ; grid = FALSE ; return = FALSE ; return.ggplot = FALSE ; return.gtable = FALSE ; plot = TRUE ; add = NULL ; warn.print = FALSE ; lib.path = NULL -# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Categ1 = rep(c("G", "H"), each = 10), Categ2 = rep(c("A", "B"), time = 10), stringsAsFactors = TRUE) ; set.seed(NULL) ; data1 = obs1 ; y = "Time" ; categ = c("Categ1") ; categ.class.order = list(c("H", "G")); box.legend.name = NULL ; categ.color = c("blue") ; box.fill = FALSE ; box.width = 0.5 ; box.space = 0.1 ; box.notch = TRUE ; box.line.size = 1 ; box.alpha = 1 ; box.mean = FALSE ; box.whisker.kind = "max" ; box.whisker.width = 0 ; dot.color = "black" ; dot.categ = "Categ1" ; dot.categ.class.order = NULL ; dot.legend.name = NULL ; dot.tidy = TRUE ; dot.tidy.bin.nb = 30 ; 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 = "log10" ; 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 ; stat.angle = 0 ; x.lab = NULL ; y.lab = NULL ; vertical = TRUE ; text.size = 12 ; title = "" ; title.text.size = 8 ; legend.width = 0.5 ; legend.show = TRUE ; x.angle = 0 ; article = FALSE ; grid = FALSE ; return = FALSE ; return.ggplot = FALSE ; return.gtable = FALSE ; plot = TRUE ; add = NULL ; warn.print = FALSE ; lib.path = NULL +# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Categ1 = rep(c("G", "H"), each = 10), Categ2 = rep(c("A", "B"), time = 10), stringsAsFactors = TRUE) ; set.seed(NULL) ; data1 = obs1 ; y = "Time" ; categ = c("Categ1") ; categ.class.order = list(c("H", "G")); box.legend.name = NULL ; categ.color = c("blue") ; box.fill = FALSE ; box.width = 0.5 ; box.space = 0.1 ; box.notch = TRUE ; box.line.size = 1 ; box.alpha = 1 ; box.mean = FALSE ; box.whisker.kind = "max" ; box.whisker.width = 0 ; dot.color = "black" ; dot.categ = "Categ1" ; dot.categ.class.order = NULL ; dot.legend.name = NULL ; dot.tidy = TRUE ; dot.tidy.bin.nb = 30 ; 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 = "log10" ; 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 = "above" ; stat.disp.mean = FALSE ; stat.size = 4 ; stat.dist = 2 ; stat.angle = 0 ; x.lab = NULL ; y.lab = NULL ; vertical = TRUE ; text.size = 12 ; title = "" ; title.text.size = 8 ; legend.width = 0.5 ; legend.show = TRUE ; x.angle = 0 ; article = FALSE ; grid = FALSE ; return = FALSE ; return.ggplot = FALSE ; return.gtable = FALSE ; plot = TRUE ; add = NULL ; warn.print = FALSE ; lib.path = NULL # function name function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()") arg.user.setting <- as.list(match.call(expand.dots = FALSE))[-1] # list of the argument settings (excluding default values not provided by the user) @@ -9055,12 +9053,22 @@ assign(paste0(tempo.gg.name, tempo.gg.count <- tempo.gg.count + 1), ggplot2::sca tempo.cat <- paste0("INTERNAL CODE ERROR IN ", function.name, "\nCODE INCONSISTENCY 1") stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n", ifelse(is.null(warn), "", paste0("IN ADDITION\nWARNING", ifelse(warn.count > 1, "S", ""), ":\n\n", warn))), call. = FALSE) # == in stop() to be able to add several messages between == } -stat.just <- fun_gg_just(angle = stat.angle, pos = ifelse(vertical == TRUE, "top", "right"), kind = "text") +if( ! is.null(stat.disp)){ +stat.just <- fun_gg_just( +angle = stat.angle, +pos = ifelse( +vertical == TRUE, +ifelse(stat.disp == "top", "bottom", "top"), # "bottom" because we want justification for text that are below the ref point which is the top of the graph. The opposite for "above" +ifelse(stat.disp == "top", "left", "right") # "left" because we want justification for text that are on the left of the ref point which is the right border of the graph. The opposite for "above" +), +kind = "text" +) +} # has in fact no interest because ggplot2 does not create room for geom_text() tempo.data.max <- data1[which.max(data1[, y]), ] tempo.data.max <- data.frame(tempo.data.max, label = formatC(tempo.data.max[, y], digit = 2, drop0trailing = TRUE, format = "f")) # end has in fact no interest because ggplot2 does not create room for geom_text() -tempo.graph.info.ini <- ggplot2::ggplot_build(eval(parse(text = paste(paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + "), if( ! is.null(stat.disp)){' + ggplot2::geom_text(data = tempo.data.max, mapping = ggplot2::aes_string(x = 1, y = y, label = "label"), size = stat.size, color = "black", angle = stat.angle, hjust = ifelse(vertical == TRUE, stat.just$hjust, stat.just$hjust - stat.dist), vjust = ifelse(vertical == TRUE, stat.just$vjust - stat.dist, stat.just$vjust))'})))) # added here to have room for annotation +tempo.graph.info.ini <- ggplot2::ggplot_build(eval(parse(text = paste(paste(paste0(tempo.gg.name, 1:tempo.gg.count), collapse = " + "), if( ! is.null(stat.disp)){' + ggplot2::geom_text(data = tempo.data.max, mapping = ggplot2::aes_string(x = 1, y = y, label = "label"), size = stat.size, color = "black", angle = stat.angle, hjust = stat.just$hjust, vjust = stat.just$vjust)'})))) # added here to have room for annotation dot.coord <- tempo.graph.info.ini$data[[1]] dot.coord$x <- as.numeric(dot.coord$x) # because weird class dot.coord$PANEL <- as.numeric(dot.coord$PANEL) # because numbers as levels. But may be a problem is facet are reordered ? @@ -9698,8 +9706,8 @@ mapping = ggplot2::aes_string(x = "X", y = "Y", label = ifelse(stat.disp.mean == size = stat.size, color = "black", angle = stat.angle, -hjust = ifelse(vertical == TRUE, 0.5, 1.1), -vjust = ifelse(vertical == TRUE, 1.1, 0.5) +hjust = stat.just$hjust, +vjust = stat.just$vjust )) # stat$X used here because identical to stat.nolog but has the X. WARNING: no need of order() for labels because box.coord$x set the order. For justification, see https://stackoverflow.com/questions/7263849/what-do-hjust-and-vjust-do-when-making-a-plot-using-ggplot coord.names <- c(coord.names, "stat.display") }else if(stat.disp == "above"){ @@ -9763,6 +9771,9 @@ tempo.center.ref <- "MEAN" # tempo.log.low <- if(diff(y.lim) > 0){stat.coord3[, tempo.center.ref] < 0}else{stat.coord3[, tempo.center.ref] >= 0} # stat.coord3$Y[tempo.log.high] <- stat.coord3[tempo.log.high, tempo.high.ref] # stat.coord3$Y[tempo.log.low] <- stat.coord3[tempo.log.low, tempo.low.ref] +# add distance +stat.coord3$Y <- stat.coord3$Y + diff(y.lim) * stat.dist / 100 +# end add distance # correct median or mean text format if(y.log != "no"){ stat.coord3[, tempo.center.ref] <- ifelse(y.log == "log2", 2, 10)^(stat.coord3[, tempo.center.ref]) @@ -9777,10 +9788,10 @@ mapping = ggplot2::aes_string(x = "x", y = "Y", label = tempo.center.ref), size = stat.size, color = "black", angle = stat.angle, -hjust = ifelse(vertical == TRUE, stat.just$hjust, stat.just$hjust - stat.dist), -vjust = ifelse(vertical == TRUE, stat.just$vjust - stat.dist, stat.just$vjust) +hjust = stat.just$hjust, +vjust = stat.just$vjust )) # WARNING: no need of order() for labels because box.coord$x set the order -coord.names <- c(coord.names, "stat.display.positive") +coord.names <- c(coord.names, "stat.display") # } # if(any(tempo.log.low) == TRUE){ # tempo.stat <- stat.coord3[tempo.log.low,] diff --git a/fun_gg_boxplot.docx b/fun_gg_boxplot.docx index 7612714fb04a1a0af032b5e91ce7d01a39a25d07..fa25d301a30f1a5b17a30eed6d7dc033a048064b 100644 Binary files a/fun_gg_boxplot.docx and b/fun_gg_boxplot.docx differ