0
点赞
收藏
分享

微信扫一扫

跟着Nature Methods学画图:R语言ggplot2+ggtree+aplot画气泡图组合聚类树图

https://mp.weixin.qq.com/s/XVl2MoOsT7pB1wNJmltoVw

论文是


论文对应的代码是公开的 https://github.com/ajwilk/2020_Wilk_COVID

按照论文提供的代码得到了画图用到的数据,部分数据如下

读入数据做气泡图,
data.final<-read.csv("NM/figure2f.csv",header=T,check.names=F)
head(data.final)

library(ggplot2)
ggplot(data.final,aes(x=features.plot,y=id))+
  geom_point(aes(size=`Percent expressed`,
                 color=`Average expression`))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5))+
  scale_color_gradient(low="lightgrey",high="blue")+
  labs(x=NULL,y=NULL)+
  guides(size=guide_legend(order=3))

以y轴为变量,做层次聚类,并使用ggtree展示层次聚类结果

聚类用到的是平均表达量那一列
df<-data.final[,c(1,2,4)]
首先是长格式数据转换为宽格式
df1<-reshape2::dcast(df,id~features.plot,value.var = "Average expression")
rownames(df1)<-df1$id
df1.1<-df1[,2:22]

层次聚类,ggtree展示结果
df1.1.clust<-hclust(dist(df1.1))
df2.1.clust<-hclust(dist(df2.1))
library(ggtree)
p2<-ggtree(df1.1.clust)
p2+
  geom_tiplab()+
  xlim(NA,7)

使用aplot包拼图
library(ggplot2)
p1<-ggplot(data.final,aes(x=features.plot,y=id))+
  geom_point(aes(size=`Percent expressed`,
                 color=`Average expression`))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5))+
  scale_color_gradient(low="lightgrey",high="blue")+
  labs(x=NULL,y=NULL)+
  guides(size=guide_legend(order=3))

library(aplot)
p1%>%
  insert_left(p2,width = 0.2)

接下来就是在上方叠加聚类树,一样的操作
df2<-reshape2::dcast(df,features.plot~id,value.var = "Average expression")
rownames(df2)<-df2$features.plot
df2.1<-df2[,2:15]
df2.1.clust<-hclust(dist(df2.1))
p3<-ggtree(df2.1.clust)+
  #geom_tiplab(angle=90)+
  #theme_tree2()+
  layout_dendrogram()
p3
p1%>%
  insert_left(p2,width = 0.2)%>%
  insert_top(p3,height = 0.2)

这里多了一个知识点是ggtree作图默认开口树的方向是向右,如果需要把开口改成向下,需要加上layout_dendrogram()函数

最终的结果如下

举报

相关推荐

0 条评论