R绘图 ggpubr: 为学术而生

连着学了几天的R基础,从阅读量来看感到大家的疲劳,今天我们来学习R绘图,毕竟能出图就有成就感。前期我们有介绍过热图和火山图的制作,今天介绍一个R包“ggpubr”,用于创建和自定义基于“ggplot2”的图。(大家只要把数据整理成示例数据的模样,就可以随心所欲的画图了)

TCGA数据分析系列之火山图

R语言学习系列之“多变的热图”

热图系列1

安装包

install.packages("ggpubr")

密度图

带平均线和边缘地毯的密度图

library(ggpubr)#> Loading required package: ggplot2#> Loading required package: magrittr# 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)))head(wdata, 4)#> sex weight#> 1 F 53.79293#> 2 F 55.27743#> 3 F 56.08444#> 4 F 52.65430
# Density plot with mean lines and marginal rug# :::::::::::::::::::::::::::::::::::::::::::::::::::# Change outline and fill colors by groups ("sex")# Use custom paletteggdensity(wdata, x = "weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = c("#00AFBB", "#E7B800"))

直方图

带平均线和边缘地毯的直方图

# Histogram plot with mean lines and marginal rug# :::::::::::::::::::::::::::::::::::::::::::::::::::# Change outline and fill colors by groups ("sex")# Use custom color palettegghistogram(wdata, x = "weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = c("#00AFBB", "#E7B800"))

箱图和小提琴图

箱图

p <- ggboxplot(df, x = "dose", y = "len", color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"), add = "jitter", shape = "dose")p

加统计值

# Add p-values comparing groups # Specify the comparisons you wantmy_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

小提琴图与箱图合并

# Violin plots with box plots inside# :::::::::::::::::::::::::::::::::::::::::::::::::::# Change fill color by groups: dose# add boxplot with white fill colorggviolin(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)

条形图

# Load datadata("mtcars")dfm <- mtcars# Convert the cyl variable to a factordfm$cyl <- as.factor(dfm$cyl)# Add the name columsdfm$name <- rownames(dfm)# Inspect the datahead(dfm[, c("name", "wt", "mpg", "cyl")])#> name wt mpg cyl#> Mazda RX4 Mazda RX4 2.620 21.0 6#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6#> Datsun 710 Datsun 710 2.320 22.8 4#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6#> Hornet Sportabout Hornet Sportabout 3.440 18.7 8#> Valiant Valiant 3.460 18.1 6

有序条形图

通过分组变量“cyl”更改填充颜色。将全局排序,但不按组排序。

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)

按组排列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 )

偏差图

偏差图显示量化值到参考值的偏差。

计算mpg数据的z值

# Calculate the z-score of the mpg datadfm$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 datahead(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])#> name wt mpg mpg_z mpg_grp cyl#> Mazda RX4 Mazda RX4 2.620 21.0 0.1508848 high 6#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 0.1508848 high 6#> Datsun 710 Datsun 710 2.320 22.8 0.4495434 high 4#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 0.2172534 high 6#> Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345 low 8#> Valiant Valiant 3.460 18.1 -0.3302874 low 6

创建一个有序的条形图,根据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")

使用Rotate=TRUE和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() )

点图

棒棒糖

当你有大量的值要可视化时,棒棒糖图表是条形图的一种替代方法。
由分组变量“cyl”着色棒棒糖图:

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 )

我们可以

按降序排序:sorting = “descending”

垂直旋转绘图:rotate = TRUE

使用group=“cyl”对每组中的mpg值进行排序。
将“点大小”设置为6。
添加mpg值作为标签。label=“mpg”或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 )

偏差图

设置分组的颜色和大小: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")

y轴文本上色:y.text.col = TRUE

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()

今天就分享到这

另外,最近收集了一些很好的资源,分享给大家,顺便能涨一些粉,主要有

1. R语言学习基础知识代码

2. 19年中标的各门类国自然题目汇总,以及17年的国自然汇总,部分含摘要!

3. R语言学习书籍

R语言实战(中文完整版)

R数据科学(中文完整版)

ggplot2:数据分析与图形艺术

30分钟学会ggplot2

4. TCGA数据整理

前期从https://xenabrowser.net/datapages/ (UCSC Xena)数据库下载的TCGA数据,传到了百度云上备份。

ggplot2速查表pdf(可复制)

感兴趣的话,转发朋友圈或者100人以上的微信群,截图发到公众号,即可获取全部资源的百度云链接,希望大家赶紧下载。你们的支持是我前进的动力,感谢。

(0)

相关推荐

  • ggcorrplot|相关性矩阵可视化神器完整教程

    欢迎来到医科研,这里是白介素2的读书笔记,跟我一起聊临床与科研的故事, 生物医学数据挖掘,R语言,TCGA.GEO, SEER数据挖掘. -ggcorrplot可视化相关性矩阵- ggcorrplot ...

  • 如何一条代码优雅的计算任意数量的线性回归图表

    写在前面 回归,是我们经常用到的分析手段,对于结果的确定比差异分析和相关分析都要更加深入,所以无论是大小文章,都可以看到回归的影子. 今天我来带大家,仅仅需要一条代码,就将这个分析做的透明,透彻,审稿 ...

  • R绘图:配对样本差异表达作图ggpubr

    R绘图往期回顾: R绘图:唱一半的歌,画一半的图 gghalves R绘图:gggibbous,基于ggplot2的Moon charts R绘图:ggeconodist,基于ggplot2的另类箱图 ...

  • R绘图笔记 | R语言绘图系统与常见绘图函数及参数

    一. R语言绘图系统 在 R 里,主要有两大底层图形系统,一是 base 图形系统,二是 grid 图形系统.lattice 包与 ggplot2包正是基于 grid 图形系统构建的,它们都有自己独特 ...

  • R绘图笔记 | 一般的散点图绘制

    R绘图笔记 | 一般的散点图绘制

  • R绘图笔记 | 柱状图绘制

    R绘图笔记 | 柱状图绘制

  • R绘图笔记 | 直方图和核密度估计图的绘制

    前面,介绍过散点图绘制,也介绍了柱状图的绘制.本文介绍直方图和核密度估计图的绘制. 1.直方图 直方图是数值数据分布的精确图形表示.这是一个连续变量(定量变量)的概率分布的估计,并且被卡尔·皮尔逊(K ...

  • R绘图笔记 | 二维散点图与统计直方图组合

    前面介绍了散点图.柱状图.直方图和核密度估计图,有时候散点图不能很直观的看的出数据的分布情况,这里介绍散点图与统计直方图组合绘制. 一.方法1 利用ggpubr包的ggscatterhist()函数进 ...

  • R绘图笔记 | 散点分布图与柱形分布图

    关于绘图图,前面介绍了一些: 这里介绍散点分布图与柱形分布图,这些图形在文章中是很常见的,也是必须要掌握的. 一.读入数据 如果你想获取该数据用于自己练习,下面是获取数据的地址: https://do ...

  • R绘图笔记 | 箱形图的绘制

    关于绘图图,前面介绍了一些: 这里介绍箱形图的绘制,这些图形在文章中是很常见的,也是必须要掌握的.比如下图中的E图(来自文献:DOI: 10.1002/jcp.30015 ) 一.读入数据 如果你想获 ...

  • R绘图笔记 | 小提琴图与漂亮的云雨图绘制

    关于绘图图,前面介绍了一些: R绘图笔记 | 一般的散点图绘制 R绘图笔记 | 柱状图绘制 R绘图笔记 | 直方图和核密度估计图的绘制 R绘图笔记 | 二维散点图与统计直方图组合 R绘图笔记 | 散点 ...