美文网首页
Reproducing-1-Fig3-B Corrplot 20

Reproducing-1-Fig3-B Corrplot 20

作者: RashidinAbdu | 来源:发表于2022-04-13 03:16 被阅读0次

分别用两种包:Corrplot和GGally包绘制相关系图热图

  1. 用Corrplot绘制相关热图

安装对应的包corrplot:

install.packages(‘corrplot’)

参数说明:
corr:需要可视化的相关系数矩阵 method:指定可视化的方法,可以是圆形、方形、椭圆形、数值、阴影、颜色或饼图形
type:指定展示的方式,可以是完全的、下三角或上三角
col:指定图形展示的颜色,默认以均匀的颜色展示
bg:指定图的背景色 title:为图形添加标题
is.corr:是否为相关系数绘图,默认为TRUE,同样也可以实现非相关系数的可视化,只需使该参数设为FALSE即可
diag:是否展示对角线上的结果,默认为TRUE
outline:是否绘制圆形、方形或椭圆形的轮廓,默认为FALSE
mar:具体设置图形的四边间距
addgrid.col:当选择的方法为颜色或阴影时,默认的网格线颜色为白色,否则为灰色
addCoef.col:为相关系数添加颜色,默认不添加相关系数,只有方法为number时,该参数才起作用
addCoefasPercent:为节省绘图空间,是否将相关系数转换为百分比格式,默认为FALSE
order:指定相关系数排序的方法,可以是原始顺序(original)、特征向量角序(AOE)、第一主成分顺序(FPC)、层次聚类顺序(hclust)和字母顺序,一般”AOE”排序结果都比”FPC”要好。
hclust.method:当order为hclust时,该参数可以是层次聚类中ward法、最大距离法等7种之一
addrect:当order为hclust时,可以为添加相关系数图添加矩形框,默认不添加框,如果想添加框时,只需为该参数指定一个整数即可
rect.col:指定矩形框的颜色
rect.lwd:指定矩形框的线宽
tl.pos:指定文本标签(变量名称)的位置,当type=full时,默认标签位置在左边和顶部(lt),当type=lower时,默认标签在左边和对角线(ld),当type=upper时,默认标签在顶部和对角线,d表示对角线,n表示不添加文本标签
tl.cex:指定文本标签的大小
tl.col:指定文本标签的颜色
cl.pos:图例(颜色)位置,当type=upper或full时,图例在右表(r),当type=lower时,图例在底部,不需要图例时,只需指定该参数为n
addshade:只有当method=shade时,该参数才有用,参数值可以是negtive/positive和all,分表表示对负相关系数、正相关系数和所有相关系数添加阴影。注意:正相关系数的阴影是45度,负相关系数的阴影是135度
shade.lwd:指定阴影的线宽
shade.col:指定阴影线的颜色

具体实现:
1)Excel准备数据,并存为CSV格式:


image.png

2)读取和相关系数的计算:

cp<-read.csv(file.choose())#选择和读取准备好的的CSV文件里的数据
head(cp)
cp1<-cp[,2:7]#选择相关系数的列单
cp2<-cor(cp1,method = 'pearson')#计算pearson相关系数
cp2#查看相关系数矩阵
看到: image.png

绘制没有legend的相关系数热图:

 crp<-corrplot(corr=cp2,method = "color", cl.lim = c(-2,2),cl.pos = "n")#没有legend 
crp
得到: image.png

legend在右侧:

crp<-corrplot(corr=cp2,method = "color",title = "Competition matrix ",cl.lim = c(-2,2),cl.pos = "r")#添加标题\legend在右侧
image.png

或者legend在底部

crp<-corrplot(corr=cp2,method = "color", cl.lim = c(-2,2),cl.pos = "b")#legend在底部
image.png

添加相关系数:

crp<-corrplot(corr=cp2,method = "color" , cl.lim = c(-2,2),addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "b")#添加相关系数
image.png

绘制椭圆ellipse形状:

crp<-corrplot(corr=cp2, cl.lim = c(-2,2),method = "ellipse", order= "AOE", col = palette(), addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "r")#绘制椭圆ellipse形状
image.png

绘制圆形:

crp<-corrplot(corr=cp2 ,cl.lim = c(-1,1),method = "circle", order= "AOE", col = palette(), addCoef.col = "gray20", addCoefasPercent = TRUE, cl.pos = "r",title())#绘制圆形状
image.png

不显示相关系数:

crp<-corrplot(corr=cp2,title = "Competition matrix",cl.lim = c(-1,1),method = "color", order= "AOE", col = palette(),  cl.pos = "r",title())#不显示相关系数
crp
image.png

不显示相关系数,轴标签倾斜45度

crp<-corrplot(corr=cp2, cl.lim = c(-1,1),method = "ellipse", order= "AOE", col = palette(),   cl.pos = "r",tl.cex = 0.99,tl.srt = 45)#绘制椭圆ellipse形状 
image.png

做成饼图状:

crp<-corrplot(corr=cp2,method = "pie", cl.lim = c(-1,1), order= "AOE", cl.pos = "r", tl.cex = 0.95, tl.col = 'black',tl.srt = 45,diag = FALSE,tl.pos = 'td_') 
image.png

标签按字母顺序排序:

crp<-corrplot(corr=cp2, cl.lim = c(-1,1),method = "color",order = "alphabet", col = palette(),   cl.pos = "r",tl.cex = 0.99,tl.srt = 45)#标签按字母顺序排序 
image.png

设置边框margin的图:

crp<-corrplot(corr=cp2, cl.lim = c(-1,1),method = "color",order = "alphabet", col = palette(),   cl.pos = "r",tl.cex = 0.99,mar=margin(3, 2, 2, 2, "cm"))#设置边框margin的图 
image.png

绘制默认的corrplot:

f3b<-corrplot(corr=cp2, title = "Competition matrix",cl.lim = c(-1,1),method = "color", order= "AOE", cl.pos = "r", tl.cex = 0.95, tl.col = 'black',tl.srt = 45,diag = FALSE,tl.pos = 'td_') 
image.png
  1. 用GGally包中的ggcorr绘制相关系数热图

原始数据为:csv格式,如下


image.png

安装对应的包,加载包:

install.packages("GGally")安装对应的表
cp<-read.csv(file.choose())#选择和读取准备好的的CSV文件里的数据
head(cp)#查看读取的数据
library(ggplot2)
library(GGally)
ggcorr(cp)

计算Pearson相关系数:
注意:由于第一列是char,不是numeric,所以从第二列开始,即 cp[, -1]

ggcorr(cp[, -1], method = c("pairwise", "pearson"))## Pearson correlation coefficients, using all observations

得到:


image.png

或者也可以用其他方法:

ggcorr(cp[, -1], method = c("everything", "pearson"))
# Kendall correlation coefficients, using complete observations
ggcorr(cp[, -1], method = c("complete", "kendall"))
# Spearman correlation coefficients, using strictly complete observations
ggcorr(cp[, -1], method = c("all.obs", "spearman"))

显示相关性系数:

ggcorr(cp[, -1], method = c("pairwise", "pearson"),
       label = TRUE, hjust = 0.75) +
    geom_point(size = 23, aes(color = coefficient > 0.5, 
                              alpha = abs(coefficient) > 0.5)) +
    scale_alpha_manual(values = c("TRUE" = 0.25, "FALSE" = 0)) +
    guides(color = FALSE, alpha = FALSE)
#突显:绝对值大于0.5的所有相关系数
image.png

或者用数字格式显示:

ggcorr(cp[, -1], geom = "blank",
       label = TRUE, hjust = 0.75) +
    geom_point(size = 10, aes(color = coefficient > 0, 
                              alpha = abs(coefficient) > 0.5)) +
    scale_alpha_manual(values = c("TRUE" = 0.25, "FALSE" = 0)) +
    guides(color = FALSE, alpha = FALSE)

得到:


image.png

调整legend部分和加上标题:

corrp<-ggcorr(cp[, -1], method = c("pairwise", "pearson"), hjust =0.5, size = 3, color = "black")+theme(text=element_text(color='black', size=12), line=element_line(color='blue'),rect=element_rect(fill='white'))+labs(title='Competition matrix based on metabolic pathways')+theme(legend.position = c(-0.1, 0.2)) 
corrp
image.png

相关文章

网友评论

      本文标题:Reproducing-1-Fig3-B Corrplot 20

      本文链接:https://www.haomeiwen.com/subject/nzctgktx.html