diff --git a/CRAN-RELEASE b/CRAN-RELEASE new file mode 100644 index 00000000..925c0b48 --- /dev/null +++ b/CRAN-RELEASE @@ -0,0 +1,2 @@ +This package was submitted to CRAN on 2020-06-27. +Once it is accepted, delete this file and tag the release (commit 7ca7d83936). diff --git a/DESCRIPTION b/DESCRIPTION index 97175ab7..b1cfeba3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: ggpubr Type: Package Title: 'ggplot2' Based Publication Ready Plots -Version: 0.3.0.999 -Date: 2020-05-03 +Version: 0.4.0 +Date: 2020-06-27 Authors@R: c( person("Alboukadel", "Kassambara", role = c("aut", "cre"), email = "alboukadel.kassambara@gmail.com")) Description: The 'ggplot2' package is excellent and flexible for elegant data @@ -33,7 +33,7 @@ Imports: glue, polynom, rlang, - rstatix (>= 0.4.0), + rstatix (>= 0.6.0), tibble, magrittr Suggests: @@ -118,4 +118,5 @@ Collate: 'text_grob.R' 'theme_pubr.R' 'theme_transparent.R' + 'utils-geom-signif.R' 'utils-pipe.R' diff --git a/NEWS.md b/NEWS.md index 210d3f7f..5b4e1960 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,5 @@ -# ggpubr 0.3.0.999 +# ggpubr 0.4.0 -To DO: merging two legends in ggarrange, discussion: https://community.rstudio.com/t/how-to-combine-two-separate-legends-side-by-side/9480/5 ## New features diff --git a/R/annotate_figure.R b/R/annotate_figure.R index 4eb74462..d601ee4e 100644 --- a/R/annotate_figure.R +++ b/R/annotate_figure.R @@ -3,7 +3,7 @@ NULL #'Annotate Arranged Figure #' #'@description Annotate figures including: i) ggplots, ii) arranged ggplots from -#' \code{\link{ggarrange}()}, \code{\link[gridExtra]{grid.arrange}()} and +#' \code{\link{ggarrange}()}, \code{\link[gridExtra:arrangeGrob]{grid.arrange}()} and #' \code{\link[cowplot]{plot_grid}()}. #'@param p (arranged) ggplots. #'@param top,bottom,left,right optional string, or grob. diff --git a/R/as_ggplot.R b/R/as_ggplot.R index bb5b5c0b..8979ac25 100644 --- a/R/as_ggplot.R +++ b/R/as_ggplot.R @@ -2,11 +2,11 @@ NULL #' Storing grid.arrange() arrangeGrob() and plots #' -#' @description Transform the output of \code{\link[gridExtra]{arrangeGrob}()} -#' and \code{\link[gridExtra]{grid.arrange}()} to a an object of class ggplot. +#' @description Transform the output of \code{\link[gridExtra:arrangeGrob]{arrangeGrob}()} +#' and \code{\link[gridExtra:arrangeGrob]{grid.arrange}()} to a an object of class ggplot. #' @param x an object of class gtable or grob as returned by the functions -#' \code{\link[gridExtra]{arrangeGrob}()} and -#' \code{\link[gridExtra]{grid.arrange}()}. +#' \code{\link[gridExtra:arrangeGrob]{arrangeGrob}()} and +#' \code{\link[gridExtra:arrangeGrob]{grid.arrange}()}. #' @return an object of class ggplot. #' #' @examples diff --git a/R/font.R b/R/font.R index fe19616d..d3188ff3 100644 --- a/R/font.R +++ b/R/font.R @@ -2,7 +2,7 @@ #' #'@description Change the appearance of the main title, subtitle, caption, axis #' labels and text, as well as the legend title and texts. Wrapper around -#' \code{\link[ggplot2]{element_text}()}. +#' \code{\link[ggplot2:element]{element_text}()}. #' #'@param object character string specifying the plot components. Allowed values #' include: \itemize{ \item \code{"title"} for the main title \item @@ -23,7 +23,7 @@ #' "bold.italic"}). #'@param family the font family. #'@param ... other arguments to pass to the function -#' \code{\link[ggplot2]{element_text}()}. +#' \code{\link[ggplot2:element]{element_text}()}. #' #'@examples #'# Load data diff --git a/R/get_breaks.R b/R/get_breaks.R index 454bb2c2..197f669b 100644 --- a/R/get_breaks.R +++ b/R/get_breaks.R @@ -1,8 +1,8 @@ #' Easy Break Creation for Numeric Axes #' #' @description Creates breaks for numeric axes to be used in the functions -#' \code{\link[ggplot2]{scale_x_continuous}()} and -#' \code{\link[ggplot2]{scale_y_continuous}()}. Can be used to increase the +#' \code{\link[ggplot2:scale_continuous]{scale_x_continuous}()} and +#' \code{\link[ggplot2:scale_continuous]{scale_y_continuous}()}. Can be used to increase the #' number of x and y ticks by specifying the option \code{n}. It's also #' possible to control axis breaks by specifying a step between ticks. For #' example, if \code{by = 5}, a tick mark is shown on every 5. diff --git a/R/get_palette.R b/R/get_palette.R index b59a10ed..57ad16d0 100644 --- a/R/get_palette.R +++ b/R/get_palette.R @@ -4,7 +4,7 @@ #' palettes and custom color palettes. Useful to extend RColorBrewer and ggsci to support more colors. #'@param palette Color palette. Allowed values include: \itemize{ \item #' \strong{Grey color palettes}: "grey" or "gray"; \item \strong{RColorBrewer -#' palettes}, see \code{\link[RColorBrewer]{brewer.pal}} and details section. +#' palettes}, see \code{\link[RColorBrewer:ColorBrewer]{brewer.pal}} and details section. #' Examples of palette names include: "RdBu", "Blues", "Dark2", "Set2", ...; #' \item \strong{Custom color palettes}. For example, palette = c("#00AFBB", #' "#E7B800", "#FC4E07"); \item \strong{ggsci scientific journal palettes}, diff --git a/R/ggscatter.R b/R/ggscatter.R index dfb05ba3..e92deaec 100644 --- a/R/ggscatter.R +++ b/R/ggscatter.R @@ -29,7 +29,7 @@ NULL #' #' \itemize{ \item \code{"convex"}: plot convex hull of a set o points. \item #' \code{"confidence"}: plot confidence ellipses arround group mean points as -#' \code{\link[FactoMineR]{coord.ellipse}()}[in FactoMineR]. \item \code{"t"}: +#' \code{FactoMineR::coord.ellipse()}. \item \code{"t"}: #' assumes a multivariate t-distribution. \item \code{"norm"}: assumes a #' multivariate normal distribution. \item \code{"euclid"}: draws a circle with #' the radius equal to level, representing the euclidean distance from the diff --git a/R/ggscatterhist.R b/R/ggscatterhist.R index 0b18d53e..b004678d 100644 --- a/R/ggscatterhist.R +++ b/R/ggscatterhist.R @@ -12,7 +12,7 @@ NULL #'@param margin.plot the type of the marginal plot. Default is "hist". #'@param margin.params parameters to be applied to the marginal plots. #'@param margin.ggtheme the theme of the marginal plot. Default is -#' \code{\link[ggplot2]{theme_void}()}. +#' \code{\link[ggplot2:ggtheme]{theme_void}()}. #'@param margin.space logical value. If TRUE, adds space between the main plot #' and the marginal plot. #'@param bins Number of histogram bins. Defaults to 30. Pick a better value that diff --git a/R/ggsummarystats.R b/R/ggsummarystats.R index 912a4074..cf3cc7b9 100644 --- a/R/ggsummarystats.R +++ b/R/ggsummarystats.R @@ -4,7 +4,7 @@ NULL #' GGPLOT with Summary Stats Table Under the Plot #' #' @description Create a ggplot with summary stats (n, median, mean, iqr) table -#' under the plot. +#' under the plot. Read more: \href{https://www.datanovia.com/en/blog/how-to-create-a-beautiful-plots-in-r-with-summary-statistics-labels/}{How to Create a Beautiful Plots in R with Summary Statistics Labels}. #' @inheritParams ggboxplot #' @param digits integer indicating the number of decimal places (round) to be #' used. @@ -79,14 +79,7 @@ NULL #' #' # Facet #' #:::::::::::::::::::::::::::::::::::::::::::::::: -#' -#' ggsummarystats( -#' df, x = "dose", y = "len", -#' ggfunc = ggboxplot, add = "jitter", -#' color = "dose", palette = "npg", -#' facet.by = c("supp", "qc") -#' ) -#' # Specify labeller +#' # Specify free.panels = TRUE for free panels #' ggsummarystats( #' df, x = "dose", y = "len", #' ggfunc = ggboxplot, add = "jitter", @@ -95,15 +88,6 @@ NULL #' labeller = "label_both" #' ) #' -#' # Free panels -#' ggsummarystats( -#' df, x = "dose", y = "len", -#' ggfunc = ggboxplot, add = "jitter", -#' color = "dose", palette = "npg", -#' facet.by = c("supp", "qc"), -#' free.panels = TRUE, labeller = "label_both" -#' ) -#' #' @describeIn ggsummarystats Create a table of summary stats #' @export ggsummarytable <- function(data, x, y, digits = 0, size = 3, color = "black", palette = NULL, diff --git a/R/reexports.R b/R/reexports.R index 6988ba45..c89be3af 100644 --- a/R/reexports.R +++ b/R/reexports.R @@ -4,9 +4,6 @@ dplyr::mutate #' @importFrom dplyr group_by #' @export dplyr::group_by -#' @importFrom ggsignif geom_signif -#' @export -ggsignif::geom_signif #' @importFrom rstatix get_summary_stats #' @export rstatix::get_summary_stats diff --git a/R/rotate_axis_text.R b/R/rotate_axis_text.R index b86890d9..20a07f50 100644 --- a/R/rotate_axis_text.R +++ b/R/rotate_axis_text.R @@ -10,7 +10,7 @@ #'@param hjust horizontal justification (in [0, 1]). #'@param vjust vertical justification (in [0, 1]). #'@param ... other arguments to pass to the function -#' \code{\link[ggplot2]{element_text}()}. +#' \code{\link[ggplot2:element]{element_text}()}. #' #'@examples #'# Load data diff --git a/R/stat_central_tendency.R b/R/stat_central_tendency.R index 6babe44d..e5679878 100644 --- a/R/stat_central_tendency.R +++ b/R/stat_central_tendency.R @@ -13,7 +13,7 @@ NULL #' For continuous variable, we can consider using mean or median as the #' measures of the central tendency. #' @inheritParams ggplot2::layer -#' @param ... other arguments to pass to \code{\link[ggplot2]{geom_line}}. +#' @param ... other arguments to pass to \code{\link[ggplot2:geom_path]{geom_line}}. #' @param na.rm If FALSE (the default), removes missing values with a warning. #' If TRUE silently removes missing values. #' @param type the type of central tendency measure to be used. Possible values diff --git a/R/stat_compare_means.R b/R/stat_compare_means.R index a66f80a7..75002654 100644 --- a/R/stat_compare_means.R +++ b/R/stat_compare_means.R @@ -41,7 +41,7 @@ NULL #'@param step.increase numeric vector with the increase in fraction of total #' height for every additional comparison to minimize overlap. #'@param ... other arguments to pass to \code{\link[ggplot2]{geom_text}} or -#' \code{\link[ggplot2]{geom_label}}. +#' \code{\link[ggplot2:geom_text]{geom_label}}. #'@param na.rm If FALSE (the default), removes missing values with a warning. If #' TRUE silently removes missing values. #'@seealso \code{\link{compare_means}} diff --git a/R/stat_conf_ellipse.R b/R/stat_conf_ellipse.R index 32f5353b..0e8219f8 100644 --- a/R/stat_conf_ellipse.R +++ b/R/stat_conf_ellipse.R @@ -2,7 +2,7 @@ NULL #' Plot confidence ellipses. #' @description Plot confidence ellipses around barycenters. The method for -#' computing confidence ellipses has been modified from FactoMineR::coord.ellipse. +#' computing confidence ellipses has been modified from \code{FactoMineR::coord.ellipse()}. #' @inheritParams ggplot2::layer #' @inheritParams ggplot2::stat_ellipse #' @param level confidence level used to construct the ellipses. By diff --git a/R/stat_cor.R b/R/stat_cor.R index 74e7c3c1..573e6890 100644 --- a/R/stat_cor.R +++ b/R/stat_cor.R @@ -39,7 +39,7 @@ NULL #' precision for the p-value. Default is NULL. Use (e.g.) 0.0001 to show 4 #' decimal places of precision. If specified, then \code{p.digits} is ignored. #'@param ... other arguments to pass to \code{\link[ggplot2]{geom_text}} or -#' \code{\link[ggplot2]{geom_label}}. +#' \code{\link[ggplot2:geom_text]{geom_label}}. #'@param na.rm If FALSE (the default), removes missing values with a warning. If #' TRUE silently removes missing values. #'@seealso \code{\link{ggscatter}} diff --git a/R/stat_overlay_normal_density.R b/R/stat_overlay_normal_density.R index a3ce4012..2647ed89 100644 --- a/R/stat_overlay_normal_density.R +++ b/R/stat_overlay_normal_density.R @@ -6,7 +6,7 @@ NULL #' density distribution of 'x'. This is useful for visually inspecting the #' degree of deviance from normality. #' @inheritParams ggplot2::layer -#' @param ... other arguments to pass to \code{\link[ggplot2]{geom_line}}. +#' @param ... other arguments to pass to \code{\link[ggplot2:geom_path]{geom_line}}. #' @param na.rm If FALSE (the default), removes missing values with a warning. #' If TRUE silently removes missing values. #' @seealso \code{\link{ggdensity}} diff --git a/R/stat_regline_equation.R b/R/stat_regline_equation.R index e95aee6a..ac4f179c 100644 --- a/R/stat_regline_equation.R +++ b/R/stat_regline_equation.R @@ -21,7 +21,7 @@ NULL #' for absolute positioning of the label. If too short they will be recycled. #'@param output.type character One of "expression", "latex" or "text". #'@param ... other arguments to pass to \code{\link[ggplot2]{geom_text}} or -#' \code{\link[ggplot2]{geom_label}}. +#' \code{\link[ggplot2:geom_text]{geom_label}}. #'@param na.rm If FALSE (the default), removes missing values with a warning. If #' TRUE silently removes missing values. #'@seealso \code{\link{ggscatter}} diff --git a/R/text_grob.R b/R/text_grob.R index 9b71fc6b..a7512697 100644 --- a/R/text_grob.R +++ b/R/text_grob.R @@ -3,7 +3,7 @@ NULL #' Create a Text Graphical object #' @description Create easily a customized text grob (graphical object). Wrapper around -#' \code{\link[grid]{textGrob}}. +#' \code{\link[grid:grid.text]{textGrob}}. #' @inheritParams grid::grid.text #' @param color text font color. #' @param face font face. Allowed values include one of \code{"plain", "bold", @@ -11,7 +11,7 @@ NULL #' @param size font size (e.g.: size = 12) #' @param lineheight line height (e.g.: lineheight = 2). #' @param family font family. -#' @param ... other arguments passed to \link[grid]{textGrob}. +#' @param ... other arguments passed to \link[grid:grid.text]{textGrob}. #' @return a text grob. #' @examples #'text <- paste("iris data set gives the measurements in cm", diff --git a/R/utils-geom-signif.R b/R/utils-geom-signif.R new file mode 100644 index 00000000..0f9c1607 --- /dev/null +++ b/R/utils-geom-signif.R @@ -0,0 +1,10 @@ +#' geom_signif exported from ggsignif package +#' +#' See \code{ggsignif::\link[ggsignif:stat_signif]{geom_signif}} for details. +#' +#' @name geom_signif +#' @rdname geom_signif +#' @keywords internal +#' @export +#' @importFrom ggsignif geom_signif +NULL diff --git a/_pkgdown.yml b/_pkgdown.yml index f6591dc7..06673f52 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -86,6 +86,10 @@ reference: - ggexport - annotate_figure - background_image + - title: Make Programming Easy with GGPlot2 + contents: + - geom_exec + - create_aes - title: Helpers contents: - facet @@ -93,8 +97,6 @@ reference: - ggadd - get_palette - as_ggplot - - geom_exec - - create_aes - title: Data contents: - diff_express diff --git a/cran-comments.md b/cran-comments.md index 43092d71..57893609 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,5 +1,5 @@ ## Test environments -* local OS X install, R 3.3.2 +* local OS X install, R 3.6.0 * win-builder * Travis @@ -13,5 +13,5 @@ All packages that I could install passed. ## Update -This is an update version 0.3.0 (see NEWS.md). +This is an update version 0.4.0 (see NEWS.md). diff --git a/docs/404.html b/docs/404.html index e69a2028..22b5a880 100644 --- a/docs/404.html +++ b/docs/404.html @@ -8,23 +8,29 @@
install.packages("ggpubr")
# Install
-if(!require(devtools)) install.packages("devtools")
-devtools::install_github("kassambara/ggpubr")
# Install +if(!require(devtools)) install.packages("devtools") +devtools::install_github("kassambara/ggpubr")
library(ggpubr)
-#> Loading required package: ggplot2
-#> Loading required package: magrittr
-# Create some data format
-# :::::::::::::::::::::::::::::::::::::::::::::::::::
-set.seed(1234)
-wdata = data.frame(
- sex = factor(rep(c("F", "M"), each=200)),
- weight = c(rnorm(200, 55), rnorm(200, 58)))
-head(wdata, 4)
-#> sex weight
-#> 1 F 53.79293
-#> 2 F 55.27743
-#> 3 F 56.08444
-#> 4 F 52.65430
-
-# Density plot with mean lines and marginal rug
-# :::::::::::::::::::::::::::::::::::::::::::::::::::
-# Change outline and fill colors by groups ("sex")
-# Use custom palette
-ggdensity(wdata, x = "weight",
- add = "mean", rug = TRUE,
- color = "sex", fill = "sex",
- palette = c("#00AFBB", "#E7B800"))
library(ggpubr) +#> Loading required package: ggplot2 +#> Loading required package: magrittr +# Create some data format +# ::::::::::::::::::::::::::::::::::::::::::::::::::: +set.seed(1234) +wdata = data.frame( + sex = factor(rep(c("F", "M"), each=200)), + weight = c(rnorm(200, 55), rnorm(200, 58))) +head(wdata, 4) +#> sex weight +#> 1 F 53.79293 +#> 2 F 55.27743 +#> 3 F 56.08444 +#> 4 F 52.65430 + +# Density plot with mean lines and marginal rug +# ::::::::::::::::::::::::::::::::::::::::::::::::::: +# Change outline and fill colors by groups ("sex") +# Use custom palette +ggdensity(wdata, x = "weight", + add = "mean", rug = TRUE, + color = "sex", fill = "sex", + palette = c("#00AFBB", "#E7B800"))
# Histogram plot with mean lines and marginal rug
-# :::::::::::::::::::::::::::::::::::::::::::::::::::
-# Change outline and fill colors by groups ("sex")
-# Use custom color palette
-gghistogram(wdata, x = "weight",
- add = "mean", rug = TRUE,
- color = "sex", fill = "sex",
- palette = c("#00AFBB", "#E7B800"))
# Histogram plot with mean lines and marginal rug +# ::::::::::::::::::::::::::::::::::::::::::::::::::: +# Change outline and fill colors by groups ("sex") +# Use custom color palette +gghistogram(wdata, x = "weight", + add = "mean", rug = TRUE, + color = "sex", fill = "sex", + palette = c("#00AFBB", "#E7B800"))
# Load data
-data("ToothGrowth")
-df <- ToothGrowth
-head(df, 4)
-#> len supp dose
-#> 1 4.2 VC 0.5
-#> 2 11.5 VC 0.5
-#> 3 7.3 VC 0.5
-#> 4 5.8 VC 0.5
-
-# Box plots with jittered points
-# :::::::::::::::::::::::::::::::::::::::::::::::::::
-# Change outline colors by groups: dose
-# Use custom color palette
-# Add jitter points and change the shape by groups
- p <- ggboxplot(df, x = "dose", y = "len",
- color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
- add = "jitter", shape = "dose")
- p
# Load data +data("ToothGrowth") +df <- ToothGrowth +head(df, 4) +#> len supp dose +#> 1 4.2 VC 0.5 +#> 2 11.5 VC 0.5 +#> 3 7.3 VC 0.5 +#> 4 5.8 VC 0.5 + +# Box plots with jittered points +# ::::::::::::::::::::::::::::::::::::::::::::::::::: +# Change outline colors by groups: dose +# Use custom color palette +# Add jitter points and change the shape by groups + p <- ggboxplot(df, x = "dose", y = "len", + color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"), + add = "jitter", shape = "dose") + p
- # Add p-values comparing groups
- # Specify the comparisons you want
-my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
-p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
- stat_compare_means(label.y = 50) # Add global p-value
+ # Add p-values comparing groups + # Specify the comparisons you want +my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") ) +p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value + stat_compare_means(label.y = 50) # Add global p-value
-
-# Violin plots with box plots inside
-# :::::::::::::::::::::::::::::::::::::::::::::::::::
-# Change fill color by groups: dose
-# add boxplot with white fill color
-ggviolin(df, x = "dose", y = "len", fill = "dose",
- palette = c("#00AFBB", "#E7B800", "#FC4E07"),
- add = "boxplot", add.params = list(fill = "white"))+
- stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ # Add significance levels
- stat_compare_means(label.y = 50) # Add global the p-value
+ +# Violin plots with box plots inside +# ::::::::::::::::::::::::::::::::::::::::::::::::::: +# Change fill color by groups: dose +# add boxplot with white fill color +ggviolin(df, x = "dose", y = "len", fill = "dose", + palette = c("#00AFBB", "#E7B800", "#FC4E07"), + add = "boxplot", add.params = list(fill = "white"))+ + stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ # Add significance levels + stat_compare_means(label.y = 50) # Add global the p-value
Load and prepare data:
-# Load data
-data("mtcars")
-dfm <- mtcars
-# Convert the cyl variable to a factor
-dfm$cyl <- as.factor(dfm$cyl)
-# Add the name colums
-dfm$name <- rownames(dfm)
-# Inspect the data
-head(dfm[, c("name", "wt", "mpg", "cyl")])
-#> name wt mpg cyl
-#> Mazda RX4 Mazda RX4 2.620 21.0 6
-#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6
-#> Datsun 710 Datsun 710 2.320 22.8 4
-#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6
-#> Hornet Sportabout Hornet Sportabout 3.440 18.7 8
-#> Valiant Valiant 3.460 18.1 6
# Load data +data("mtcars") +dfm <- mtcars +# Convert the cyl variable to a factor +dfm$cyl <- as.factor(dfm$cyl) +# Add the name colums +dfm$name <- rownames(dfm) +# Inspect the data +head(dfm[, c("name", "wt", "mpg", "cyl")]) +#> name wt mpg cyl +#> Mazda RX4 Mazda RX4 2.620 21.0 6 +#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6 +#> Datsun 710 Datsun 710 2.320 22.8 4 +#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6 +#> Hornet Sportabout Hornet Sportabout 3.440 18.7 8 +#> Valiant Valiant 3.460 18.1 6
Change the fill color by the grouping variable “cyl”. Sorting will be done globally, but not by groups.
-ggbarplot(dfm, x = "name", y = "mpg",
- fill = "cyl", # change fill color by cyl
- color = "white", # Set bar border colors to white
- palette = "jco", # jco journal color palett. see ?ggpar
- sort.val = "desc", # Sort the value in dscending order
- sort.by.groups = FALSE, # Don't sort inside each group
- x.text.angle = 90 # Rotate vertically x axis texts
- )
ggbarplot(dfm, x = "name", y = "mpg", + fill = "cyl", # change fill color by cyl + color = "white", # Set bar border colors to white + palette = "jco", # jco journal color palett. see ?ggpar + sort.val = "desc", # Sort the value in dscending order + sort.by.groups = FALSE, # Don't sort inside each group + x.text.angle = 90 # Rotate vertically x axis texts + )
Sort bars inside each group. Use the argument sort.by.groups = TRUE.
-ggbarplot(dfm, x = "name", y = "mpg",
- fill = "cyl", # change fill color by cyl
- color = "white", # Set bar border colors to white
- palette = "jco", # jco journal color palett. see ?ggpar
- sort.val = "asc", # Sort the value in dscending order
- sort.by.groups = TRUE, # Sort inside each group
- x.text.angle = 90 # Rotate vertically x axis texts
- )
ggbarplot(dfm, x = "name", y = "mpg", + fill = "cyl", # change fill color by cyl + color = "white", # Set bar border colors to white + palette = "jco", # jco journal color palett. see ?ggpar + sort.val = "asc", # Sort the value in dscending order + sort.by.groups = TRUE, # Sort inside each group + x.text.angle = 90 # Rotate vertically x axis texts + )
The deviation graph shows the deviation of quantitatives values to a reference value. In the R code below, we’ll plot the mpg z-score from the mtcars dataset.
Calculate the z-score of the mpg data:
-# Calculate the z-score of the mpg data
-dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
-dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"),
- levels = c("low", "high"))
-# Inspect the data
-head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])
-#> name wt mpg mpg_z mpg_grp cyl
-#> Mazda RX4 Mazda RX4 2.620 21.0 0.1508848 high 6
-#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 0.1508848 high 6
-#> Datsun 710 Datsun 710 2.320 22.8 0.4495434 high 4
-#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 0.2172534 high 6
-#> Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345 low 8
-#> Valiant Valiant 3.460 18.1 -0.3302874 low 6
# Calculate the z-score of the mpg data +dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg) +dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), + levels = c("low", "high")) +# Inspect the data +head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")]) +#> name wt mpg mpg_z mpg_grp cyl +#> Mazda RX4 Mazda RX4 2.620 21.0 0.1508848 high 6 +#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 0.1508848 high 6 +#> Datsun 710 Datsun 710 2.320 22.8 0.4495434 high 4 +#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 0.2172534 high 6 +#> Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345 low 8 +#> Valiant Valiant 3.460 18.1 -0.3302874 low 6
Create an ordered barplot, colored according to the level of mpg:
-ggbarplot(dfm, x = "name", y = "mpg_z",
- fill = "mpg_grp", # change fill color by mpg_level
- color = "white", # Set bar border colors to white
- palette = "jco", # jco journal color palett. see ?ggpar
- sort.val = "asc", # Sort the value in ascending order
- sort.by.groups = FALSE, # Don't sort inside each group
- x.text.angle = 90, # Rotate vertically x axis texts
- ylab = "MPG z-score",
- xlab = FALSE,
- legend.title = "MPG Group"
- )
ggbarplot(dfm, x = "name", y = "mpg_z", + fill = "mpg_grp", # change fill color by mpg_level + color = "white", # Set bar border colors to white + palette = "jco", # jco journal color palett. see ?ggpar + sort.val = "asc", # Sort the value in ascending order + sort.by.groups = FALSE, # Don't sort inside each group + x.text.angle = 90, # Rotate vertically x axis texts + ylab = "MPG z-score", + xlab = FALSE, + legend.title = "MPG Group" + )
Rotate the plot: use rotate = TRUE and sort.val = “desc”
-ggbarplot(dfm, x = "name", y = "mpg_z",
- fill = "mpg_grp", # change fill color by mpg_level
- color = "white", # Set bar border colors to white
- palette = "jco", # jco journal color palett. see ?ggpar
- sort.val = "desc", # Sort the value in descending order
- sort.by.groups = FALSE, # Don't sort inside each group
- x.text.angle = 90, # Rotate vertically x axis texts
- ylab = "MPG z-score",
- legend.title = "MPG Group",
- rotate = TRUE,
- ggtheme = theme_minimal()
- )
ggbarplot(dfm, x = "name", y = "mpg_z", + fill = "mpg_grp", # change fill color by mpg_level + color = "white", # Set bar border colors to white + palette = "jco", # jco journal color palett. see ?ggpar + sort.val = "desc", # Sort the value in descending order + sort.by.groups = FALSE, # Don't sort inside each group + x.text.angle = 90, # Rotate vertically x axis texts + ylab = "MPG z-score", + legend.title = "MPG Group", + rotate = TRUE, + ggtheme = theme_minimal() + )
Lollipop chart is an alternative to bar plots, when you have a large set of values to visualize.
Lollipop chart colored by the grouping variable “cyl”:
-ggdotchart(dfm, x = "name", y = "mpg",
- color = "cyl", # Color by groups
- palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
- sorting = "ascending", # Sort value in descending order
- add = "segments", # Add segments from y = 0 to dots
- ggtheme = theme_pubr() # ggplot2 theme
- )
ggdotchart(dfm, x = "name", y = "mpg", + color = "cyl", # Color by groups + palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette + sorting = "ascending", # Sort value in descending order + add = "segments", # Add segments from y = 0 to dots + ggtheme = theme_pubr() # ggplot2 theme + )
ggdotchart(dfm, x = "name", y = "mpg",
- color = "cyl", # Color by groups
- palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
- sorting = "descending", # Sort value in descending order
- add = "segments", # Add segments from y = 0 to dots
- rotate = TRUE, # Rotate vertically
- group = "cyl", # Order by groups
- dot.size = 6, # Large dot size
- label = round(dfm$mpg), # Add mpg values as dot labels
- font.label = list(color = "white", size = 9,
- vjust = 0.5), # Adjust label parameters
- ggtheme = theme_pubr() # ggplot2 theme
- )
ggdotchart(dfm, x = "name", y = "mpg", + color = "cyl", # Color by groups + palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette + sorting = "descending", # Sort value in descending order + add = "segments", # Add segments from y = 0 to dots + rotate = TRUE, # Rotate vertically + group = "cyl", # Order by groups + dot.size = 6, # Large dot size + label = round(dfm$mpg), # Add mpg values as dot labels + font.label = list(color = "white", size = 9, + vjust = 0.5), # Adjust label parameters + ggtheme = theme_pubr() # ggplot2 theme + )
Deviation graph:
ggdotchart(dfm, x = "name", y = "mpg_z",
- color = "cyl", # Color by groups
- palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
- sorting = "descending", # Sort value in descending order
- add = "segments", # Add segments from y = 0 to dots
- add.params = list(color = "lightgray", size = 2), # Change segment color and size
- group = "cyl", # Order by groups
- dot.size = 6, # Large dot size
- label = round(dfm$mpg_z,1), # Add mpg values as dot labels
- font.label = list(color = "white", size = 9,
- vjust = 0.5), # Adjust label parameters
- ggtheme = theme_pubr() # ggplot2 theme
- )+
- geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
ggdotchart(dfm, x = "name", y = "mpg_z", + color = "cyl", # Color by groups + palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette + sorting = "descending", # Sort value in descending order + add = "segments", # Add segments from y = 0 to dots + add.params = list(color = "lightgray", size = 2), # Change segment color and size + group = "cyl", # Order by groups + dot.size = 6, # Large dot size + label = round(dfm$mpg_z,1), # Add mpg values as dot labels + font.label = list(color = "white", size = 9, + vjust = 0.5), # Adjust label parameters + ggtheme = theme_pubr() # ggplot2 theme + )+ + geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
Color y text by groups. Use y.text.col = TRUE.
-ggdotchart(dfm, x = "name", y = "mpg",
- color = "cyl", # Color by groups
- palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
- sorting = "descending", # Sort value in descending order
- rotate = TRUE, # Rotate vertically
- dot.size = 2, # Large dot size
- y.text.col = TRUE, # Color y text by groups
- ggtheme = theme_pubr() # ggplot2 theme
- )+
- theme_cleveland() # Add dashed grids
ggdotchart(dfm, x = "name", y = "mpg", + color = "cyl", # Color by groups + palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette + sorting = "descending", # Sort value in descending order + rotate = TRUE, # Rotate vertically + dot.size = 2, # Large dot size + y.text.col = TRUE, # Color y text by groups + ggtheme = theme_pubr() # ggplot2 theme + )+ + theme_cleveland() # Add dashed grids
Draw a paragraph o text. Splits a long text into multiple lines (by inserting line breaks) so that the output will fit within the current viewport.
-ggparagraph( @@ -129,7 +137,7 @@- +Draw a Paragraph of Text
# S3 method for splitText drawDetails(x, recording)
# Density plot @@ -189,16 +197,10 @@Examp heights = c(1, 0.3))
Create a pie chart.
-ggpie( @@ -129,7 +137,7 @@- +Pie chart
ggtheme = theme_pubr(), ... )
other arguments to be passed to be passed to ggpar(). |
The plot can be easily customized using the function ggpar(). Read @@ -212,11 +220,9 @@
ggpubr General Arguments Description
-Quantile-Quantile plot.
-ggqqplot( @@ -136,7 +144,7 @@- +QQ Plots
ggtheme = theme_pubr(), ... )
other arguments to be passed to |
The plot can be easily customized using the function ggpar(). Read @@ -262,11 +270,9 @@
# Create some data format @@ -288,18 +294,10 @@Examp color = "sex", palette = c("#00AFBB", "#E7B800"))
Create a scatter plot.
-ggscatter( @@ -154,6 +162,7 @@- +Scatter plot
label.select = NULL, repel = FALSE, label.rectangle = FALSE, + parse = FALSE, cor.coef = FALSE, cor.coeff.args = list(), cor.method = "pearson", @@ -164,7 +173,7 @@Scatter plot
ggtheme = theme_pubr(), ... )
label.rectangle | logical value. If TRUE, add rectangle underneath the text, making it easier to read. |
+
+
---|---|
parse | +If |
cor.coef | @@ -411,7 +425,7 @@
The plot can be easily customized using the function ggpar(). Read @@ -429,11 +443,9 @@
stat_cor
, stat_stars
, stat_conf_ellipse
and ggpar
.
Create a scatter plot with marginal histograms, density plots or box plots.
- +Create a scatter plot with marginal histograms, density plots or + box plots.
ggscatterhist( @@ -137,9 +147,21 @@- +Scatter Plot with Marginal Histograms
ylab = NULL, legend = "top", ggtheme = theme_pubr(), + print = TRUE, + ... +) + +# S3 method for ggscatterhist +print( + x, + margin.space = FALSE, + main.plot.size = 2, + margin.plot.size = 1, + title = NULL, + legend = "top", ... )
x | -x and y variables for drawing. |
+ an object of class |
---|---|---|
y | @@ -192,8 +214,8 @@||
bins | -Number of histogram bins. Defaults to 30. Pick a better value -that fit to your data. |
+ Number of histogram bins. Defaults to 30. Pick a better value that +fit to your data. |
margin.plot | @@ -205,11 +227,13 @@||
margin.ggtheme | -the theme of the marginal plot. Default is |
+ the theme of the marginal plot. Default is
+ |
margin.space | -logical value. If TRUE, adds space between the main plot and the marginal plot. |
+ logical value. If TRUE, adds space between the main plot +and the marginal plot. |
main.plot.size | @@ -235,18 +259,32 @@||
legend | -specify the legend position. Allowed values include: "top", "bottom", "left", "right". |
+ specify the legend position. Allowed values include: "top", +"bottom", "left", "right". |
ggtheme | -the theme to be used for the scatter plot. Default is |
+ the theme to be used for the scatter plot. Default is
+ |
+
logical value. If |
||
... | other arguments passed to the function |
an object of class ggscatterhist
, which is list of ggplots,
+ including the following elements:
xplot: marginal x-axis plot;
yplot: marginal y-axis plot.
.
+User can modify each of plot before printing.
+)# Basic scatter plot with marginal density plot @@ -268,16 +306,19 @@Examp palette = c("#00AFBB", "#E7B800", "#FC4E07"), margin.plot = "boxplot", ggtheme = theme_bw() -)
Create a stripchart, also known as one dimensional scatter plots. These plots are suitable compared to box plots when sample sizes are small.
-ggstripchart( @@ -151,7 +159,7 @@- +Stripcharts
ggtheme = theme_pubr(), ... )
character vector for adding another plot element (e.g.: dot plot or error bars). Allowed values are one or the combination of: "none", "dotplot", "jitter", "boxplot", "point", "mean", "mean_se", "mean_sd", -"mean_ci", "mean_range", "median", "median_iqr", "median_mad", -"median_range"; see ?desc_statby for more details. |
+"mean_ci", "mean_range", "median", "median_iqr", "median_hilow",
+"median_q1q3", "median_mad", "median_range"; see ?desc_statby for more
+details.
add.params | @@ -335,7 +344,7 @@
---|
The plot can be easily customized using the function ggpar(). Read @@ -353,12 +362,10 @@
# Load data @@ -418,18 +425,10 @@Examp add = "boxplot", add.params = list(color = "black") )
Create a ggplot with summary stats (n, median, mean, iqr) table - under the plot.
- + under the plot. Read more: How to Create a Beautiful Plots in R with Summary Statistics Labels.ggsummarytable( @@ -153,7 +161,7 @@- +GGPLOT with Summary Stats Table Under the Plot
# S3 method for ggsummarystats_list print(x, heights = c(0.8, 0.2), legend = NULL, ...)
ggsummarytable
: Create a table of summary stats
ggsummarystats
: Create a ggplot with a summary stat table under the plot.
+)# Data preparation @@ -276,12 +283,12 @@Examp df$qc <- as.factor(sample(qc, 60)) # Inspect the data head(df)
#> len supp dose qc -#> 1 4.2 VC 0.5 fail +#> 1 4.2 VC 0.5 pass #> 2 11.5 VC 0.5 pass -#> 3 7.3 VC 0.5 fail -#> 4 5.8 VC 0.5 pass +#> 3 7.3 VC 0.5 pass +#> 4 5.8 VC 0.5 fail #> 5 6.4 VC 0.5 pass -#> 6 10.0 VC 0.5 pass+#> 6 10.0 VC 0.5 fail# Basic summary stats #:::::::::::::::::::::::::::::::::::::::::::::::: @@ -289,12 +296,12 @@Examp summary.stats <- df %>% group_by(dose) %>% get_summary_stats(type = "common") -summary.stats
#> # A tibble: 3 x 11 +summary.stats#> # A tibble: 3 x 11 #> dose variable n min max median iqr mean sd se ci -#> <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> -#> 1 0.5 len 20 4.2 21.5 9.85 5.03 10.6 4.5 1.01 2.11 -#> 2 1 len 20 13.6 27.3 19.2 7.12 19.7 4.42 0.987 2.07 -#> 3 2 len 20 18.5 33.9 26.0 4.3 26.1 3.77 0.844 1.77+#> <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> +#> 1 0.5 len 20 4.2 21.5 9.85 5.03 10.6 4.5 1.01 2.11 +#> 2 1 len 20 13.6 27.3 19.2 7.12 19.7 4.42 0.987 2.07 +#> 3 2 len 20 18.5 33.9 26.0 4.3 26.1 3.77 0.844 1.77# Visualize summary table ggsummarytable( summary.stats, x = "dose", y = c("n", "median", "iqr"), @@ -322,40 +329,20 @@Examp )
# Facet #:::::::::::::::::::::::::::::::::::::::::::::::: - -ggsummarystats( - df, x = "dose", y = "len", - ggfunc = ggboxplot, add = "jitter", - color = "dose", palette = "npg", - facet.by = c("supp", "qc") -)# Specify labeller +# Specify free.panels = TRUE for free panels ggsummarystats( df, x = "dose", y = "len", ggfunc = ggboxplot, add = "jitter", color = "dose", palette = "npg", facet.by = c("supp", "qc"), labeller = "label_both" -)-# Free panels -ggsummarystats( - df, x = "dose", y = "len", - ggfunc = ggboxplot, add = "jitter", - color = "dose", palette = "npg", - facet.by = c("supp", "qc"), - free.panels = TRUE, labeller = "label_both" -)-
Add text to a plot.
-ggtext( @@ -128,13 +136,14 @@- +Text
label.select = NULL, repel = FALSE, label.rectangle = FALSE, + parse = FALSE, grouping.vars = NULL, position = "identity", ggp = NULL, ggtheme = theme_pubr(), ... )
show.legend | -logical. Should text be included in the legends? NA, -the default, includes if any aesthetics are mapped. FALSE never includes, -and TRUE always includes. |
+ logical. Should text be included in the legends? NA, the +default, includes if any aesthetics are mapped. FALSE never includes, and +TRUE always includes. |
---|---|---|
label.select | @@ -207,6 +216,11 @@label.rectangle | logical value. If TRUE, add rectangle underneath the text, making it easier to read. |
+
parse | +If |
|
grouping.vars | @@ -233,7 +247,7 @@other arguments to be passed to |
The plot can be easily customized using the function ggpar(). Read @@ -249,11 +263,9 @@
Draw a textual table.
ggtexttable()
: draw
a textual table.
table_cell_font()
: access to a table cell for changing the text font (size and face).
table_cell_bg()
: access to a table cell for changing the background (fill, color, linewidth).
tab_cell_crossout()
: cross out a table cell.
tab_ncol(), tab_nrow()
: returns, respectively, the number of columns and rows in a ggtexttable.
tab_add_hline()
: Creates horizontal lines or separators at the top or the bottom side of a given specified row.
tab_add_vline()
: Creates vertical lines or separators at the right or the left side of a given specified column.
tab_add_border(), tbody_add_border(), thead_add_border()
: Add borders to table; tbody is for table body and thead is for table head.
tab_add_title(),tab_add_footnote()
: Add title, subtitle and footnote to a table.
ggtexttable( @@ -184,10 +204,113 @@-table_cell_bg(tab, row, column, fill = NULL, color = NULL, linewidth = NULL) -Draw a Textual Table
... ) -table_cell_font(tab, row, column, face = NULL, size = NULL) +table_cell_font(tab, row, column, face = NULL, size = NULL, color = NULL) + +table_cell_bg( + tab, + row, + column, + fill = NULL, + color = NULL, + linewidth = NULL, + alpha = NULL +) + +tab_cell_crossout( + tab, + row, + column, + linetype = 1, + linewidth = 1, + linecolor = "black", + reduce.size.by = 0 +) + +tab_ncol(tab) + +tab_nrow(tab) + +tab_add_hline( + tab, + at.row = 2:tab_nrow(tab), + row.side = c("bottom", "top"), + from.column = 1, + to.column = tab_ncol(tab), + linetype = 1, + linewidth = 1, + linecolor = "black" +) + +tab_add_vline( + tab, + at.column = 2:tab_ncol(tab), + column.side = c("left", "right"), + from.row = 1, + to.row = tab_nrow(tab), + linetype = 1, + linewidth = 1, + linecolor = "black" +) + +tab_add_border( + tab, + from.row = 2, + to.row = tab_nrow(tab), + from.column = 1, + to.column = tab_ncol(tab), + linetype = 1, + linewidth = 1, + linecolor = "black" +) + +tbody_add_border( + tab, + from.row = 2, + to.row = tab_nrow(tab), + from.column = 1, + to.column = tab_ncol(tab), + linetype = 1, + linewidth = 1, + linecolor = "black" +) + +thead_add_border( + tab, + from.row = 1, + to.row = 1, + from.column = 1, + to.column = tab_ncol(tab), + linetype = 1, + linewidth = 1, + linecolor = "black" +) + +tab_add_title( + tab, + text, + face = NULL, + size = NULL, + color = NULL, + family = NULL, + padding = unit(1.5, "line"), + just = "left", + hjust = NULL, + vjust = NULL +) + +tab_add_footnote( + tab, + text, + face = NULL, + size = NULL, + color = NULL, + family = NULL, + padding = unit(1.5, "line"), + just = "right", + hjust = NULL, + vjust = NULL +)
tab | -an object of class ggtexttable. |
+ an object from |
---|---|---|
row, column | an integer specifying the row and the column numbers for the cell of interest. |
|
alpha | +numeric value specifying fill color transparency. +Value should be in [0, 1], where 0 is full transparency and 1 is no transparency. |
+ |
linetype | +line type |
+ |
reduce.size.by | +Numeric value in [0, 1] to reduce the size by. |
+ |
at.row | +a numeric vector of row indexes; for example |
+ |
row.side | +row side to which the horinzotal line should be added. Can be one of |
+ |
from.column | +integer indicating the column from which to start drawing the horizontal line. |
+ |
to.column | +integer indicating the column to which the horizontal line should end. |
+ |
at.column | +a numeric vector of column indexes; for example |
+ |
column.side | +column side to which the vertical line should be added. Can be one of |
+ |
from.row | +integer indicating the row from which to start drawing the horizontal line. |
+ |
to.row | +integer indicating the row to which the vertical line should end. |
+ |
text | +text to be added as title or footnote. |
+ |
family | +font family |
+ |
just | +The justification of the text relative to its (x, y) location. If +there are two values, the first value specifies horizontal justification +and the second value specifies vertical justification. Possible string +values are: "left", "right", "centre", "center", "bottom", and "top". For +numeric values, 0 means left (bottom) alignment and 1 means right (top) +alignment. |
+ |
hjust | +A numeric vector specifying horizontal justification. If +specified, overrides the just setting. |
+ |
vjust | +A numeric vector specifying vertical justification. If +specified, overrides the just setting. |
+
an object of class ggplot.
-# Blank theme ggtexttable(df, rows = NULL, theme = ttheme("blank"))+# light theme +ggtexttable(df, rows = NULL, theme = ttheme("light"))+# Column names border only +ggtexttable(df, rows = NULL, theme = ttheme("blank")) %>% + tab_add_hline(at.row = 1:2, row.side = "top", linewidth = 2)# classic theme -ggtexttable(df, rows = NULL, theme = ttheme("classic"))+ggtexttable(df, rows = NULL, theme = ttheme("classic"))# minimal theme -ggtexttable(df, rows = NULL, theme = ttheme("minimal"))+ggtexttable(df, rows = NULL, theme = ttheme("minimal"))# Medium blue (mBlue) theme -ggtexttable(df, rows = NULL, theme = ttheme("mBlue"))+ggtexttable(df, rows = NULL, theme = ttheme("mBlue"))# Customize the table as you want ggtexttable(df, rows = NULL, @@ -316,7 +515,7 @@Examp colnames.style = colnames_style(color = "white", fill = "#8cc257"), tbody.style = tbody_style(color = "black", fill = c("#e8f3de", "#d3e8bb")) ) -)
+)# Use RColorBrewer palette # Provide as many fill color as there are rows in the table body, here nrow = 6 ggtexttable(df, @@ -324,7 +523,7 @@Examp colnames.style = colnames_style(fill = "white"), tbody.style = tbody_style(fill = get_palette("RdBu", 6)) ) -)
+)# Text justification #:::::::::::::::::::::::::::::::::::::::::::::: # Default is "centre" for the body and header, and "right" for the row names. @@ -337,7 +536,7 @@Examp colnames.style = colnames_style(color = "white", fill = "#8cc257"), tbody.style = tbody.style ) -)
+)# Access and modify the font and # the background of table cells # ::::::::::::::::::::::::::::::::::::::::::::: @@ -347,7 +546,50 @@Examp face = "bold") tab <- table_cell_bg(tab, row = 4, column = 3, linewidth = 5, fill="darkolivegreen1", color = "darkolivegreen4") -tab
+tab+# Change table cells background and font for column 3, +# Spaning from row 2 to the last row in the data +tab <- ggtexttable(df, rows = NULL, theme = ttheme("classic")) +tab %>% + table_cell_bg(row = 2:tab_nrow(tab), column = 3, fill = "darkblue") %>% + table_cell_font(row = 2:tab_nrow(tab), column = 3, face = "italic", color = "white")+# Add separators and borders +# ::::::::::::::::::::::::::::::::::::::::::::::::::: +# Table with blank theme +tab <- ggtexttable(df, theme = ttheme("blank"), rows = NULL) +# Add horizontal and vertical lines +tab %>% + tab_add_hline(at.row = c(1, 2), row.side = "top", linewidth = 3, linetype = 1) %>% + tab_add_hline(at.row = c(7), row.side = "bottom", linewidth = 3, linetype = 1) %>% + tab_add_vline(at.column = 2:tab_ncol(tab), column.side = "left", from.row = 2, linetype = 2)+# Add borders to table body and header +# Cross out some cells +tab %>% + tbody_add_border() %>% + thead_add_border() %>% + tab_cell_crossout( + row = c(2, 4), column = 3, linecolor = "red", + reduce.size.by = 0.6 + )+# Add titles andd footnote +# ::::::::::::::::::::::::::::::::::::::::::::::::::: +# Add titles and footnote +# Wrap subtitle into multiple lines using strwrap() +main.title <- "Edgar Anderson's Iris Data" +subtitle <- paste0( +"This famous (Fisher's or Anderson's) iris data set gives the measurements", +" in centimeters of the variables sepal length and width and petal length and width,", + " respectively, for 50 flowers from each of 3 species of iris.", + " The species are Iris setosa, versicolor, and virginica." +) %>% + strwrap(width = 80) %>% + paste(collapse = "\n") + +tab <- ggtexttable(head(iris), theme = ttheme("light")) +tab %>% + tab_add_title(text = subtitle, face = "plain", size = 10) %>% + tab_add_title(text = main.title, face = "bold", padding = unit(0.1, "line")) %>% + tab_add_footnote(text = "*Table created using ggpubr", size = 10, face = "italic")# Combine density plot and summary table #::::::::::::::::::::::::::::::::::::: @@ -366,19 +608,13 @@Examp # Arrange the plots on the same page ggarrange(density.p, stable.p, ncol = 1, nrow = 2, - heights = c(1, 0.5))
Create a violin plot with error bars. Violin plots are similar to box plots, except that they also show the kernel probability density of the data at different values.
-ggviolin( @@ -154,7 +162,7 @@- +Violin plot
ggtheme = theme_pubr(), ... )
character vector for adding another plot element (e.g.: dot plot or error bars). Allowed values are one or the combination of: "none", "dotplot", "jitter", "boxplot", "point", "mean", "mean_se", "mean_sd", -"mean_ci", "mean_range", "median", "median_iqr", "median_mad", -"median_range"; see ?desc_statby for more details. |
+"mean_ci", "mean_range", "median", "median_iqr", "median_hilow",
+"median_q1q3", "median_mad", "median_range"; see ?desc_statby for more
+details.
add.params | @@ -351,7 +360,7 @@
---|
The plot can be easily customized using the function ggpar(). Read @@ -369,11 +378,9 @@
# Load data @@ -429,18 +436,10 @@Examp palette = c("#00AFBB", "#E7B800"), add = "boxplot")
Change gradient color.
gradient_color()
: Change gradient color.
gradient_fill()
: Change gradient fill.
gradient_color(palette) gradient_fill(palette)- +
Add grids to ggplot.
-grids(axis = c("xy", "x", "y"), color = "grey92", size = NULL, linetype = NULL)- +
Visualize distributions.
+ + + + +Visualize data distributions by groups.
Scatter plots
Scatter Plot with Marginal Histograms
Paired data
+ + +ggtexttable()
ttheme()
colnames_style()
rownames_style()
tbody_style()
table_cell_font()
table_cell_bg()
ggtexttable()
ttheme()
colnames_style()
rownames_style()
tbody_style()
table_cell_font()
table_cell_bg()
tab_cell_crossout()
tab_ncol()
tab_nrow()
tab_add_hline()
tab_add_vline()
tab_add_border()
tbody_add_border()
thead_add_border()
tab_add_title()
tab_add_footnote()
Draw a Textual Table
Plotting data obtained from genomic data analyses.
Change graphical parameters such as, titles, legend, colors, axis limits, scale transformation (log, sqrt), axis ticks and plot orientation (vertical, horizontal).
Easy Break Creation for Numeric Axes
Execute ggplot2 functions
Create Aes Mapping from a List
mean_se_()
mean_sd()
mean_ci()
mean_range()
median_iqr()
median_mad()
median_range()
+ add_summary()
mean_se_()
mean_sd()
mean_ci()
mean_range()
median_iqr()
median_hilow_()
median_q1q3()
median_mad()
median_range()
Add Summary Statistics onto a ggplot.
See magrittr::%>%
for details.
lhs %>% rhs+ + + +
These objects are imported from other packages. Follow the links below to see their documentation.
-%>%
Rotate a ggplot to create horizontal plots. Wrapper around
coord_flip
.
rotate(...)- +
other arguments to pass to |
# Create horizontal plots p + rotate()
Rotate the x-axis text (tick mark labels).
rotate_x_text
(): Rotate x axis text.
rotate_y_text
(): Rotate y axis text.
rotate_x_text(angle = 90, hjust = NULL, vjust = NULL, ...) rotate_y_text(angle = 90, hjust = NULL, vjust = NULL, ...)- +
... | other arguments to pass to the function
- |
+
---|
p + rotate_y_text(45)
Remove a specific component from a ggplot.
-rremove(object)- +
change_palette(), set_palette()
: Change
both color and fill palettes.
fill_palette()
: change fill palette only.
set_palette(p, palette) @@ -137,7 +145,7 @@- +Set Color Palette
color_palette(palette = NULL, ...) fill_palette(palette = NULL, ...)
Show line types available in R.
-show_line_types()
-
+
+
a ggplot.
-show_line_types()+ theme_minimal()
Show point shapes available in R.
-show_point_shapes()
-
+
+
a ggplot.
-show_point_shapes()+ theme_minimal()#>#>
R/stat_central_tendency.R
stat_central_tendency.Rd
Add central tendency measures (mean, median, mode) to density and histogram plots created using ggplots.
Note that, normally, the mode is used for categorical data where we wish to @@ -125,7 +134,6 @@
For continuous variable, we can consider using mean or median as the measures of the central tendency.
-stat_central_tendency( @@ -139,7 +147,7 @@- +Add Central Tendency Measures to a GGPLot
type = c("mean", "median", "mode"), ... )
other arguments to pass to |
# Simple density plot @@ -232,16 +239,10 @@Examp stat_central_tendency(type = "median", color = "blue", linetype = 2)
Plot convex hull of a set of points.
-stat_chull( @@ -124,7 +132,7 @@- +Plot convex hull of a set of points
inherit.aes = TRUE, ... )
# Load data @@ -205,16 +212,10 @@Examp stat_chull(aes(color = cyl, fill = cyl), alpha = 0.1, geom = "polygon")
Add mean comparison p-values to a ggplot, such as box blots, dot plots and stripcharts.
-stat_compare_means( @@ -143,7 +151,7 @@- +Add Mean Comparison P-values to a ggplot
inherit.aes = TRUE, ... )
Plot confidence ellipses around barycenters. The method for - computing confidence ellipses has been modified from FactoMineR::coord.ellipse.
- + computing confidence ellipses has been modified fromFactoMineR::coord.ellipse()
.
stat_conf_ellipse( @@ -129,7 +137,7 @@- +Plot confidence ellipses.
bary = TRUE, ... )
stat_conf_ellipse
# Load data @@ -224,16 +231,10 @@Examp stat_conf_ellipse(aes(color = cyl, fill = cyl), alpha = 0.1, geom = "polygon")
Add correlation coefficients with p-values to a scatter plot. Can be also used to add `R2`.
-stat_cor( mapping = NULL, data = NULL, method = "pearson", + alternative = "two.sided", cor.coef.name = c("R", "rho", "tau"), label.sep = ", ", label.x.npc = "left", @@ -130,6 +139,8 @@- +Add Correlation Coefficients with P-values to a Scatter Plot
digits = 2, r.digits = digits, p.digits = digits, + r.accuracy = NULL, + p.accuracy = NULL, geom = "text", position = "identity", na.rm = FALSE, @@ -137,7 +148,7 @@Add Correlation Coefficients with P-values to a Scatter Plot
inherit.aes = TRUE, ... )
a character string indicating which correlation coefficient (or covariance) is to be computed. One of "pearson" (default), "kendall", or "spearman". |
+
+ ||
alternative | +a character string specifying the alternative hypothesis, +must be one of "two.sided" (default), "greater" or "less". You can specify +just the initial letter. |
|
---|---|---|
cor.coef.name | character. Can be one of |
+|
label.sep | @@ -202,12 +219,26 @@||
output.type | -character One of "expression", "latex" or "text". |
+ character One of "expression", "latex", "tex" or "text". |
digits, r.digits, p.digits | -integer indicating the number of decimal places (round) or -significant digits (signif) to be used for the correlation coefficient and the p-value, respectively.. |
+ integer indicating the number of decimal +places (round) or significant digits (signif) to be used for the correlation +coefficient and the p-value, respectively.. |
+
r.accuracy | +a real value specifying the number of decimal places of
+precision for the correlation coefficient. Default is NULL. Use (e.g.) 0.01
+to show 2 decimal places of precision. If specified, then |
+ |
p.accuracy | +a real value specifying the number of decimal places of
+precision for the p-value. Default is NULL. Use (e.g.) 0.0001 to show 4
+decimal places of precision. If specified, then |
|
geom | @@ -244,11 +275,20 @@
correlation coefficient
correlation coefficient squared
formatted label + for the correlation coefficient
formatted label for the + squared correlation coefficient
label for the p-value
default labeldisplayed by stat_cor()
# Load data @@ -265,30 +305,30 @@Examp ) # Add correlation coefficient sp + stat_cor(method = "pearson", label.x = 3, label.y = 30)
#>+# Specify the number of decimal places of precision for p and r +# Using 3 decimal places for the p-value and +# 2 decimal places for the correlation coefficient (r) +sp + stat_cor(p.accuracy = 0.001, r.accuracy = 0.01)#>+# Show only the r.label but not the p.label +sp + stat_cor(aes(label = ..r.label..), label.x = 3)#># Use R2 instead of R ggscatter(df, x = "wt", y = "mpg", add = "reg.line") + stat_cor( aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")), label.x = 3 -)#>+)#># Color by groups and facet #:::::::::::::::::::::::::::::::::::::::::::::::::::: sp <- ggscatter(df, x = "wt", y = "mpg", color = "cyl", palette = "jco", add = "reg.line", conf.int = TRUE) -sp + stat_cor(aes(color = cyl), label.x = 3)#>+sp + stat_cor(aes(color = cyl), label.x = 3)#>
Draw the mean point of each group.
-stat_mean( @@ -124,7 +132,7 @@- +Draw group mean points
inherit.aes = TRUE, ... )
other arguments to pass to |
R/stat_overlay_normal_density.R
stat_overlay_normal_density.Rd
Overlay normal density plot (with the same mean and SD) to the density distribution of 'x'. This is useful for visually inspecting the degree of deviance from normality.
-stat_overlay_normal_density( @@ -128,7 +136,7 @@- +Overlay Normal Density Plot
inherit.aes = TRUE, ... )
other arguments to pass to |
# Simpledensity plot @@ -209,16 +216,10 @@Examp stat_overlay_normal_density(color = "red", linetype = "dashed")
Add manually p-values to a ggplot, such as box blots, dot plots - and stripcharts.
- + and stripcharts. Frequently asked questions are available on Datanovia ggpubr FAQ page, for example: +stat_pvalue_manual( @@ -126,6 +152,8 @@- +Add Manually P-values to a ggplot
size = 3.88, label.size = size, bracket.size = 0.3, + bracket.nudge.y = 0, + bracket.shorten = 0, color = "black", linetype = 1, tip.length = 0.03, @@ -134,10 +162,11 @@Add Manually P-values to a ggplot
step.group.by = NULL, hide.ns = FALSE, vjust = 0, + coord.flip = FALSE, position = "identity", ... )
bracket.size | Width of the lines of the bracket. |
+ ||
---|---|---|---|
bracket.nudge.y | +Vertical adjustment to nudge brackets by. Useful to +move up or move down the bracket. If positive value, brackets will be moved +up; if negative value, brackets are moved down. |
+ ||
bracket.shorten | +a small numeric value in [0-1] for shortening the with +of bracket. |
+ ||
color | text and line color. Can be variable name in the data for coloring by groups. |
@@ -226,6 +266,14 @@ vjust | move the text up or down relative to the bracket. Can be also a column name available in the data. |
+
coord.flip | +logical. If |
||
position | @@ -238,11 +286,10 @@
+#> # A tibble: 3 x 8 +stat.test#> # A tibble: 3 x 8 #> .y. group1 group2 p p.adj p.format p.signif method -#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr> -#> 1 len 0.5 1 1.27e- 7 2.50e- 7 1.3e-07 **** T-test -#> 2 len 0.5 2 4.40e-14 1.30e-13 4.4e-14 **** T-test -#> 3 len 1 2 1.91e- 5 1.90e- 5 1.9e-05 **** T-test+#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr> +#> 1 len 0.5 1 1.27e- 7 2.50e- 7 1.3e-07 **** T-test +#> 2 len 0.5 2 4.40e-14 1.30e-13 4.4e-14 **** T-test +#> 3 len 1 2 1.91e- 5 1.90e- 5 1.9e-05 **** T-test#> # A tibble: 3 x 8 +stat.test#> # A tibble: 3 x 8 #> .y. group1 group2 p p.adj p.format p.signif method -#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr> -#> 1 len 0.5 1 1.27e- 7 2.50e- 7 1.3e-07 **** T-test -#> 2 len 0.5 2 4.40e-14 1.30e-13 4.4e-14 **** T-test -#> 3 len 1 2 1.91e- 5 1.90e- 5 1.9e-05 **** T-test+#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr> +#> 1 len 0.5 1 1.27e- 7 2.50e- 7 1.3e-07 **** T-test +#> 2 len 0.5 2 4.40e-14 1.30e-13 4.4e-14 **** T-test +#> 3 len 1 2 1.91e- 5 1.90e- 5 1.9e-05 **** T-test# Add manually p-values from stat.test data # First specify the y.position of each comparison stat.test <- stat.test %>% @@ -288,13 +335,13 @@Examp len ~ dose, data = ToothGrowth, group.by = "supp", method = "t.test", ref.group = "0.5" ) -stat.test
#> # A tibble: 4 x 9 +stat.test#> # A tibble: 4 x 9 #> supp .y. group1 group2 p p.adj p.format p.signif method -#> <fct> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr> -#> 1 VC len 0.5 1 0.000000681 0.000002 6.8e-07 **** T-test -#> 2 VC len 0.5 2 0.0000000468 0.00000019 4.7e-08 **** T-test -#> 3 OJ len 0.5 1 0.0000878 0.000088 8.8e-05 **** T-test -#> 4 OJ len 0.5 2 0.00000132 0.0000026 1.3e-06 **** T-test# Plot +#> <fct> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr> +#> 1 VC len 0.5 1 0.000000681 0.000002 6.8e-07 **** T-test +#> 2 VC len 0.5 2 0.0000000468 0.00000019 4.7e-08 **** T-test +#> 3 OJ len 0.5 1 0.0000878 0.000088 8.8e-05 **** T-test +#> 4 OJ len 0.5 2 0.00000132 0.0000026 1.3e-06 **** T-test# Plot bp <- ggbarplot(ToothGrowth, x = "supp", y = "len", fill = "dose", palette = "jco", add = "mean_sd", add.params = list(group = "dose"), @@ -304,18 +351,12 @@Examp label = "p.signif", position = position_dodge(0.8) )
-
R/stat_regline_equation.R
stat_regline_equation.Rd
Add regression line equation and R^2 to a ggplot. Regression
model is fitted using the function lm
.
stat_regline_equation( @@ -132,7 +140,7 @@- +Add Regression Line Equation and R-Square to a GGPLOT.
inherit.aes = TRUE, ... )
x position for left edge
y position near upper edge
equation for the @@ -239,17 +247,14 @@
the source code of the function stat_regline_equation()
is
inspired from the code of the function stat_poly_eq()
(in ggpmisc
package).
@@ -293,20 +298,10 @@Examp ggpar(p, palette = "jco")
Create a star plot by drawing segments from group centroid to each points.
-stat_stars( @@ -124,7 +132,7 @@- +Add Stars to a Scatter Plot
inherit.aes = TRUE, ... )
other arguments to pass to |
Create easily a customized text grob (graphical object). Wrapper around
textGrob
.
text_grob( @@ -129,7 +137,7 @@- +Create a Text Graphical object
family = NULL, ... )
hjust | @@ -188,11 +196,10 @@other arguments passed to textGrob. |
---|
a text grob.
-text <- paste("iris data set gives the measurements in cm", @@ -207,16 +214,10 @@Examp as_ggplot(tgrob)
theme_pubr(): Create a publication ready theme
ggsummarytable()
.
-theme_pubr( @@ -162,7 +170,7 @@- +Publication ready theme
clean_theme() clean_table_theme()
Create a ggplot with transparent background.
-theme_transparent(base_size = 12, base_family = "")- +
base font family |