```r library('JBUtilities') library('ggplot2') knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
# GGplot2 related function ## Extract legend from ggplot2 object ```r # Create a plot with legend iris_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Width, colour = Species)) + geom_boxplot() # print(iris_plot) # Extract the legend plot iris_plot_legend <- extract_plot_legend(iris_plot, Save = TRUE, device = 'png', width = 5, height = 5)
The png file saved:
library(knitr) knitr::include_graphics('legend.png', dpi = 300)
# Create a plot with legend iris_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Width, colour = Species)) + geom_boxplot() # Extract the legend plot save_plot(iris_plot, output = 'Beamer', width = 5, height = 5)
# Create a plot with legend iris_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Width, colour = Species)) + geom_boxplot() # Print the plot show_ggplot2(iris_plot, ShowPlot = TRUE)
Available color blind palette:
l_cb_palette <- JBUtilities:::l_cb_palette JBUtilities:::list_plotter(l_cb_palette, names = names(l_cb_palette), package_name = 'JBUtilities')
How to use discrete palette with ggplot2
# Create a plot with legend iris_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Width, color = Species)) + scale_color_manual(values = cb_pal(n = 3, name = "cb.grey.pal")) + geom_boxplot() # Print the plot show_ggplot2(iris_plot, ShowPlot = TRUE)
How to use continuois palette with ggplot2
# Create a plot with legend iris_plot2 <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) + scale_color_gradientn(colours = cb_pal(name = "cb.map.blue.red.pal", type = "continuous")) + geom_point() # Print the plot show_ggplot2(iris_plot2, ShowPlot = TRUE)
For one plot :
# Create a plot with legend iris_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Width, color = Species)) + simple_ggplot2_theme() + geom_boxplot() # Print the plot show_ggplot2(iris_plot, ShowPlot = TRUE)
For all plots in a R session:
theme_set(simple_ggplot2_theme()) # Create a plot with legend iris_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Width, color = Species)) + geom_boxplot() show_ggplot2(iris_plot, ShowPlot = TRUE) # Create a plot with legend iris_plot2 <- ggplot(data = iris, aes(x = Species, y = Sepal.Length, color = Species)) + geom_boxplot() # Print the plot show_ggplot2(iris_plot2, ShowPlot = TRUE)
Standardisation of a numeric variable:
## simulation of a covariate covariate <- runif(100, 1, 10) ## standardisation covariate_std <- cov_std(covariate) head(covariate_std) ## the same covariate sampled in a different place but we want the same standardisation covariate_grid <- runif(100, 5, 10) ## use the fonction covariate_grid_std covariate_grid_std <- cov_std_grid(covariate, mean_x_obs = mean(covariate), sd_x_obs = sd(covariate)) head(covariate_grid_std)
Create a vector of month in letter from a numeric vector
## simulation of a vector of month month_numeric <- sample(1:12, size = 10) head(month_numeric) ## transform to letters month_f <- month_number2letter(month_numeric) head(month_f)
In order to fill NA gaps in a vector with the previous value:
## simulation of a vector vec <- c(1, NA, 5, 6, 1, NA, 1) fill_NA_gaps(vec) ## simulation of a vector vec <- c(1, NA, NA, 6, 1, NA, 1) fill_NA_gaps(vec) vec <- c(NA, 2, NA, 6, 1, NA, 1) fill_NA_gaps(vec, firstBack = TRUE)
Compute summarizing statistics of a vector:
## simulation of a vector vec <- runif(100,1,1000) ## compute quantiles compute_q025_q25_q50_q75_q975(vec) ## compute min, max, sd and mean compute_min_mean_sd_max(vec)
Which Operating System is in use.
## function to get the os of the computer where R is executed get_os()
## ## Makes it so you can write this: X <- 2 Y <- 3 cat0("X = ", X, " and Y = ", Y) ## instead of this: cat(paste("X = ", X, " and Y = ", Y, "\n", sep=""))
## install package if not already installed InstallPackages('devtools')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.