2.2.Plot with R
本章我们介绍如何使用 R 进行数据可视化。
R语言对于科学作图提供了强大的支持。在R语言中主要存在两套作图系统,一套是R语言原生的Base图形系统,一套是基于R包grid中实现的图形语法进行作图的一系列工具,后者相对而言更加灵活方便。
在grid中实现的主要是一些非常底层的作图函数,从使用者的角度来说需要直接调用的情况并不多。ggplot2和lattice两个R包对grid中的函数进行了很好的封装,是实际工作中非常常用的工具。
Method 1: Use Rstudio
本方案需要先按照我们上节课介绍的方法配置好R语言和rstudio,并加载一个我们提供的文件:
(1) 安装 R: https://www.r-project.org/
(2) 安装 RStudio:https://www.rstudio.com/
(3) 下载并解压lulab-plot-master.zip, 双击其中的
lulab-plot.Rproj。主要的代码都包含在一些R markdown文件中。
R markdown是一种markdown文件的扩展,rstudio可以加载R markdown文件,运行R markdown中的R代码,并将输入输出内嵌在文件中进行展示。
(4) 安装需要的package:
(5) 打开
.Rmd文件
用Rstudio打开all.Rmd文件, 即可阅读教程,并执行相关代码。
如果你更喜欢每个文件仅包含一节的内容(一种 plot 类型),可以先打开
index.Rmd,安装需要的 packages,然后依次打开每一节对应的.Rmd文件(动画展了第1、2小节对应的1.box-plots.Rmd和2.violin-plots.Rmd)
Method 2: Use R in Docker
如果你在使用方案一时遇到了问题,也可以用我们提供的 Docker(里面已经预装好了 R 语言和需要的 packages)。
(a) Use R in a Docker container
首先进入容器:
本章的操作均在 /home/test/plot/ 下进行:
进入容器后,输入R回车进入R的交互式环境:
在实际画图时,依次将下文给出的 R 代码复制到 Terminal 中运行。
(b) load data, install & library packages
Prepare output directory
在R语言中也提供了操作文件系统的函数,例如可以用
dir.create建立一个新的目录
Load data
用
read.table函数将表格数据读取到数据框中(上一节中我们已对read.table函数进行了介绍)
Install R packages
Docker 中已经装好所需要的 R 包,如果你是在自己电脑上运行,则需要安装 ggplot2, qqman, gplots, pheatmap, scales, reshape2, RColorBrewer 和 plotrix(使用 install.packages(), 如 install.packages('ggplot2'))。
Import R packages
(c) Save & view the plot
这里我们介绍保存作图结果的两种方式:
在作图代码前加上
pdf("path-to-save.pdf"),代码后加上dev.off()。这样R语言会将图片保存到路径path-to-save.pdf中。如果想保存成pdf之外的其他格式,可将pdf()换成png()等相应的函数。这种方式对于原生R语言的作图结果和ggplot2的作图结果都是适用的。以下给出了一个简单的例子:
使用
ggplot2中的ggsave函数,它只适用于保存ggplot2以及基于ggplot2的一些package的作图结果
完成作图后,可以将作图结果复制到共享目录中,在宿主机上进行查看
1) Box plots
1a) Basic box plot
在箱线图(box plot)中,我们按某个离散变量对数据进行分组展示,即x轴为类别变量,y轴通常为连续变量

1b) Change continuous color by groups

1c) Grouped boxplots
lattice和ggplot2一样,也是一个比较常用的package,大家有兴趣可自行了解
downlood input: boxplot_example.csv
download above R script

1d) Boxplot with statistical test
ggplot2支持很多个性化的配置,可以进行非常复杂的可视化
基于这样的package,可以用少量代码实现比较复杂的功能,大家可以根据具体的需求选择使用
以下代码对箱线图进行了大量个性化的设置,并利用ggpubr中的
stat_compare_means函数标注了组件均值差异的显著性geom_boxplot: 作箱线图geom_point: 展示出每个点的数值(对类别变量x轴的位置引入一定的随机性,避免点的重合,方便展示y轴每个点的分布)scale_fill_brewer: 使用RColorBrewer的配色theme_bw: 白色背景,其他设置可参考https://ggplot2-book.org/polishing.htmltheme: 对各种各样的属性进行配置,可结合具体需求进行调整panel.grid=element_blank(): 不绘制网格panel.border=element_blank(): 不添加边框axis.line = element_line(size=1, colour = "black"): 设置坐标轴颜色和粗细legend.title = element_text(face="bold", color="black",family = "Arial", size=24):设置图注标题属性,文本格式都可以通过element_text函数设置...
stat_compare_means: ggpubr提供的函数,用于标注统计显著性,输入为需要进行的两两比较列表labs: 设置坐标轴标题等

2) Violin plots
和箱线图一样,Violin plots 中横轴为类别变量,纵轴为连续变量
2a) Basic violin plot

2b) Add summary statistics on a violin plot
(2b.1) Add median and quartile

or

(2b.2) Add mean and standard deviation

or

2c) Change violin plot fill colors

3) Histogram plots
3a) Basic histogram plot

3b) Add mean line on a histogram plot

3c) Change histogram plot fill colors

4) Density plots
4a) Basic density

4b) Add mean line on a density plot

4c) Change density plot fill colors
draw the plot
4d) Change fill colors

4e) Change line colors

4f) Combine histogram and density plots

5) Dot plots
5a) Basic dot plots

5b) Add mean and standard deviation

or

5c) Change dot colors

5d) Change dot colors, shapes and align types

6) Scatter plots
6a) Basic scatter plots

6b) Add regression lines and change the point colors, shapes and sizes

6c) Scatter plot with statistical test

6d) Multiple correlation plot

7) Volcano plots
用如2.3介绍的方法进行差异表达分析,得到的结果可以用来作火山图

8) Manhattan plots

9) Heatmaps
Heatmap是可视化基因表达的常见方法
我们这里提供gplots package提供的heatmap.2函数和pheatmap package提供的pheatmap函数,以及ggplot2的scale_fill_gradient三个例子
ComplexHeatmap也是一个很常见的工具,推荐大家了解
9a) gplots package: heatmap.2()
heatmap.2()
9b) pheatmap package: pheatmap()

9c) ggplot2 package

10) Ballon plots
10a) basic ballon plots

10b) change the dot colors

11) Vennpie plots
The vennpie plot is the combination of a venn diagram and a pie chart.

Reference: http://onetipperday.sterding.com/2014/09/vennpier-combination-of-venn-diagram.html
12) Colored Bar plot for GO results


13) Combined barplot

14) Stacked barplot

15) Radar plot

16) More Reading
Last updated
Was this helpful?