diff --git a/cute_little_R_functions.R b/cute_little_R_functions.R index f17ed09556d20b713069b462d83ff2f1445e2a38..fe8afcc9fd503f60b877177334b703a0ba8ed112 100644 --- a/cute_little_R_functions.R +++ b/cute_little_R_functions.R @@ -3564,7 +3564,7 @@ return(window.width) # Check OK: clear to go Apollo -fun_open <- function(pdf = TRUE, pdf.path = "working.dir", pdf.name = "graph", width = 7, height = 7, paper = "special", pdf.overwrite = FALSE, remove.read.only = TRUE, return.output = FALSE){ +fun_open <- function(pdf = TRUE, pdf.path = "working.dir", pdf.name = "graph", width = 7, height = 7, paper = "special", pdf.overwrite = FALSE, rescale = "fixed", remove.read.only = TRUE, return.output = FALSE){ # AIM # open a pdf or screen (GUI) graphic window and return initial graphic parameters # this order can be used: @@ -3579,13 +3579,14 @@ fun_open <- function(pdf = TRUE, pdf.path = "working.dir", pdf.name = "graph", w # REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION # fun_check() # ARGUMENTS: -# pdf: logical. Use pdf display? +# pdf: logical. Use pdf display? If FALSE, a GUI is opened # pdf.path: where the pdf is saved (do not terminate by / or \\). Write "working.dir" if working directory is required (default). Ignored if pdf == FALSE # pdf.name: name of the pdf file containing the graphs (the .pdf extension is added by the function, if not detected in the name end). Ignored if pdf == FALSE # width: width of the window (in inches) # height: height of the window (in inches) # paper: paper argument of the pdf function (paper format). Only used for pdf(). Either "a4", "letter", "legal", "us", "executive", "a4r", "USr" or "special". If "special", means that the paper dimension will be width and height. With another paper format, if width or height is over the size of the paper, width or height will be modified such that the plot is adjusted to the paper dimension (see $dim in the returned list below to see the modified dimensions). Ignored if pdf == FALSE # pdf.overwrite: logical. Existing pdf can be overwritten? . Ignored if pdf == FALSE +# rescale: kind of GUI. Either "R", "fit", or "fixed". Ignored on Mac and Linux OS. See ?windows for details # remove.read.only: logical. remove the read only (R.O.) graphical parameters? If TRUE, the graphical parameters are returned without the R.O. parameters. The returned $ini.par list can be used to set the par() of a new graphical device. If FALSE, graphical parameters are returned with the R.O. parameters, which provides information like text dimension (see ?par() ). The returned $ini.par list can be used to set the par() of a new graphical device, but generate a warning message. Ignored if return.output == FALSE. # return.output: logical. Return output ? If TRUE the output list is displayed # RETURN @@ -3619,6 +3620,7 @@ tempo <- fun_check(data = width, class = "vector", mode = "numeric", length = 1, tempo <- fun_check(data = height, class = "vector", mode = "numeric", length = 1, neg.values = FALSE, fun.name = function.name) ; eval(ee) tempo <- fun_check(data = paper, options = c("a4", "letter", "legal", "us", "executive", "a4r", "USr", "special", "A4", "LETTER", "LEGAL", "US"), length = 1, fun.name = function.name) ; eval(ee) tempo <- fun_check(data =pdf.overwrite, class = "logical", length = 1, fun.name = function.name) ; eval(ee) +tempo <- fun_check(data = rescale, options = c("R", "fit", "fixed"), length = 1, fun.name = function.name) ; eval(ee) tempo <- fun_check(data = remove.read.only, class = "logical", length = 1, fun.name = function.name) ; eval(ee) tempo <- fun_check(data = return.output, class = "logical", length = 1, fun.name = function.name) ; eval(ee) if(any(arg.check) == TRUE){ @@ -3696,7 +3698,7 @@ pdf(width = width, height = height, file=pdf.loc, paper = paper) }else if(pdf == FALSE){ pdf.loc <- NULL if(Sys.info()["sysname"] == "Windows"){ # .Platform$OS.type() only says "unix" for macOS and Linux and "Windows" for Windows -windows(width = width, height = height, rescale="fixed") +windows(width = width, height = height, rescale = rescale) }else if(Sys.info()["sysname"] == "Linux"){ if( ! is.null(open.fail)){ tempo.cat <- "\n\n================\n\nERROR IN fun_open()\nTHIS FUNCTION CANNOT OPEN GUI ON LINUX OR NON MACOS UNIX SYSTEM (X GRAPHIC INTERFACE HAS TO BE SET)\nTO OVERCOME THIS, PLEASE SET pdf ARGUMENT TO TRUE AND RERUN\n\n================\n\n" @@ -4865,7 +4867,7 @@ fun_gg_just <- function(angle, axis){ # AIM # provide correct justification for axis labeling, depending on the chosen angle # ARGUMENTS -# angle: integer value of the text angle for the axis labels. 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. +# angle: integer value of the text angle for the axis labels, 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. # axis: which axis for? Either "x" or "y" # REQUIRED PACKAGES # none diff --git a/cute_little_R_functions.docx b/cute_little_R_functions.docx index 02cb918a3296ca42961294904b3444b8d4c1aead..fb395f7db46b234bda0e5410a9c5898e7f68efc1 100644 Binary files a/cute_little_R_functions.docx and b/cute_little_R_functions.docx differ diff --git a/scatter_examples.R b/scatter_examples.R index 32abcf3be4c553e97d46b698c9c33543ce7c8a48..c5a11526abc37483c6ba01266f6f1043742a6b6e 100644 --- a/scatter_examples.R +++ b/scatter_examples.R @@ -2,49 +2,75 @@ ### Data set set.seed(1) -# straight relationship +# Curves obs1 <- data.frame( - Km = 2:7, - Time = (2:7)^2, - Car = c("TUTUT", "TUTUT", "TUTUT", "WIM-WIM", "WIM-WIM", "WIM-WIM"), + Km = 2:7, + Time = (2:7)^2, + Car = c("TUUT", "TUUT", "TUUT", "WIM-WIM", "WIM-WIM", "WIM-WIM"), Color1 = rep(c("coral", "lightblue"), each = 3), stringsAsFactors = TRUE ) -# first scatter +# First scattering obs2 <- data.frame( - Km = rnorm(1000, 20, 3), - Time = rnorm(1000, 20, 3), + Km = rnorm(1000, 20, 3), + Time = rnorm(1000, 20, 3), Animal = rep(c("CAT", "DOG"), 500), - Color1 = rep(c("coral", "lightblue"), times = 500), + Color2 = rep(c("darkblue", "darkred"), times = 500), stringsAsFactors = TRUE ) -# second scatter +# Second scattering obs3 <- data.frame( - Km = rnorm(1000, 30, 3), - Time = rnorm(1000, 30, 3), - Animal = rep(c("LION", "ZEBRA"), 500), - Color1 = rep(1:2, times = 500), + Distance = rnorm(1000, 30, 3), + Time_lapse = rnorm(1000, 30, 3), + Beast = rep(c("LION", "ZEBRA"), 500), + Color3 = rep(3:4, times = 500), stringsAsFactors = TRUE ) set.seed(NULL) fun_info(obs1) fun_info(obs2) fun_info(obs3) +fun_open(pdf = FALSE, width = 5, height = 5, rescale = "R") + + + + + + + + ## Mandatory arguments ### single dataset +fun_gg_scatter(data1 = obs1, x = "Km", y = "Time") +### single dataset submitted as list fun_gg_scatter( - data1 = obs1, - x = "Km", - y = "Time" + data1 = list(obs1), + x = list("Km"), + y = list("Time") ) -### single dataset submitted as list +### multiple dataset +# Elements in list have names (L1, L2, etc.) just to show the correspondence between the arguments data1, x, y, categ, etc. +# Of note, no legend is displayed. For a legend, please merge the two data frames, or create a new categ column in each data frame (see below) fun_gg_scatter( - data1 = list(obs1), - x = list("Km"), - y = list("Time") + data1 = list( + L1 = obs2, + L2 = obs3 + ), + x = list( + L1 = "Km", + L2 = "Distance" + ), + y = list( + L1 = "Time", + L2 = "Time_lapse" + ) ) -# multiple dataset. Elements in list have names (L1, L2, etc.) just to show the correspondence between the arguments data1, x, y, categ, etc. + +### The categ argument to generate legends. Create a categ column in data frames if a legend is required +### single dataset +fun_gg_scatter(data1 = obs1, x = "Km", y = "Time", categ = "Car") +### multiple datasets fun_gg_scatter( data1 = list( L1 = obs2, @@ -52,41 +78,179 @@ fun_gg_scatter( ), x = list( L1 = "Km", - L2 = "Km" + L2 = "Distance" ), y = list( L1 = "Time", - L2 = "Time" + L2 = "Time_lapse" + ), + categ = list( + L1 = "Animal", + L2 = "Beast" ) ) +# Another way for multiple datasets representation, depending on legend required +obs4 <- data.frame( + Km = c(obs2$Km, obs3$Distance), + Time = c(obs2$Time, obs3$Time_lapse), + Type = c(as.character(obs2$Animal), as.character(obs3$Beast)), + stringsAsFactors = TRUE +) +fun_gg_scatter(data1 = obs4, x = "Km", y = "Time", categ = "Type") +### Order in the legend +### single dataset +fun_gg_scatter( + data1 = obs1, + x = "Km", + y = "Time", + categ = "Car", + categ.class.order = c("WIM-WIM", "TUUT") +) +### multiple datasets +fun_gg_scatter( + data1 = list( + L1 = obs2, + L2 = obs3 + ), + x = list( + L1 = "Km", + L2 = "Distance" + ), + y = list( + L1 = "Time", + L2 = "Time_lapse" + ), + categ = list( + L1 = "Animal", + L2 = "Beast" + ), + categ.class.order = list( + c("DOG", "CAT"), + c("ZEBRA", "LION") + ) +) + + + +### Colors +### single dataset +# Using a single color value +fun_gg_scatter(data1 = obs1, x = "Km", y = "Time", color = "black") # replace "black" by 2 to test integer values +# Using one color value par class of Categ1 +fun_gg_scatter( + data1 = obs1, + x = "Km", + y = "Time", + categ = "Car", # mandatory categ argument when several color required + color = c("darkblue", "darkgreen") # replace c("darkblue", "darkgreen") by 2:3 to test integer values +) +# Using a vector of color values (e.g., data frame column), with respect of the correspondence between Car and Color1 columns +fun_gg_scatter( + data1 = obs1, + x = "Km", + y = "Time", + categ = "Car", # mandatory categ argument when several color required + color = obs1$Color1 # # replace obs1$Color1 by as.numeric(obs1$Color1) to test integer values +) +### multiple datasets +# single color -> same color for all the datasets and all the classes of categ if non NULL +fun_gg_scatter( + data1 = list( + L1 = obs2, + L2 = obs3 + ), + x = list( + L1 = "Km", + L2 = "Distance" + ), + y = list( + L1 = "Time", + L2 = "Time_lapse" + ), + categ = list( # mandatory categ argument when several color required per dataset + L1 = "Animal", + L2 = "Beast" + ), + color = "darkblue" # replace "darkblue" by 2 to test integer values +) + +# list of single colors -> each dataset with the same color +fun_gg_scatter( + data1 = list( + L1 = obs2, + L2 = obs3 + ), + x = list( + L1 = "Km", + L2 = "Distance" + ), + y = list( + L1 = "Time", + L2 = "Time_lapse" + ), + categ = list( # mandatory categ argument when several color required per dataset + L1 = "Animal", + L2 = "Beast" + ), + color = list( + L1 = "darkblue", # replace "darkblue" by 2 to test integer values + L2 = "darkgreen" # replace "darkgreen" by 3 to test integer values + ) +) +# list of single colors for each class of the categ argument +fun_gg_scatter( + data1 = list( + L1 = obs2, + L2 = obs3 + ), + x = list( + L1 = "Km", + L2 = "Distance" + ), + y = list( + L1 = "Time", + L2 = "Time_lapse" + ), + categ = list( # mandatory categ argument when several color required per dataset + L1 = "Animal", + L2 = "Beast" + ), + color = list( + L1 = c("darkblue", "darkred"), # replace c("darkblue", "darkred") by 2:3 to test integer values + L2 = c("darkgreen", "darkorange") # replace c("darkgreen", "darkviolet") by 4:5 to test integer values + ) +) +# list of vectors of color values (e.g., data frame column), with respect of the correspondence between the categorical and the color columns +fun_gg_scatter( + data1 = list( + L1 = obs2, + L2 = obs3 + ), + x = list( + L1 = "Km", + L2 = "Distance" + ), + y = list( + L1 = "Time", + L2 = "Time_lapse" + ), + categ = list( # mandatory categ argument when several color required per dataset + L1 = "Animal", + L2 = "Beast" + ), + color = list( + L1 = obs2$Color2, # Replace by NULL to see that different levels of grey is used per dataset (not per class of the categ argument) + L2 = obs3$Color3 # color of integer value can be used. Replace by NULL to see that different levels of grey is used per dataset (not per class of the categ argument) + ) +) -### Changing the order of the boxes -# separate boxes -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", categ.class.order = list(c("DOG", "CAT"))) -# grouped boxes -fun_gg_boxplot(data1 = obs1, y = "Time", categ = c("Categ1", "Categ2"), categ.class.order = list(c("DOG", "CAT"), c("D", "C", "B", "A"))) -### Box color -# Using a single color value -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", categ.color = "coral") -# Using one color value par class of Categ1 -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", categ.color = c("coral", "lightblue")) -# Using a vector of color values (e.g., data frame column), with respect of the correspondence between Categ1 and box.color columns -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", categ.color = obs1$Color1) -# Using integers instead of color strings -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", categ.color = 1) -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", categ.color = 1:2) -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", categ.color = as.numeric(obs1$Color1)) -# With grouped boxes, we generate the same effects but for the second category -fun_gg_boxplot(data1 = obs1, y = "Time", categ = c("Categ1", "Categ2"), categ.color = "coral") -fun_gg_boxplot(data1 = obs1, y = "Time", categ = c("Categ1", "Categ2"), categ.color = 1:4) -fun_gg_boxplot(data1 = obs1, y = "Time", categ = c("Categ1", "Categ2"), categ.color = obs1$Color2) ### Other parameters of boxes fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", @@ -128,12 +292,7 @@ dot.categ.class.order = c("D", "A", "C", "B"), dot.legend.name = "ANIMAL GROUP" ) -### Tidy or random dot spreading -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", -dot.tidy = TRUE, -dot.tidy.bin.nb = 50, # from 0 to Inf. Only if dot.tidy = TRUE -dot.jitter = 0.5 # from 0 to 1. Only if dot.tidy = FALSE -) + ### Other parameters of dots fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", @@ -162,18 +321,7 @@ y.top.extra.margin = 0, y.bottom.extra.margin = 0, ) -### Stat numbers above boxes -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", -stat.disp = "above", # try "top" -stat.disp.mean = FALSE, -stat.size = 4, -stat.dist = 2 -) -### Plot orientation -fun_gg_boxplot(data1 = obs1, y = "Time", categ = "Categ1", -vertical = FALSE # with log2 and log10 scales, horizontal orientation is blocked because of a bug in ggplot2 (https://github.com/tidyverse/ggplot2/issues/881) -) ### Text management