ggpubr
ggpubr专门为学术期刊绘图而生,据说是能画出editor最喜欢看的图。
1.安装和加载
由于这个包放在cran
上,所以安装也方便,直接install.packages()
.
install.packages("ggpubr")
library("ggpubr")
2.绘图示例
- Gene expression data
- Box plots
- Violin plots
- Stripcharts and dot plots
- Density plots
- Histogram plots
- Empirical cumulative density function
- Quantile - Quantile plot
library(ggpubr)
set.seed(1234)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))#200个随机数,平均数是55
head(wdata, 4)
# sex weight
#1 F 53.79293
#2 F 55.27743
#3 F 56.08444
#4 F 52.65430
2.1 Density plots
# 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"))
其中,x
为横轴需要绘制的变量;add = "mean"
表示添加上均值;rug = TRUE
表示添加数据小地毯,这样更能展示出数据的分布;color = "sex", fill = "sex"
,表示密度图的颜色以sex为填充,如果只有color没有fill,则只会展示出密度曲线,不会有填充,反之则密度曲线都用黑线表示;palette
作为调色板,指定绘图颜色。
2.2 Histogram plot
# 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"))
参数与Density plots类似,便不再赘述。
image2.3 Box plots and violin plots
# 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
image
做什么类型的图,都要把参数调成示例的模样,然后再进一步地修改,上述参数意义为以dose为横坐标,len为纵坐标,根据不同的dose填充不同的颜色;add = "jitter"
表示在boxplot上添加跳动的点,表示数据的分布;shape = "dose"
表示点的形状依据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
my_comparisons
表示添加比较的分组;label.y
表示总的P值显示的位置。若去除最后一个stat_compare_means
,则不显示整体的P值。
小提琴图
# 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
image
label
中P值有两种表示方式,Allowed values include "p.signif" (shows the significance levels), "p.format" (shows the formatted p value),默认是第二种方式。其中,add = "boxplot"
表示在小提琴图上添加箱线图,add.params
表示add
添加图形的参数,此处为添加白色的箱线图。
2.4 Bar plots
2.4.1Ordered bar plots
data("mtcars")
df2 <- mtcars
df2$cyl <- factor(df2$cyl)
df2$name <- rownames(df2) #添加一行name
head(df2[, c("name", "wt", "mpg", "cyl")])
df2<-dfm
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
)
image
fill = "cyl"
,表示颜色填充按另一个分组来;color = "white"
改变柱子的边界,即无色;sort.val = "desc"
表示变量按降序排列,其他的选择为 Allowed values are "none" (no sorting), "asc" (for ascending) or "desc" (for descending);sort.by.groups = FALSE
表示分好的组并不聚集在一起,若为T,则聚集在一起; x.text.angle = 90
X轴的名称逆时针旋转90度。
2.4.2 分组内部排序的barplot
通过设置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
)
image
2.4.3 Deviation graphs
偏差图显示了定量值与参考值的偏差,下面的代码中,我们将从mtcars数据集绘制mpg z-score。
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")])
创造一个有序的barplot,并且根据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"
)
image
legend.title
对legend添加名称。
旋转图像。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()
)
image
2.5 Dot charts
Lollipop chart(棒棒糖图)
当您有大量的值要可视化时,棒棒图是柱状图的另一种选择。
2.5.1 一般棒棒糖图
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
)
image
add = "segments"
添加棒。
2.5.2 水平棒棒糖图
- Sort in descending order. sorting = “descending”.
- Rotate the plot vertically, using rotate = TRUE.
- Sort the mpg value inside each group by using group = “cyl”.
- Set dot.size to 6.
- Add mpg values as label. label = “mpg” or label = round(dfm$mpg).
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
)
image
group=
按组进行排序;dot.size
设置点的大小;label
将值写在点上,其中round()
表示取整;font.label
设置点的字体。
2.5.3 棒棒糖图类型的偏差图
- Use
y = “mpg_z”
- Change segment color and size:
add.params = list(color = “lightgray”, size = 2)
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")
image
geom_hline
绘制指定的水平线。
2.5.4 Cleveland’s dot plot
说白了就是将Y轴的颜色和图中分组的颜色对应起来。Use y.text.col = TRUE.
image<meta charset="utf-8">
3. 常用基本绘图函数及参数
基本绘图函数
gghistogram Histogram plot #绘制直方图
ggdensity Density plot #绘制密度图
ggdotplot Dot plot #绘制点图
ggdotchart Cleveland's Dot Plots #绘制Cleveland点图
ggline Line plot #绘制线图
ggbarplot Bar plot #绘制柱状图
ggstripchart Stripcharts #绘制带状图
ggboxplot Box plot #绘制箱线图
ggviolin Violin plot #绘制小提琴图
ggpie Pie chart #绘制饼图
ggqqplot QQ Plots #绘制QQ图
ggscatter Scatter plot #绘制散点图
ggmaplot MA-plot from means and log fold changes #绘制M-A图
ggpaired Plot Paired Data #绘制散点图矩阵
ggerrorplot Visualizing Error #绘制误差图
基本参数
ggtext Text #添加文本
border Set ggplot Panel Border Line #设置画布边框
grids Add Grids to a ggplot #添加网格线
font Change the Appearance of Titles and Axis Labels #设置字体类型
bgcolor Change ggplot Panel Background Color #更改画布背景颜色
background_image Add Background Image to ggplot2 #添加背景图片
facet Facet a ggplot into Multiple Panels #设置分面
ggpar Graphical parameters #添加画图参数
ggparagraph Draw a Paragraph of Text #添加文本段落
ggtexttable Draw a Textual Table #添加文本表格
ggadd Add Summary Statistics or a Geom onto a ggplot #添加基本统计结果或其他几何图形
ggarrange Arrange Multiple ggplots #排版多个图形
gradient_color Set Gradient Color #设置连续型颜色
xscale Change Axis Scale: log2, log10 and more #更改坐标轴的标度
add_summary Add Summary Statistics onto a ggplot #添加基本统计结果
set_palette Set Color Palette #设置画板颜色
rotate Rotate a ggplot Horizontally #设置图形旋转
rotate_axis_text Rotate Axes Text #旋转坐标轴文本
stat_stars Add Stars to a Scatter Plot #添加散点图星标
stat_cor Add Correlation Coefficients with P-values to a Scatter Plot #添加相关系数
stat_compare_means Add Mean Comparison P-values to a ggplot #添加平均值比较的P值
theme_transparent Create a ggplot with Transparent Background #设置透明背景
theme_pubr Publication ready theme #设置出版物主题
参考链接:
http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/
链接:https://www.jianshu.com/p/f53a05da7745
参考链接:https://www.jianshu.com/p/43967191bb06
网友评论