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

如何用ggplot2的geom_point实现细胞生物学风格的双色融合绘图

嘿,这个细胞成像里的双色共定位图需求太典型了!我来手把手教你用ggplot2的geom_point实现这种渐变融合效果,完全贴合你要的“绿蛋白单图、红蛋白单图、融合黄渐变图”的展示逻辑:

核心思路

每个点的颜色由两种蛋白的荧光强度(或表达量)直接混合:用RGB颜色模型,绿色通道对应蛋白1的强度,红色通道对应蛋白2的强度,蓝色通道固定为0。这样:

  • 只有蛋白1的点:绿色通道值高,红色低 → 纯绿
  • 只有蛋白2的点:红色通道值高,绿色低 → 纯红
  • 两者都有的点:红绿通道都高 → 混合出黄色,中间强度则呈现平滑的渐变过渡

步骤1:准备模拟数据

先造一个包含细胞坐标、两种蛋白强度的数据集(你可以替换成自己的真实数据):

library(tidyverse)

# 生成模拟数据:1000个细胞的坐标+两种蛋白强度(0-1区间)
set.seed(123)
cell_data <- tibble(
  x = rnorm(1000, mean = 0, sd = 2),
  y = rnorm(1000, mean = 0, sd = 2),
  green_intensity = runif(1000, 0, 1),
  red_intensity = runif(1000, 0, 1)
)

# 计算每个点的混合RGB颜色
cell_data <- cell_data %>%
  mutate(
    col = rgb(green = green_intensity, red = red_intensity, blue = 0)
  )

步骤2:绘制融合共定位图

用geom_point直接映射计算好的RGB颜色:

# 融合图:双色渐变共定位
fusion_plot <- ggplot(cell_data, aes(x = x, y = y)) +
  geom_point(color = cell_data$col, size = 3, alpha = 0.8) +
  labs(title = "蛋白共定位融合图", x = "", y = "") +
  theme_minimal() +
  theme(panel.grid = element_blank())

fusion_plot

步骤3:绘制单通道对比图

为了对应你说的“分别展示单蛋白图像”,可以单独绘制绿色通道和红色通道的图:

# 绿色蛋白单图
green_plot <- ggplot(cell_data, aes(x = x, y = y, color = green_intensity)) +
  geom_point(size = 3, alpha = 0.8) +
  scale_color_gradient(low = "black", high = "green") +
  labs(title = "仅绿色蛋白", x = "", y = "") +
  theme_minimal() +
  theme(panel.grid = element_blank(), legend.position = "none")

# 红色蛋白单图
red_plot <- ggplot(cell_data, aes(x = x, y = y, color = red_intensity)) +
  geom_point(size = 3, alpha = 0.8) +
  scale_color_gradient(low = "black", high = "red") +
  labs(title = "仅红色蛋白", x = "", y = "") +
  theme_minimal() +
  theme(panel.grid = element_blank(), legend.position = "none")

# 把三个图拼在一起(用patchwork包)
library(patchwork)
green_plot + red_plot + fusion_plot + plot_layout(nrow = 1)

关键细节说明

  • 这里的rgb()函数是核心:它把0-1的强度值直接转换成对应颜色通道的亮度,完美模拟荧光成像的颜色混合逻辑
  • 如果你的强度值不是0-1区间,可以用scales::rescale()先归一化:rescale(your_intensity, to = c(0,1))
  • 调整size和alpha参数可以让点的显示更接近真实成像的效果

内容的提问来源于stack exchange,提问作者Eric Foss

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:35:17