软件简介
Hadley Wickham编写的ggplot2是出色且灵活的作图软件包,可用于R中的优雅数据可视化。ggplot2默认生成的绘图需要进行一些格式化,才能作出发表级别的图片质量。另外,要自定义使用ggplot,但其语法是不透明的,这对没有熟练R编程技能的用户增加了难度。“ ggpubr”软件包提供了一些易于使用的功能,用于创建和自定义基于“ ggplot2”的图片绘制。
ggpubr包的主要特点:
- ggplot2软件包周围的包装,对于R编程的初学者来说,语法不太透明。
- 使用非高级R编程技能帮助研究人员轻松创建可随时发布的图。
- 可以将p值和显着性水平自动添加到箱形图,条形图,折线图等中。
- 使在同一页面上排列和注释多个图变得容易。
- 使更改颜色和标签等图形参数变得容易。
软件安装
软件安装很方便,安装完成直接加载调用
install.packages("ggpubr")
#或者
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
library(ggpubr)
密度分布图
set.seed(1234)
df <- data.frame(sex=factor(rep(c("F","M"),each=200)),weight=c(rnorm(200,55),rnorm(200,58)))
ggdensity(df,x="weight",add = "mean",rug = TRUE,color = "sex",fill = "sex",palette= c("#00AFBB", "#E7B800"))
ggdensity
柱状图
gghistogram(df,x="weight",add = "mean",rug = TRUE,color = "sex",fill = "sex",palette = c("#00AFBB", "#E7B800"))
histogram
# 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)))
p1 <- ggqqplot(wdata, x = "weight")
p1
ggqqplot
# Change colors and shape by groups ("sex")
# Use custom palette
p2 <- ggqqplot(wdata, x = "weight",
color = "sex", palette = c("#00AFBB", "#E7B800"))
p2
ggqqplot
# 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)))
p1 <- ggecdf(wdata, x = "weight")
p1
ggecdf
# Change colors and linetype by groups ("sex")
# Use custom palette
p2 <- ggecdf(wdata, x = "weight",
color = "sex", linetype = "sex",
palette = c("#00AFBB", "#E7B800"))
p2
ggecdf
p3 <- ggecdf(wdata, x = "weight", facet.by = "sex",
color = "sex", linetype = "sex",
palette = c("#00AFBB", "#E7B800"))
p3
ggecdf
箱线图小提琴图
rm(list = ls())
data("ToothGrowth")
df1<- ToothGrowth
head(df1)
p<- ggboxplot(df1,x="dose",y="len",color = "dose",palette=c("#00AFBB", "#E7B800", "#FC4E07"),add = "jitter",shape="dose")
p
my_comparisons <- list(c("0.5", "1"), c("1", "2"), c("0.5", "2"))
p + stat_compare_means(comparisons = my_comparisons)+ stat_compare_means(label.y = 50)
ggviolin(df1,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") + stat_compare_means(label.y = 50)
ggboxplot
ggboxplot
ggviolin
柱状图
data("mtcars")
df2<- mtcars
df2$cyl <- factor(df2$cyl)
df2$name <- rownames(df2)
head(df2[,c("name","wt","mpg")])
ggbarplot(df2,x="name",y="mpg",fill = "cyl",color = "white",palette="npg",sort.val = "desc",sort.by.groups = FALSE,x.text.angle= 60)
ggbarplot
data("mtcars")
df2<- mtcars
df2$cyl <- factor(df2$cyl)
df2$name <- rownames(df2)
head(df2[,c("name","wt","mpg")])
ggbarplot(df2,x="name",y="mpg",fill = "cyl",color = "white",palette="aaas",sort.val = "asc",sort.by.groups = TRUE,x.text.angle= 60)
ggbarplot
df2$mpg_z <- (df2$mpg - mean(df2$mpg))/sd(df2$mpg)
df2$mpg_grp <- factor(ifelse(df2$mpg_z<0,"low","high"),levels = c("low","high"))
head(df2[,c("name","wt","mpg","mpg_grp","cyl")])
ggbarplot(df2,x="name",y="mpg_z",fill = "mpg_grp",color = "white",palette="jco",sort.val = "asc",sort.by.groups = FALSE,x.text.angle=60,ylab="MPG z-score",xlab = FALSE,legend.title="MPG Group", rotate=TRUE)
ggbarplot
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())
ggdotchart
散点图
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)
head(df)
p1 <- ggscatter(df, x = "wt", y = "mpg",
color = "red")
p1
ggscatter
p2 <- ggscatter(df, x = "wt", y = "mpg",
color = "black", shape = 21, size = 3, # Points color, shape and size
add = "reg.line", # Add regressin line
add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
conf.int = TRUE, # Add confidence interval
cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor
cor.coeff.args = list(method = "pearson", label.x = 3, label.sep = "\n")
)
p2
ggscatter
# loess method: local regression fitting
p3 <- ggscatter(df, x = "wt", y = "mpg",
add = "loess", conf.int = TRUE,
cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor
cor.coeff.args = list(method = "spearman", label.x = 3, label.sep = "\n")
)
p3
ggscatter
# Control point size by continuous variable values ("qsec")
p4 <- ggscatter(df, x = "wt", y = "mpg",
color = "#00AFBB", size = "qsec")
p4
ggscatter
# Change colors
# Use custom color palette
# Add marginal rug
p5 <- ggscatter(df, x = "wt", y = "mpg", color = "cyl", size = "qsec",
palette = c("#00AFBB", "#E7B800", "#FC4E07") )
p5
ggscatter
p6 <- ggscatter(df, x = "wt", y = "mpg", color = "cyl", rug=TRUE,
palette = c("#00AFBB", "#E7B800", "#FC4E07") )
p6
ggscatter
# Add group ellipses and mean points
# Add stars
p7 <- ggscatter(df, x = "wt", y = "mpg",
color = "cyl", shape = "cyl",
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
ellipse = TRUE)
p7
ggscatter
p8 <- ggscatter(df, x = "wt", y = "mpg",
color = "cyl", shape = "cyl",
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
ellipse = TRUE, ellipse.type = "convex",
mean.point = TRUE,
)
p8
ggscatter
p9 <- ggscatter(df, x = "wt", y = "mpg",
color = "cyl", shape = "cyl",
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
ellipse = TRUE, ellipse.type = 'confidence',
mean.point = TRUE,
star.plot = TRUE)
p9
ggscatter
# Textual annotation
df$name <- rownames(df)
p10 <- ggscatter(df, x = "wt", y = "mpg",
color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
label = "name")
p10
ggscatter
p11 <- ggscatter(df, x = "wt", y = "mpg",
color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
label = "name", repel = TRUE)
p11
ggscatter
# Data
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
print(df)
p1 <- ggbarplot(df,x="dose",y="len",label=TRUE,label.pos="out")
p1
ggbarplot
# Change width 更改柱子的宽度
p2 <- ggbarplot(df, x = "dose", y = "len", width = 0.2)
p2
ggbarplot
# Change the plot orientation: horizontal 变换坐标轴的方向
p3 <- ggbarplot(df, "dose", "len", orientation = "horiz")
p3
ggbarplot
# Change the default order of items 设定指定的排列顺序
p4 <- ggbarplot(df, "dose", "len",
order = c("D2", "D1", "D0.5"))
p4
ggbarplot
# Change colors 更改填充色和边框色
# Change fill and outline color
# add labels inside bars
p5 <- ggbarplot(df, "dose", "len",
fill = "steelblue", color = "black",
label = TRUE, lab.pos = "in", lab.col = "white")
p5
ggbarplot
# Change colors by groups: dose
# Use custom color palette
p6 <- ggbarplot(df, "dose", "len", color = "dose",
palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p6
ggbarplot
# Change fill and outline colors by groups
p7 <- ggbarplot(df, "dose", "len",
fill = "dose", color = "dose",
palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p7
ggbarplot
# Plot with multiple groups 分组绘图
# Create some data
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
print(df2)
# Plot "len" by "dose" and change color by a second group: "supp"
# Add labels inside bars
p8 <- ggbarplot(df2, "dose", "len",
fill = "supp", color = "supp",
label = TRUE, lab.col = "white", lab.pos = "in")
p8
ggbarplot
# Change position: Interleaved (dodged) bar plot 更改排列方式
p9 <- ggbarplot(df2, "dose", "len",
fill = "supp", color = "supp", palette = "Paired",
label = TRUE,
position = position_dodge())
p9
ggbarplot
p10 <- ggbarplot(df2, "dose", "len",
fill = "supp", color = "supp", palette = "Paired",
label = TRUE,
position = position_fill())
p10
ggbarplot
# Add points and errors
# Data: ToothGrowth data set we'll be used.
df3 <- ToothGrowth
head(df3, 10)
# It can be seen that for each group we have
# different values
p11 <- ggbarplot(df3, x = "dose", y = "len",width =0.3)
p11
ggbarplot
# Visualize the mean of each group
p12 <- ggbarplot(df3, x = "dose", y = "len",
add = "mean")
p12
ggbarplot
# Add error bars: mean_se 添加不同类型的误差棒
# (other values include: mean_sd, mean_ci, median_iqr, ....)
# Add labels
p13 <- ggbarplot(df3, x = "dose", y = "len",
add = "mean_se", label = TRUE, lab.vjust = -1.6)
p13
误差棒
# Use only "upper_errorbar"
p14 <- ggbarplot(df3, x = "dose", y = "len",
add = "mean_se", error.plot = "upper_errorbar")
p14
头部误差棒
# Change error.plot to "pointrange"
p15 <- ggbarplot(df3, x = "dose", y = "len",
add = "mean_se", error.plot = "pointrange")
p15
误差点
# Add jitter points and errors (mean_se)
p16 <- ggbarplot(df3, x = "dose", y = "len",
add = c("mean_se", "jitter"))
p16
误差散点
# Add dot and errors (mean_se)
p17 <- ggbarplot(df3, x = "dose", y = "len",
add = c("mean_se", "dotplot"))
p17
误差点
# Multiple groups with error bars and jitter point
p18 <- ggbarplot(df3, x = "dose", y = "len", color = "supp",
add = "mean_se", palette = c("#00AFBB", "#E7B800"),
position = position_dodge())
p18
误差点
MA图
# 加载数据集
data(diff_express)
head(diff_express)
p1 <- ggmaplot(diff_express, fdr = 0.05, fc = 2, size = 0.4,
palette = c("red","green","gray"))
p1
MA图
p2 <- ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
fdr = 0.05, fc = 2, size = 0.4,
palette = c("#B31B21", "#1465AC", "darkgray"),
genenames = as.vector(diff_express$name),
xlab = "M",ylab = "A",
legend = "top", top = 20,
font.label = c("bold", 11),
font.legend = "bold",
font.main = "bold",
ggtheme = ggplot2::theme_minimal())
p2
MA图
# Add rectangle around labels
p3 <- ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
fdr = 0.05, fc = 2, size = 0.4,
palette = c("#B31B21", "#1465AC", "darkgray"),
genenames = as.vector(diff_express$name),
legend = "top", top = 20,
font.label = c("bold", 11), label.rectangle = TRUE,
font.legend = "bold", select.top.method = "padj",
font.main = "bold",
ggtheme = ggplot2::theme_minimal())
p3
MA图
p4 <- ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
fdr = 0.05, fc = 2, size = 0.5,
palette = c("#B31B21", "#1465AC", "darkgray"),
genenames = as.vector(diff_express$name),
legend = "top", top = 20, select.top.method = "fc",
font.label = c("bold", 11), label.rectangle = TRUE,
font.legend = "bold",
font.main = "bold",
ggtheme = ggplot2::theme_minimal())
p4
MA图
饼状图
# Data: Create some data
df <- data.frame(
group = c("Male", "Female", "Child"),
value = c(25, 25, 50))
head(df)
p1 <- ggpie(df, "value", label = "group")
p1
饼状图
# Change color
# Change fill color by group
# set line color to white
# Use custom color palette
p2 <- ggpie(df, "value", label = "group",
fill = "group", color = "white",
palette = c("#00AFBB", "#E7B800", "#FC4E07") )
p2
饼状图
# Change label
# Show group names and value as labels
labs <- paste0(df$group, " (", df$value, "%)")
labs
p3 <- ggpie(df, "value", label = labs,
fill = "group", color = "white",
palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p3
饼状图
# Change the position and font color of labels
p4 <- ggpie(df, "value", label = labs,
lab.pos = "in", lab.font = "white",
fill = "group", color = "white",
palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p4
饼状图
网友评论