基于ggplot绘制带双分组注释的微生物物种丰度热图求助
R语言ggplot微生物物种丰度热图优化
需求概述
- X轴为样本ID,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'))
核心问题与优化方向
- X轴变量错误:当前用
Sorghum_Variety作为X轴,需替换为样本ID列(需根据实际数据确认列名,如Sample_ID) - 双分组分面缺失:仅实现了单因子分面,需添加第二个分组因子完成顶部双行分面布局
- 细节样式不匹配:热图边框、分面间距、轴文本布局需向目标图对齐
优化后代码示例
library(tidyverse) library(ggplot2) # 数据预处理:确认样本ID列名,此处假设为Sample_ID dat <- Intrasporangiaceae %>% mutate(Sample_ID = as.factor(Sample_ID)) # 物种聚类排序 ad_wide <- dat %>% pivot_wider(names_from = Species, values_from = Abundance, values_fill = 0) ad_matrix <- as.matrix(ad_wide %>% select(-c(1:11))) ad_scaled <- scale(ad_matrix) species_clust <- hclust(dist(t(ad_scaled))) sorted_species <- colnames(ad_matrix)[species_clust$order] # 绘制优化后热图 ggplot(dat, aes(x = Sample_ID, y = Species, fill = Abundance)) + geom_tile(color = "white", linewidth = 0.2) + # 双因子分面:将两个分组标签移至X轴上方,按需调整因子顺序 facet_grid(Striga_Infestation_Status ~ Sorghum_Variety, switch = "x") + scale_fill_gradientn( name = "丰度", colours = c("#373898ff", "pink", "#c11630ff"), values = c(0, 0.1, 1), expand = c(0, 0), limits = c(0, NA) ) + scale_y_discrete(limits = sorted_species) + scale_x_discrete(expand = c(0, 0)) + labs( title = paste(family_Name, "科物种丰度热图"), x = "", y = "" ) + theme_minimal() + theme( legend.position = "right", axis.text.x = element_text(size = 8, angle = 90, hjust = 1, vjust = 0.5), axis.text.y = element_text(size = 8), strip.text.x = element_text(size = 10, face = "bold"), strip.text.y = element_blank(), plot.title = element_text(size = 15, hjust = 0.5, face = "bold"), panel.spacing = unit(0.3, "lines"), axis.ticks = element_blank(), panel.grid = element_blank() ) + coord_fixed(ratio = 0.6)
关键调整说明
- 修正X轴变量为样本ID,确保每个样本对应独立tile
- 使用
facet_grid(因子1 ~ 因子2, switch = "x")实现顶部双分组分面 - 添加tile白色边框、移除网格线、调整分面间距,匹配目标图视觉风格
- 需根据实际数据列名调整分组因子、样本ID的变量名
内容的提问来源于stack exchange,提问作者user8019548
相关产品推荐
相关产品推荐

