You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在热图上方绘制geom_line图?昼夜节律数据可视化求助

解决行为折线图与昼夜节律基因热图的合并布局及红点定位问题

我明白你现在遇到的问题:想用par(mfrow)把行为折线图和热图上下排列,但因为ggplot和基础绘图系统不兼容,导致两个图分开显示,同时需要确保红点精准对应CT13和CT37的位置。下面是具体的解决方案:

问题根源

ggplot基于grid绘图系统,而par(mfrow)是基础绘图系统的布局命令,两者无法互通,所以你的代码会生成两个独立的图,而不是合并布局。此外,要确保红点定位准确,需要确认行为数据中CT的顺序和高亮标记的对应关系。

解决方案:使用patchwork包实现组合布局

patchwork是专门为ggplot和grid对象设计的布局工具,可以轻松实现多图组合,同时兼容pheatmap的输出(需要将pheatmap转换为grid对象)。

完整修改代码

# 加载必要包
library(pheatmap)
library(RColorBrewer)
library(ggplot2)
library(patchwork)
library(gridGraphics)

# ----------------------
# 1. 准备热图数据
# ----------------------
my_heatmap <- read.csv("circ_genes.csv", header = TRUE)
mymat <- my_heatmap[,c(2:13)]
rownames(mymat) <- paste("Gene", my_heatmap[,1], sep="_")
mydf <- data.frame(row.names = paste("Gene", my_heatmap[,1], sep="_"), category = my_heatmap[,14])
my_palette <- colorRampPalette(c("blue", "white", "red"))(n = 1000)

# ----------------------
# 2. 准备行为折线图数据(确保红点对应CT13和CT37)
# ----------------------
behavior_LD <- read.csv("behavior.csv", header = TRUE)
# 确保CT按原始顺序排序,同时确认CT13和CT37在数据中存在
behavior_LD$CT <- factor(behavior_LD$CT, levels = unique(behavior_LD$CT))
# 确认highlight列仅在CT13和CT37时标记为"H1"(如果数据中没设置,可手动添加)
behavior_LD$highlight <- ifelse(behavior_LD$CT %in% c("CT13", "CT37"), "H1", "H0")

mycolours <- c("H1" = "red", "H0" = "black")
behavePlot <- ggplot(data=behavior_LD, aes(x=CT, y=Average, group=1)) + 
  geom_line() +
  geom_point(aes(colour = factor(highlight)), size = 3) +
  geom_errorbar(aes(ymin=Average-SE, ymax=Average+SE), width=.1)+
  scale_color_manual(values = mycolours) +
  theme(legend.position="none") +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
  theme(plot.margin=unit(c(1,2,0,2),"cm")) # 调整底部边距,让两个图更紧凑

# ----------------------
# 3. 将pheatmap转换为grid对象(兼容patchwork)
# ----------------------
grid.newpage()
pheatmap_obj <- pheatmap(mymat, 
                         color = my_palette, 
                         legend_labels = "Clusters", 
                         cluster_cols = F, 
                         cluster_rows = F, 
                         annotation_row = mydf, 
                         gaps_row = c(43, 60, 88, 124), 
                         annotation_names_row = FALSE, 
                         show_rownames = FALSE )
heatmap_grob <- grid.grab()

# ----------------------
# 4. 组合两个图,行为图在上,热图在下
# ----------------------
combined_plot <- behavePlot / heatmap_grob
# 调整上下图的高度比例(可根据需求修改)
combined_plot + plot_layout(heights = c(1, 3))

# 保存组合图(可选)
ggsave("combined_circ_plot.pdf", combined_plot, width = 10, height = 12)

关键修改说明

  1. 布局兼容处理:用gridGraphics::grid.grab()将pheatmap的输出转换为grid对象,这样就能被patchwork识别和组合,替代无效的par(mfrow)。
  2. 红点精准定位:通过ifelse手动确保只有CT13和CT37对应的点被标记为红色(如果你的原始数据中highlight列已经正确设置,可以跳过这一步)。
  3. 间距优化:调整行为图的底部边距,让两个图的间距更自然,同时通过plot_layout(heights)控制上下图的高度比例。

内容的提问来源于stack exchange,提问作者Eviatar Nov Weizman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:25:13