使用 GGplot 进行注释的热图

问题描述 投票:0回答:1

我正在尝试在 R 中为微生物家族中的物种制作热图,样本 ID 为 x,物种名称为 y,顶部有两个分面因子组。我想要像下图这样的东西

这是我到目前为止所能做的:

ad = tidyr::pivot_wider(Intrasporangiaceae, names_from = 'Species',values_from='Abundance')         
     ad[is.na(ad)] <- 0
     ad = as.matrix(ad[,-c(1:11)])
adscaled <- scale(ad)
Aclust <- hclust(dist(t(ad)))

ggplot(Intrasporangiaceae, aes(Sorghum_Variety, Species, fill = Abundance)) + 
    geom_tile(width=1.0) +
    facet_grid(~Striga_Infestation_Status) +
    
    scale_fill_gradientn(name= 'Abundance',
                         colours = c("#373898ff", "pink","#c11630ff"), 
                         values = c(0,0.1,1), expand = c(0,0), limit = c(0,NA)) +
    scale_y_discrete(limits = colnames(ad)[Aclust$order]) +
    labs(title = paste('Species Abundance in', family_Name), x='', y='') +
    theme(legend.position='right',panel.background=element_blank(),
          axis.line = element_blank(),
          axis.ticks = element_blank(),
          axis.text.x = element_text(size = 8,angle = 90, hjust = 1),
          axis.text.y= element_markdown(),
          legend.text = element_text(size=7),
          legend.key.height = unit(10,'pt'),
          legend.title = element_text(size = 10),
          strip.text = element_text(size = 10),
          axis.title.x = element_text(size = 8),
          axis.title.y = element_text(size = 10),
          plot.title = element_text(size=15, hjust = 0.5)) +
    coord_fixed(ratio = 0.5) +
    theme(panel.spacing = unit(0.04, 'lines'))

我想要什么: What I want 我期待您的帮助。提前致谢。 此处提供了示例数据:https://drive.google.com/file/d/1Y-D2h3mYNOpBjAPaqdLmTcGtHtOS-F2z/view?usp=sharing`

ggplot2 heatmap geom-tile complexheatmap d3heatmap
1个回答
0
投票

ggh4x
包可以帮助您创建具有嵌套面的热图

library(ggplot2)

ggplot(df, aes(SampleID, Species, fill = Abundance)) +
  geom_tile(width = 1) +
  scale_fill_gradientn(name= 'Abundance',
                       colours = c("#373898ff", "pink","#c11630ff"), 
                       values = c(0,0.1,1), expand = c(0,0), limit = c(0,NA)) +
  ggh4x::facet_nested(~ Sorghum_Variety + Striga_Infestation_Status,
               scales = 'free_x', space = 'free_x') +
  theme(axis.text.x = element_text(angle = 60, hjust = 1))

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.