ggpubr: 'ggplot2' Based Publication Ready Plots
1.R包的安装及加载ggpubr包可以从CRAN或GitHub中进行下载安装 Install from CRAN as follow: install.packages('ggpubr') library(ggpubr) Or, install the latest version from GitHub as follow: if(!require(devtools)) install.packages('devtools') 2.常用基本图形的绘制
带有均值线和地毯线的密度图#构建数据集set.seed(1234) df <- data.frame( sex=factor(rep(c('f', 'M'), each=200)), weight=c(rnorm(200, 55), rnorm(200, 58))) # 预览数据格式 head(df) # 绘制密度图 # rug参数添加地毯线,add参数可以添加均值mean和中位数median,按性别”sex“分组标记边框线颜色和填充色,使用palette参数自定义颜色 p1 <- ggdensity(df, x='weight', add = 'mean', rug = TRUE, color = 'sex', fill = 'sex',palette = c('#00AFBB', '#E7B800')) p1 带有均值线和边际地毯线的直方图p2 <- gghistogram(df, x='weight', add = 'mean', rug = TRUE, color = 'sex',
箱线图+分组形状+统计#加载数据集 ToothGrowthdata('ToothGrowth') df1 <- ToothGrowth head(df1) #jitter参数添加扰动点,点的形状shape由dose变量映射 p3 <- ggboxplot(df1, x='dose', y='len', color = 'dose', palette = c('#00AFBB', '#E7B800', '#FC4E07'), add = 'jitter', shape='dose') p3 # stat_compare_means参数比较不同组之间的均值,并增加不同组间比较的p-value值,可以自定义需要标注的组间比较 内有箱线图的小提琴图+星标记# add = “boxplot”添加箱线图,stat_compare_means中设置lable=”p.signif”,即可添加星添加组间比较连线和统计P值按星分类add添加箱线图,label标注选择显著性标记(星号) p5 <- 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) p5
# 加载数据集 # 按组进行排序 p7 <- ggbarplot(df2, x='name', y='mpg', fill = 'cyl', color = 'white', palette = 'aaas', #杂志Science的配色 sort.val = 'asc', #升序排序,区别于desc sort.by.groups=TRUE,x.text.angle=60) #按组排序 x.text.angl设置x轴标签旋转角度 p7
偏差图展示了与参考值之间的偏差 df2$mpg_z <- (df2$mpg-mean(df2$mpg))/sd(df2$mpg) # rotate设置x/y轴对换 p9 <- ggbarplot(df2, x='name', y='mpg_z', fill = 'mpg_grp', color = 'white', palette = 'jco', sort.val = 'desc', sort.by.groups = FALSE, x.text.angle=90, ylab = 'MPG z-score', xlab = FALSE, legend., rotate=TRUE, ggtheme = theme_minimal()) p9
棒棒图可以代替条形图展示数据 p10 <- ggdotchart(df2, x='name', y='mpg', color = 'cyl', # 设置其他参数, dot.size = 6调整糖的大小,添加label标签,设置字体样式和方向 p11 <- ggdotchart(df2, x='name', y='mpg', color = 'cyl', palette = c('#00AFBB', '#E7B800', '#FC4E07'), sorting = 'descending', add = 'segments', rotate = TRUE, group = 'cyl', dot.size = 6, label = round(df2$mpg), font.label = list(color='white', size=9, vjust=0.5), ggtheme = theme_pubr()) p11 #棒棒糖偏差图
# theme_cleveland()主题可设置为Cleveland点图样式 p13 <- 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 rotate = TRUE, # Rotate vertically dot.size = 2, # Large dot size y.text.col = TRUE, # Color y text by groups ggtheme = theme_pubr() # ggplot2 theme ) + theme_cleveland() # Add dashed grids p13 3.常用基本绘图函数及参数基本绘图函数gghistogram Histogram plot #绘制直方图 基本参数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 #排版多个图形 annotate_figure Annotate Arranged Figure #添加注释信息 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值 diff_express Differential gene expression analysis results #内置差异分析结果数据集 ggexport Export ggplots # 导出图片 theme_transparent Create a ggplot with Transparent Background #设置透明背景 theme_pubr Publication ready theme #设置出版物主题 |
|