ggplot2 绘制 相关矩阵图

7 篇文章 2 订阅


ggplot2中绘制相关矩阵有很多包可以实现,目前看 ggcorr包的定制性极大。

数据获取

iris[,-5] %>%
  correlate(cor.test = TRUE,method="pearson",p.adjust = T) %>%
  as_cor_tbl()

结果为:

A tibble: 16 x 8
.row.names .col.names r p.value upper.ci lower.ci .row.id
1 Sepal.Length Sepal.Length 1 0 1 1 4
2 Sepal.Width Sepal.Length -0.118 3.04e- 1 0.0435 -0.273 3
3 Petal.Length Sepal.Length 0.872 1.04e-46 0.906 0.827 2
4 Petal.Width Sepal.Length 0.818 1.86e-36 0.865 0.757 1
5 Sepal.Length Sepal.Width -0.118 3.04e- 1 0.0435 -0.273 4
6 Sepal.Width Sepal.Width 1 0 1 1 3
7 Petal.Length Sepal.Width -0.428 2.71e- 7 -0.288 -0.551 2
8 Petal.Width Sepal.Width -0.366 1.63e- 5 -0.219 -0.497 1
9 Sepal.Length Petal.Length 0.872 1.04e-46 0.906 0.827 4
10 Sepal.Width Petal.Length -0.428 2.71e- 7 -0.288 -0.551 3
11 Petal.Length Petal.Length 1 0 1 1 2
12 Petal.Width Petal.Length 0.963 5.61e-85 0.973 0.949 1
13 Sepal.Length Petal.Width 0.818 1.86e-36 0.865 0.757 4
14 Sepal.Width Petal.Width -0.366 1.63e- 5 -0.219 -0.497 3
15 Petal.Length Petal.Width 0.963 5.61e-85 0.973 0.949 2
16 Petal.Width Petal.Width 1 0 1 1 1

iris[,-5] %>% cor(method="spearman") %>%
  fortify_cor(type="upper",cor.test = T,cluster=T)

结果为:

A tibble: 10 x 8
.row.names .col.names r p.value upper.ci lower.ci .row.id .col.id

1 Sepal.Width Sepal.Width 1 0 1 1 4 1
2 Sepal.Width Sepal.Length -0.975 2.51e- 2 -0.219 -0.999 4 2
3 Sepal.Length Sepal.Length 1 2.22e-16 1 1.00 3 2
4 Sepal.Width Petal.Length -0.999 9.41e- 4 -0.954 -1.00 4 3
5 Sepal.Length Petal.Length 0.982 1.79e- 2 1.00 0.374 3 3
6 Petal.Length Petal.Length 1 0 1 1 2 3
7 Sepal.Width Petal.Width -0.998 1.53e- 3 -0.926 -1.00 4 4
8 Sepal.Length Petal.Width 0.971 2.89e- 2 0.999 0.150 3 4
9 Petal.Length Petal.Width 0.996 4.13e- 3 1.00 0.811 2 4
10 Petal.Width Petal.Width 1 0 1 1 1 4

iris[,-5] %>%
  correlate(cor.test = TRUE,method="pearson",p.adjust = T) %>%
  fortify_cor(type="full",cluster=T)

结果为:

A tibble: 16 x 8
.row.names .col.names r p.value upper.ci lower.ci .row.id .col.id
1 Sepal.Width Sepal.Width 1 0 1 1 4 1
2 Sepal.Length Sepal.Width -0.118 3.04e- 1 0.0435 -0.273 3 1
3 Petal.Length Sepal.Width -0.428 2.71e- 7 -0.288 -0.551 2 1
4 Petal.Width Sepal.Width -0.366 1.63e- 5 -0.219 -0.497 1 1
5 Sepal.Width Sepal.Length -0.118 3.04e- 1 0.0435 -0.273 4 2
6 Sepal.Length Sepal.Length 1 0 1 1 3 2
7 Petal.Length Sepal.Length 0.872 1.04e-46 0.906 0.827 2 2
8 Petal.Width Sepal.Length 0.818 1.86e-36 0.865 0.757 1 2
9 Sepal.Width Petal.Length -0.428 2.71e- 7 -0.288 -0.551 4 3
10 Sepal.Length Petal.Length 0.872 1.04e-46 0.906 0.827 3 3
11 Petal.Length Petal.Length 1 0 1 1 2 3
12 Petal.Width Petal.Length 0.963 5.61e-85 0.973 0.949 1 3
13 Sepal.Width Petal.Width -0.366 1.63e- 5 -0.219 -0.497 4 4
14 Sepal.Length Petal.Width 0.818 1.86e-36 0.865 0.757 3 4
15 Petal.Length Petal.Width 0.963 5.61e-85 0.973 0.949 2 4
16 Petal.Width Petal.Width 1 0 1 1 1 4

可视化

#可视化
dat1=iris[,-5] %>%
  correlate(cor.test = TRUE,method="pearson",p.adjust = T) %>%
  fortify_cor(type="full",cluster=T)
  
dat1 %>%
  ggcor()+
  geom_color(aes(fill=r))+
  geom_number(aes(num=r))

在这里插入图片描述

快速绘制

几何形状
#geoms
p1=quickcor(iris[,-5]) + geom_square()+
  labs(title="geom_square")
p2=quickcor(iris[,-5]) + geom_color()+
  labs(title="geom_color")
p3=quickcor(iris[,-5]) + geom_pie2()+
  labs(title="geom_pie2")
p4=quickcor(iris[,-5]) + geom_ellipse2()+
  labs(title="geom_ellipse2")
p5=quickcor(iris[,-5]) + geom_circle2()+
  labs(title="geom_circle2")
p7=quickcor(iris[,-5]) +geom_ring()+
  labs(title="geom_ring")
p8=quickcor(iris[,-5]) +geom_star()+
  labs(title="geom_star")
p9=quickcor(iris[,-5]) +geom_color()+geom_shade()+
  labs(title="geom_shade")

在这里插入图片描述

带统计检验
p1=quickcor(iris[,-5],cor.test=T) +
  geom_number(aes(num=p.value))+
  labs(title="geom_number (p.value)")

p2=quickcor(iris[,-5],cor.test=T) +
  geom_mark(aes(p.value=p.value))+
  labs(title="geom_mark (r)")

p3=quickcor(iris[,-5],cor.test=T) + geom_cross(sig.level=0.01)+
  labs(title="geom_cross, insignificant with x")

p4=quickcor(iris[,-5],cor.test=T) + geom_confbox()+
  labs(title="geom_confbox, CI of r")

在这里插入图片描述

自定义绘图

ggcor()+geom_???()
#上下不一致
#自定义
dat1 %>% ggcor()+
  geom_square(data = get_upper_data(dat1,show.diag = FALSE),aes(fill=r),r0=1)+
  geom_number(data = get_upper_data(dat1,show.diag = FALSE),aes(num=r))+
  geom_mark(data = get_lower_data(dat1,show.diag = FALSE), aes(p.value=p.value),size = 4.5) +
  geom_abline(slope = -1, intercept = 5)+
  expand_limits(x=c(0.5,4.5),y=c(0.5,4.5))+
  scale_fill_distiller(palette = "Spectral")

在这里插入图片描述

quickcor()+geom_???()
#快速
quickcor(iris[,-5], cor.test = TRUE) +
  geom_square(data = get_data(type = "lower", show.diag = FALSE)) +
  geom_mark(data = get_data(type = "upper", show.diag = FALSE), size = 2.5) +
  geom_abline(slope = -1, intercept = 5)

在这里插入图片描述

复杂矩阵图

矩阵相关Mantel
data("varechem", package = "vegan") #样地属性矩阵
data("varespec", package = "vegan") #物种覆盖度矩阵

mantel <- mantel_test(varespec, varechem,
                      spec.select = list(Spec01 = 1:7,
                                         Spec02 = 8:18,
                                         Spec03 = 19:37,
                                         Spec04 = 38:44)) %>% #Mantel检验
  mutate(rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),
                  labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),
         pd = cut(p.value, breaks = c(-Inf, 0.01, 0.05, Inf),
                  labels = c("< 0.01", "0.01 - 0.05", ">= 0.05"))) #对连续变量分组

quickcor(varechem, type = "upper") +
  geom_square() + #相关矩阵图
  scale_fill_gradient2(midpoint = 0, low = "#56B4E9", mid = "white",high = "#E69F00", space = "Lab")+
  anno_link(aes(colour = pd, size = rd), data = mantel) + #mantel连接
  geom_diag_label(remove.axis = T)+
  remove_axis("x")+
  scale_size_manual(values = c(0.5, 1, 2)) + #粗细, r系数
 # scale_colour_manual(values = c( "#56B4E9", "#E69F00", "#999999")) + #颜色 p value
  guides(size = guide_legend(title = "Mantel's r", override.aes = list(colour = "grey35"),order = 1),
         colour = guide_legend(title = "Mantel's p",override.aes = list(size = 3),order = 2),
         fill = guide_colorbar(title = "Pearson's r", order = 3))

在这里插入图片描述

聚类热图
mtcars %>%
  quickcor(cluster = T) +
  geom_colour(colour = "white", size = 1) +
  scale_fill_distiller(palette = "Spectral")+
  anno_row_tree(pos="left") +
  anno_col_tree()

在这里插入图片描述

极坐标聚类热图
quickcor(varespec,varechem,
         cluster=T)+
  geom_colour(colour = "white", size = 1) +
  scale_fill_distiller(palette = "Spectral")+
  anno_row_tree(pos="left") +
  anno_col_tree()

在这里插入图片描述

quickcor(varespec,varechem,
         cluster=T,
         circular=T,open=125)+ #极坐标
  geom_colour(colour = "white", size = 1) +
  scale_fill_distiller(palette = "Spectral")+
  anno_row_tree(pos="left") +
  anno_col_tree()+
  set_p_xaxis()+
  set_p_yaxis()

在这里插入图片描述

多组分热图
## 复杂热图
d1 <- cor(varespec,varechem) %>% as.data.frame() %>%
  gcor_tbl(cluster = TRUE)

p <- matrix(sample(LETTERS[1:5], 132, replace = TRUE), nrow = 44,
            dimnames = list(names(varespec), paste0("Type", 1:3))) %>%
  gcor_tbl(name = "Type",row.order=d1) %>%
  qheatmap(aes(fill = Type)) +  remove_y_axis()

d2=data.frame(x = sample(names(varechem), 200, replace = TRUE))

#使用ggcor()代替quickcor()
# d1 %>% ggcor()+
#   geom_tile(aes(fill=value),color="white")+
#   scale_fill_distiller(palette="Spectral")+
#   theme_cor()+
#   anno_hc_bar(k=3,width=0.3,fill=ggsci::pal_aaas()(3))+
#   anno_row_custom(p,width=0.2)+
#   anno_row_tree(width=0.2)+
#   anno_hc_bar(k=3,pos="top",height = 0.3)+
#   anno_bar(data=d2,aes(x=x),height=0.2)+
#   anno_col_tree()

在这里插入图片描述

  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值