导语
GUIDE ╲
回顾之前我们介绍的BIB发表的工作:人类致癌通路的全面综述,文章中的绘图都很漂亮,小编发现文中主要的数据展示的绘图方法有饼图、堆积条形图、网络图、热图、箱式图、翻转条形图,都是一些比较常见的绘图方法,但是因作者比较善用配色,绘制的图形就看起来比较高级。吸引小编的是一组“饼图矩阵”,如下所示:
这个图中的“饼图矩阵”展示的是survival-related基因在不同的癌型和通路中所占的比例,填充颜色深浅和扇形大小代表所占比例的高低,可以说是一个非常直观的展示方法,经过小编一番研究,终于确定了这个绘图R包是scatterpie。那么今天小编就向大家介绍一下这个图的绘制方法吧~
R包scatterpie
1. 使用文章图复现
小编没能下载源文件,只好自己编一些数字了~
在EXCEL中使用=RANDBETWEEN(0,100)生成0至100间的随机矩阵。生成4个矩阵,读入数据分别命名为M1,M2,M3和M4.
整理成这种格式
x为CANCER
Y为PATHWAY
A,B,C,D为四维数据
代码语言:javascript复制install.packages("scatterpie")
library(ggplot2)
library(scatterpie)
library(RColorBrewer)
Color<-brewer.pal(4, "Set2")
ggplot()
geom_scatterpie(data = DATA,aes(x=x, y=y,r=0.4),
cols=colnames(DATA)[3:6])
scale_fill_manual(name = "DATA", values = Color)
theme(panel.background = element_blank()
)
绘出图后用AI处理一下:
由于我的数据是随机生成的,没有0值,所以与原文章图多少有些出入,大家理解一下这个绘图意思就好~
2. 气泡饼图
当然,这个包非常实用的是,还可以根据一些指标调整饼图的大小(即圆的直径)。
代码语言:javascript复制#构建数据
set.seed(123)
long <- rnorm(50, sd=100)
lat <- rnorm(50, sd=50)
d <- data.frame(long=long, lat=lat)
d <- with(d, d[abs(long) < 150 & abs(lat) < 70,])
n <- nrow(d)
d$region <- factor(1:n)
d$A <- abs(rnorm(n, sd=1))
d$B <- abs(rnorm(n, sd=2))
d$C <- abs(rnorm(n, sd=3))
d$D <- abs(rnorm(n, sd=4))
d[1, 4:7] <- d[1, 4:7] * 3
head(d)
代码语言:javascript复制d$radius <- 6 * abs(rnorm(n))
p <- ggplot() geom_scatterpie(aes(x=long, y=lat, group=region, r=radius), data=d,
cols=LETTERS[1:4], color=NA) coord_equal()
p geom_scatterpie_legend(d$radius, x=-140, y=-70)
R包corrplot
corrplot的功能是对相关矩阵或一般矩阵的图形展示。它还包含一些算法对矩阵重新排序。此外,corrplot可以选择颜色、文本标签、颜色标签、布局等。
小编想尝试是否可用corrplot中的饼图绘制来复现上图。首先我们来介绍一下这个R包。
代码语言:javascript复制install.packages("corrplot")
library(corrplot)
data(mtcars)
代码语言:javascript复制M <- cor(mtcars) #计算相关性系数
代码语言:javascript复制set.seed(0) #设定生成随机数的种子
##定义几组颜色
col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white",
"cyan", "#007FFF", "blue","#00007F"))
par(ask = FALSE)
#将绘图区分割成规则的几个部分,ask在新图绘制前是否进行提示
1. 不同排序方式展示图
代码语言:javascript复制corrplot(M, order = "AOE", col = col1(200), cl.length = 21, addCoef.col = "grey")
corrplot(M, order = "FPC", col = col1(200), cl.length = 21, addCoef.col = "grey")
corrplot(M, order = "hclust", col = col1(200), cl.length = 21, addCoef.col = "grey")
corrplot(M, order = "alphabet", col = col1(200), cl.length = 21, addCoef.col = "grey")
#order,相关矩阵字符的排序方法,可选c("AOE", "FPC", "hclust", "alphabet")
#“AOE”表示特征向量的角度顺序。它是根据角度的顺序来计算的
#“FPC”为第一个主成分排序
#“hclust”,层次聚类顺序
#“alphabet”按字母顺序排列
#ddCoef.col,在图上添加系数的颜色
#cl.length,在彩色标签中的数字文本的数目
2. 不同方法展示
代码语言:javascript复制corrplot(M, method = "circle", col = col1(200), cl.length = 21, order = "AOE",
addCoef.col = "grey",number.cex=0.7,tl.col="black")
corrplot(M, method = "square", col = col1(200), cl.length = 21, order = "AOE",
addCoef.col = "grey",number.cex=0.7,tl.col="black")
corrplot(M, method = "ellipse", col = col1(200), cl.length = 21, order = "AOE",
addCoef.col = "grey",number.cex=0.7,tl.col="black")
corrplot(M, method = "number", col = col1(200), cl.length = 21, order = "AOE",
addCoef.col = "grey",number.cex=0.7,tl.col="black")
corrplot(M, method = "shade", col = col1(200), cl.length = 21, order = "AOE",
addCoef.col = "grey",number.cex=0.7,tl.col="black")
corrplot(M, method = "color", col = col1(200), cl.length = 21, order = "AOE",
addCoef.col = "grey",number.cex=0.7,tl.col="black")
corrplot(M, method = "pie", col = col1(200), cl.length = 21, order = "AOE",
addCoef.col = "grey",number.cex=0.7,tl.col="black")
#method,相关性矩阵的可视化方法。
#method = c("circle", "square", "ellipse", "number", "shade","color", "pie")
#number.cex为系数标签大小
#tl.col是坐标标签颜色
3. 设置成黑白点
代码语言:javascript复制wb <- c("white", "black")
corrplot(M, col = wb, order = "AOE", bg = "pink",
outline = TRUE, cl.pos = "r",tl.col="black")
#outline是否绘制圆形、正方形和椭圆形的轮廓
#cl.pos,颜色标签的位置,"n"是不要绘制标签,"r"颜色标签在右侧,"n"颜色标签在下方
#bg背景色
corrplot(M, col = wb,bg = "pink", order = "AOE",
cl.pos = "r",tl.col="black")
4. 混合方法
(1)corrplot
代码语言:javascript复制## circle ellipse
corrplot(M, order = "AOE", type = "upper", tl.pos = "d",tl.col="black",tl.cex=0.7)
#type,展示矩阵类型,"upper"展示上部分,"lower"是下部分,"full"是全部
#tl.pos,颜色标签位置
corrplot(M, add = TRUE, type = "lower", method = "ellipse", order = "AOE",
diag = FALSE, tl.pos = "n", cl.pos = "n")
#add = TRUE,该图被添加到现有的图中
#diag,是否在主对角线上显示相关系数
## circle square
corrplot(M, order = "AOE",type = "upper", tl.pos = "d",tl.col="black",tl.cex=0.7)
corrplot(M, add = TRUE, type = "lower", method = "square", order = "AOE",
diag = FALSE, tl.pos = "n", cl.pos = "n")
## circle colorful number
corrplot(M, order = "AOE", type = "upper", tl.pos = "d",tl.col="black",tl.cex=0.7)
corrplot(M, add = TRUE, type = "lower", method = "number", order = "AOE",
diag = FALSE, tl.pos = "n", cl.pos = "n",number.cex=0.8)
## circle black number
corrplot(M, order = "AOE", type = "upper", tl.pos = "tp",tl.col="black",tl.cex=0.7)
corrplot(M, add = TRUE, type = "lower", method = "number", order = "AOE",
col = "black", diag = FALSE, tl.pos = "n", cl.pos = "n",number.cex=0.8)
(2) corrplot.mixed
代码语言:javascript复制M <- cor(mtcars)
ord <- corrMatOrder(M, order = "AOE")
M2 <- M[ord,ord]
corrplot.mixed(M2)
corrplot.mixed(M2, lower = "ellipse", upper = "circle")
corrplot.mixed(M2, lower = "square", upper = "circle")
corrplot.mixed(M2, lower = "shade", upper = "circle")
corrplot.mixed(M2, tl.pos = "lt")
corrplot.mixed(M2, tl.pos = "lt", diag = "u")
corrplot.mixed(M2, tl.pos = "lt", diag = "l")
corrplot.mixed(M2, tl.pos = "n")
5. 添加聚类
代码语言:javascript复制corrplot(M, order = "hclust", hclust.method = "ward.D2",
addrect = 4,rect.col = "blue",tl.col="black")
#addrect根据层次聚类绘制在图上矩形的数量
#rect.col矩形边框颜色
#hclust.method,当order为hclust时采用的聚类方法
#hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single",
#"average", "mcquitty", "median", "centroid")
6. 其他范围的系数
(1)[0, 1]
代码语言:javascript复制corrplot(abs(M), order = "AOE", col = col1(200),
cl.lim = c(0,1),tl.col="black")
(2)[-100, 100]
代码语言:javascript复制ran <- round(matrix(runif(225, -100,100), 15))
corrplot(ran, is.corr = FALSE, cl.lim = c(-100, 100),tl.col="black")
7. 缺失值
代码语言:javascript复制M2 <- M
diag(M2) = NA
corrplot(M2)
corrplot(M2, na.label = "o",tl.col="black")
corrplot(M2, na.label = "NA",tl.col="black")
8. 输入矩阵非正方形
代码语言:javascript复制corrplot(M[1:8,],tl.col="black")
9. 标记显著性值
代码语言:javascript复制corrplot(M, p.mat = res1$p, sig.level =0.05,insig = "blank",tl.col="black")
corrplot(M, p.mat = res1$p, sig.level =0.05,insig = "p-value",tl.col="black")
corrplot(M, p.mat = res1$p, sig.level =0.05,insig = "pch",tl.col="black")
corrplot(M, p.mat = res1$p, sig.level =0.05,insig = "label_sig",tl.col="black")
#sig.level大于0.05的值认为非显著的,标记为×
#insig,特殊标记不显著相关系数
#"blank"将不显著的标记为空白
#"p-value"标记不显著的P值
#"pch"不显著的标记为×
#"label_sig"将显著的值标记
10. 标记置信区间,默认0.95
(1)彩色区间
代码语言:javascript复制corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
plotCI = "circle", addg = "grey20", cl.pos = "n",tl.col="black")
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
plotCI = "rect", addg = "grey20", cl.pos = "n",tl.col="black")
#p.mat矩阵的P值
#low置信区间下界的矩阵
#upp置信区间上界的矩阵
#plotCI,绘制置信区间的方法。"n"不绘制置信区间;
#plotCI ="rect"绘制矩形,矩形的上半部分表示上界,下半部分表示下界,同时在矩形上加入相关系数
#plotCI = "circle",首先画一个绝对边界较大的圆,然后画一个绝对边界较小的圆。
#plotCI = "circle",如果这两个界限是相同的符号,较小的圆圈将被擦除,从而形成一个环。
#plotCI = "square"与"circle"相似
(2)黑白区间
代码语言:javascript复制corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
col = c("white", "black"), bg = "pink", order = "AOE",
plotCI = "circle", cl.pos = "n", pch.col = "red",tl.col="black")
11.图形复现(失败)
经过小编的实践发现,corrplot的饼图只能展示一个维度的数据,不可堆积多组数据形成饼图矩阵。所以下面展示一个矩阵数据的绘图
代码语言:javascript复制M1=as.matrix(read.csv("C:/Users/DELL/Desktop/Test/M1.csv",sep=",",header=T,quote="", check.names=F, row.names=1))
corrplot(M1, is.corr = FALSE, method = "pie",col = col1(200),
addgrid.col="white", #设置网格为白色
outline="white", #边的颜色
insig = "blank",
tl.col="black",tl.cex=0.6
)
小编总结
今天我们不只介绍了文中使用的散点饼图矩阵的方法,还介绍了绘制相关性图的方法,都是很实用的数据展示方式,大家动手操作一下吧~