ggtree+ggplot做的样品的分布图

作者: 一只烟酒僧 | 来源:发表于2021-10-31 15:17 被阅读0次
    library(ggtree)
    library(ape)
    library(patchwork)
    library(ggplot2)
    library(dplyr)
    library(ggprism)
    library(reshape2)
    library(cowplot)
    #示例数据
    data("iris")
    set.seed(2021)
    iris<-iris[sample(1:150,40,replace = F),]
    
    #树
    rownames(iris)<-paste0("Sample",rownames(iris))
    hclust(dist(iris[,1:4]))%>%
      ggtree(size=1.5)+
      theme(plot.margin = margin(r=0,unit = "cm")) ->x
    
    #这里调整了margin,为了更好地与热图合并
    
    
    #特征矩阵
    a<-hclust(dist(iris[,1:4]))
    iris_feature<-iris[a$order,]%>%
      mutate(id=factor(rownames(.),levels = rownames(.)),
             type1=sample(c("KO","WT"),nrow(.),replace = T),
             type2=sample(c("Group1","Group2","Group3"),nrow(.),replace = T),
             type3=sample(c("Group1","Group2","Group3"),nrow(.),replace = T),
             type4=.$Species)%>%
      select(c("id",paste0("type",1:4)))%>%
      melt(id.vars="id",measure.vars=2:5,variable.name="type",value.name="character")
    
    iris_feature%>%
      ggplot(.,aes(x=type,y=id))+
      geom_tile(aes(fill=character),width=0.9)+
      labs(y="")+
      theme_prism()+
      theme(axis.text.x = element_text(angle = 90,hjust = -0.5),
                          axis.text.y = element_text(hjust = 0),plot.margin = margin(r=0,l=-1,unit = "cm"))->y
    
    #这里设置hjust=0是为了左对齐,这样不管样本名称有多长,都不会影响最后出图的效果
    
    #合并
    plot_grid(x,y,nrow = 1,align = "hv",rel_widths = c(1,4))
    
    
    #使用patchwork的话,树形图和条形图中间会有较大的空白区域,没法调整,这里通过使用crowplot以及margin进行调整
    
    image.png

    相关文章

      网友评论

        本文标题:ggtree+ggplot做的样品的分布图

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