前面我们已经介绍了如何选择相关性分析的方法和流程,在双变量相关性分析最后得到的了相关系数及一个带有拟合曲线的散点图(点击查看双变量相关性分析及R作图)。而在多变量相关性分析中我们得到了一个相关性矩阵,下面将介绍如何使用R的corrplot包
进行花式做图
# 使用corplot建立共表达分析矩阵:
# 初始化:
rm(list=ls())
# 载入corrplot包,没有安装的请install.packages("corrplot")
library(corrplot)
使用example函数查看corrplot包中两个关键函数的作图实例,按回车
查看下个实例,按ESC
退出观看实例。
# 输入example("corrplot)查看corrplot中corrplot函数的实例
example("corrplot",package = "corrplot")
# 查看corrplot包中corrplot.mixed函数的实例。
example("corrplot.mixed",package = "corrplot")
我们以attitude为例,使用Spearman计算变量之间的相关性:
# 使用Spearman方法计算相关性矩阵:
data("attitude")
cor_attitude <- cor(attitude,method="spearman")
# 将相关性系数保留三位小数
cor_attitude<-round(cor_attitude,3)
head(cor_attitude)
![](https://img.haomeiwen.com/i903467/109610e862cd4f9e)
image
使用corrplot()函数进行作图,这个图是我个人比较喜欢的,所以就放第一了。
corrplot(cor_attitude, #相关性矩阵
method="color", #填充方法,可以是颜色或图形,circle,square,ellipse,number
type="upper", #只显示上半部分矩阵三角
order = "hclust", #排序方式
addCoef.col = "#ff0099") #设置相关性系数的字体颜色
![](https://img.haomeiwen.com/i903467/0b37931e5f04598b)
image
还有以下一些图:
# 圆圈显示:
corrplot(cor_attitude,method = "circle")
![](https://img.haomeiwen.com/i903467/ba221ea4e43d30bb)
image
# 矩形显示:
corrplot(cor_attitude,method = "square")
![](https://img.haomeiwen.com/i903467/00b60c6da34a4e65)
image
# 椭圆显示:
corrplot(cor_attitude,method = "ellipse")
![](https://img.haomeiwen.com/i903467/df406e83b264dc9e)
image
# 显示相关性系数:
corrplot(cor_attitude,method = "number")
![](https://img.haomeiwen.com/i903467/6b0771449bd00a3d)
image
# 纯色显示:
corrplot(cor_attitude,method = "color")
![](https://img.haomeiwen.com/i903467/fb8e86c58dbb3185)
image
# 饼图显示:
corrplot(cor_attitude,method = "pie",addCoef.col = "#ff0099")
![](https://img.haomeiwen.com/i903467/a045d26fe378c0c2)
image
# 只显示上面的相关性三角
corrplot(cor_attitude, type="upper")
![](https://img.haomeiwen.com/i903467/41f07c52d010d978)
image
# 只显示下面的相关性三角
corrplot(cor_attitude, type="lower")
![](https://img.haomeiwen.com/i903467/b4b35818cb171c82)
image
corrplot.mixed()
用阿莱混合显示样式:
# 混合显示圆圈和相关性系数:
corrplot.mixed(cor_attitude)
![](https://img.haomeiwen.com/i903467/ecdd7e5e9f4dd04f)
image
# 混合显示上面是圆圈,下面是矩形。
corrplot.mixed(cor_attitude, lower="square", upper="circle")
![](https://img.haomeiwen.com/i903467/fa5e42dc1764d5d1)
image
当然还可以使用GGally
的ggcorr
来实现。
install.packages("GGally")
library(GGally)
# 不以色条形式展示,而是以颜色区间展示,如果想以色条展示,去掉nbreaks参数。
ggcorr(cor_attitude[,],
nbreaks=6,
low = "steelblue",
mid = "white",
high = "darkred",label= T)
![](https://img.haomeiwen.com/i903467/09cf8658e04f6eea)
image
但是作为一个忠实的ggplot2脑残粉!我还是想用ggplo2来实现相关性热图。稍微搜索了以下,老外又帮我们做好了,点击这里查看,感谢老外。
总结下,我们今天使用了R
的corrplot
包和GGally
包对相关性矩阵进行了花式作图,相关性分析在基因组研究中主要用于共表达分析,但共表达分析存在一定的局限,而加权基因共表达网络分析(WGCNA)
用的更多些,后面我将开个专题专门介绍WGCNA分析从入门到精通。
点击查看corrplot包说明文档
点击查看GGally包说明文档
网友评论