软件简介
Hadley Wickham编写的ggplot2是出色且灵活的作图软件包,可用于R中的优雅数据可视化。ggplot2默认生成的绘图需要进行一些格式化,才能作出发表级别的图片质量。另外,要自定义使用ggplot,但其语法是不透明的,这对没有熟练R编程技能的用户增加了难度。“ ggpubr”软件包提供了一些易于使用的功能,用于创建和自定义基于“ ggplot2”的图片绘制。
ggpubr包的主要特点:
- ggplot2软件包周围的包装,对于R编程的初学者来说,语法不太透明。
- 使用非高级R编程技能帮助研究人员轻松创建可随时发布的图。
- 可以将p值和显着性水平自动添加到箱形图,条形图,折线图等中。
- 使在同一页面上排列和注释多个图变得容易。
- 使更改颜色和标签等图形参数变得容易。
软件安装
软件安装很方便,安装完成直接加载调用1
2
3
4
5install.packages("ggpubr")
#或者
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
library(ggpubr)
密度分布图
1 | set.seed(1234) |
柱状图
1 | gghistogram(df,x="weight",add = "mean",rug = TRUE,color = "sex",fill = "sex",palette = c("#00AFBB", "#E7B800")) |
1 |
|
1 | # Change colors and shape by groups ("sex") |
1 | # Create some data format |
1 | # Change colors and linetype by groups ("sex") |
1 | p3 <- ggecdf(wdata, x = "weight", facet.by = "sex", |
箱线图小提琴图
1 | rm(list = ls()) |
柱状图
1 | data("mtcars") |
1 | data("mtcars") |
1 | df2$mpg_z <- (df2$mpg - mean(df2$mpg))/sd(df2$mpg) |
1 | ggdotchart(df2,x="name",y="mpg",color="cyl",palette=c("#00AFBB", "#E7B800", "#FC4E07"),sorting = "ascending",label=round(df2$mpg),sort.by.groups=TRUE, add = "segments",ggtheme = theme_pubr()) |
散点图
1 | data("mtcars") |
1 | p2 <- ggscatter(df, x = "wt", y = "mpg", |
1 | # loess method: local regression fitting |
1 | # Control point size by continuous variable values ("qsec") |
1 | # Change colors |
1 | p6 <- ggscatter(df, x = "wt", y = "mpg", color = "cyl", rug=TRUE, |
1 | # Add group ellipses and mean points |
1 | p8 <- ggscatter(df, x = "wt", y = "mpg", |
1 | p9 <- ggscatter(df, x = "wt", y = "mpg", |
1 | # Textual annotation |
1 | p11 <- ggscatter(df, x = "wt", y = "mpg", |
1 | # Data |
1 | # Change width 更改柱子的宽度 |
1 | # Change the plot orientation: horizontal 变换坐标轴的方向 |
1 | # Change the default order of items 设定指定的排列顺序 |
1 | # Change colors 更改填充色和边框色 |
1 | # Change colors by groups: dose |
1 | # Change fill and outline colors by groups |
1 | # Plot with multiple groups 分组绘图 |
1 | # Change position: Interleaved (dodged) bar plot 更改排列方式 |
1 | p10 <- ggbarplot(df2, "dose", "len", |
1 | # Add points and errors |
1 | # Visualize the mean of each group |
1 | # Add error bars: mean_se 添加不同类型的误差棒 |
1 | # Use only "upper_errorbar" |
1 | # Change error.plot to "pointrange" |
1 | # Add jitter points and errors (mean_se) |
1 | # Add dot and errors (mean_se) |
1 | # Multiple groups with error bars and jitter point |
MA图
1 | # 加载数据集 |
1 | p2 <- ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"), |
1 | # Add rectangle around labels |
1 | p4 <- ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"), |
饼状图
1 | # Data: Create some data |
1 | # Change color |
1 | # Change label |
1 | # Change the position and font color of labels |
参考材料:
关注微信公众号,第一时间获取更新