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

如何在单幅图中为每个TF_variant重复绘制相同密度背景图?

解决方案

问题分析

你当前的代码仅在y=1的位置绘制了全局密度图,要让每个TF_variant行都显示相同的背景密度,需要先单独计算全局密度数据,再将其与每个TF_variant绑定后绘制。

修改后代码

# 1. 计算整个数据集的mean_LFC密度数据
global_density <- density(significant_genes_df$mean_LFC, n = 1000)
density_df <- data.frame(
  x = global_density$x,
  density = global_density$y
)

# 2. 为每个TF_variant复制一份密度数据
tf_variants <- unique(significant_genes_df$TF_variant)
density_full <- purrr::map_dfr(tf_variants, function(tf) {
  cbind(density_df, TF_variant = tf)
})

# 3. 绘制图形
ggplot() +
  # 绘制每个TF_variant行的密度背景
  geom_raster(data = density_full, aes(x = x, y = TF_variant, fill = density), alpha = 0.8) +
  # 叠加当前TF_variant的mean_LFC红色竖线
  geom_point(data = significant_genes_df, aes(x = mean_LFC, y = TF_variant), 
             shape = "|", color = "red", alpha = 0.45, size = 3, 
             position = position_jitter(height = 0)) +
  # 添加x=0的虚线
  geom_vline(xintercept = 0, linetype = "dotted", color = "steelblue3") +
  # 样式调整
  scale_fill_gradient(low = "gray95", high = "gray35", name = "Density") +
  scale_y_discrete(expand = c(0, 0.5)) +
  scale_x_continuous(expand = c(0, 0)) +
  theme_ridges(font_size = 14, font_family = "", grid = FALSE, center = TRUE)

代码说明

  • 第一步单独计算全局密度,确保所有行共用同一个分布数据
  • 第二步通过purrr::map_dfr将密度数据复制到每个TF_variant分组,让每个y轴行都对应完整的密度坐标
  • 绘图时先绘制密度背景(geom_raster),再叠加红色点,保证图层层级正确

内容的提问来源于stack exchange,提问作者Jinsoul Jung

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 23:17:07