01 R包ggpubr学习- ggarragne

307 阅读1分钟

R包ggpubr学习-ggarrange

文档地址:cran.r-project.org/web/package… github地址:github.com/kassambara/…

下载安装

# Install
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")

# 通过conda或mamba安装
mamba install -y r-ggpubr -c conda-forge

相比R包ggplot2,ggpubr绘制出的图更加的精美,可以直接用于文章使用。

  • ggarrange() 相比wraopper包中的plot_grid(),可以为多图构建一个共同的legend。
ggarrange( ..., plotlist = NULL, ncol = NULL, nrow = NULL, labels = NULL, label.x = 0, label.y = 1, hjust = -0.5, vjust = 1.5, font.label = list(size = 14, color = "black", face = "bold", family = NULL), align = c("none", "h", "v", "hv"), widths = 1, heights = 1, legend = NULL, common.legend = FALSE, legend.grob = NULL )
  • 函数参数

image.png

image.png

  • 举例
library(ggpubr)
library(ggplot2)
library(dplyr)

data("ToothGrowth")
df <- ToothGrowth
df$dose <- as.factor(df$dose)

# Box plot
bxp <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette = "jco")
# Dot plot
dp <- ggdotplot(df, x = "dose", y = "len",
color = "dose", palette = "jco")
# Density plot
dens <- ggdensity(df, x = "len", fill = "dose", palette = "jco")
ggarrange(bxp, dp, dens, ncol = 3, nrow = 1 )

image.png 上面的拼图是箱线图,蜂群图和密度度,各自有各自的legend,可以通过参数common.legend = T,只保留一个参数。

common.legend = T

ggarrange(bxp, dp, dens, ncol = 3, nrow = 1 ,common.legend = T)

image.png 从结果图上可以看出,是3张图保留了一个legend,但是保留的lengend是箱线图的legend,是否意味着设置该参数只会保留第一张图的legend?

ggarrange(dp, bxp ,dens, ncol = 3, nrow = 1 ,common.legend = T)

image.png 验证的结果确实是只会保留第一张图的legend。

labels

该参数可以设置成labels = "AUTO"或者labels = "auto",自动产生大写或者小写的图顺序符号。

ggarrange(dp, bxp ,dens, ncol = 3, nrow = 1 ,common.legend = T, labels = "AUTO")

image.png

ggarrange(dp, bxp ,dens, ncol = 3, nrow = 1 ,common.legend = T, labels = "auto")

image.png

legend

"top", "bottom", "left", "right", "none"值中的一个,调整legend的位置。

ggarrange(dp, bxp ,dens, ncol = 3, nrow = 1 , labels = "auto", legend = "bottom")

image.png

font.label

调整图顺序标签,参数输入值为list,可输入的元素有字体大小"size",字体样式"family",字体是否加粗"face",字体颜色"color"

ggarrange(dp, bxp ,dens, ncol = 3, nrow = 1 , labels = "auto", legend = "bottom", font.label = list(size = 20, color = "red",font = "bold"))

image.png