Description

‘ggpubr’ provides some easy-to-use functions for creating and customizing ‘ggplot2’- based publication ready plots.

Load library

library(ggpubr)
library(dplyr)
library(ggsci)

Plot density/histogram

data(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
ggdensity(mtcars, x = "mpg", add = "mean",rug = TRUE, color = "cyl", fill = "cyl") + scale_color_npg() + scale_fill_npg()

gghistogram(mtcars, x = "mpg", add = "mean", rug = TRUE, color = "cyl", fill = "cyl") + scale_color_npg() + scale_fill_npg()
## Warning: Using `bins = 30` by default. Pick better value with the argument
## `bins`.

Plot Box plot and violin plot

data("ToothGrowth")
ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", add = "jitter") +
        scale_color_npg()

## use ggplot
ggplot(ToothGrowth, aes(x = factor(dose), y = len, group = dose, color = factor(dose))) + geom_boxplot() + scale_color_npg(alpha = 0.8) + geom_jitter(shape = 16, position = position_jitter(0.2), size = 5) + theme_classic()

## add p-value
my_comparision <- list(c("0.5", "1"), c("1", "2"), c("0.5", "2"))
ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", add = "jitter") + 
        stat_compare_means(comparisons = my_comparision) +
        stat_compare_means(label.y = 50) +
        scale_color_npg(alpha = 0.7)
## Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(c(16.5, 16.5, 15.2, 17.3, 22.5, 17.3, 13.6, :
## cannot compute exact p-value with ties

## violin plot + dotplot
ggviolin(ToothGrowth, x = "dose", y = "len", fill = "dose", alpha = 0.8,
         add = "dotplot", add.params = list(fill = "white", size = 2, alpha = 0.7)) +
        stat_compare_means(comparisons = my_comparision, label = "p.signif") +
        stat_compare_means(label.y = 50) +
        scale_fill_npg()
## Warning: Ignoring unknown parameters: size
## Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(c(16.5, 16.5, 15.2, 17.3, 22.5, 17.3, 13.6, :
## cannot compute exact p-value with ties

## violin plot + boxplot
ggviolin(ToothGrowth, x = "dose", y = "len", fill = "dose", alpha = 0.8,
         add = "boxplot", add.params = list(fill = "white")) +
        stat_compare_means(comparisons = my_comparision, label = "p.signif") +
        stat_compare_means(label.y = 50) +
        scale_fill_npg()
## Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(c(16.5, 16.5, 15.2, 17.3, 22.5, 17.3, 13.6, :
## cannot compute exact p-value with ties

Bar plot

data("mtcars")
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$name <- rownames(mtcars)
## sort values in descending order
ggbarplot(mtcars, x = "name", y = "mpg", fill = "cyl", alpha = 0.8, color = "white",
          sort.val = "desc", sort.by.groups = FALSE, x.text.angle = 90) + 
        scale_fill_npg()

## sort bars in each group
ggbarplot(mtcars, x = "name", y = "mpg", fill = "cyl", alpha = 0.8, color = "white",
          sort.val = "asc", sort.by.groups = TRUE, x.text.angle = 90) + 
        scale_fill_npg()

## Deviation graphs
# Calculate the z-score of mpg first
mtcars$mpg_z <- (mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg)
mtcars$mpg_grp <- factor(ifelse(mtcars$mpg_z < 0, "low", "high"), 
                         levels = c("low", "high"))
## Horizional plot
ggbarplot(mtcars, x = "name", y = "mpg_z", fill = "mpg_grp", alpha = 0.8, 
          color = "white", sort.val = "asc", sort.by.groups = FALSE, 
          x.text.angle = 90, ylab = "MPG z-score", xlab = FALSE, 
          legend.title = "MPG Group") + 
        scale_fill_npg()

## Vertical plot
mtcars$mpg_z <- (mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg)
mtcars$mpg_grp <- factor(ifelse(mtcars$mpg_z < 0, "low", "high"), 
                         levels = c("low", "high"))
ggbarplot(mtcars, x = "name", y = "mpg_z", fill = "mpg_grp", alpha = 0.8, 
          color = "white", sort.val = "desc", sort.by.groups = FALSE, 
          x.text.angle = 90, ylab = "MPG z-score", xlab = FALSE, 
          legend.title = "MPG Group", rotate = TRUE, ggtheme = theme_classic()) + 
        scale_fill_npg()

Cleveland’s dot plot

ggdotchart(mtcars, x = "name", y = "mpg", color = "cyl", alpha = 0.9,
           sorting = "desc", rotate = TRUE, dot.size = 4, ggtheme = theme_classic()) + scale_color_npg() + theme_cleveland()