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

如何使用ggplot2复现带渐变阴影区域的参数范围热图

如何使用ggplot2复现带渐变阴影区域的参数范围热图

嘿,我来帮你搞定这个渐变阴影热图的复现问题!你说用geom_tile()出来的图太“块儿化”,确实,因为geom_tile是画实心矩形,没法体现那种带不确定性的渐变阴影感。咱们换个思路,用两种方法都能还原出你想要的效果,我给你一步步说清楚:

方法一:用密度阴影模拟不确定性(更贴合原图的“模糊阴影”感)

这个方法的核心是先在每个疾病的参数范围内生成随机点,再用密度图画出渐变的阴影区域——密度越高的地方(参数最可能的取值)颜色越深,完美体现不确定性的大小,还不会像geom_tile那样生硬。

先上代码:

library(ggplot2)
library(dplyr)

# 你的原始数据
data <- data.frame(
  Disease = c("Influenza", "Smallpox", "SARS", "HIV"),
  R0_Min = c(1, 4, 1, 1),
  R0_Max = c(21, 10, 7, 5),
  Theta_Min = c(30, 5, 0, 80),
  Theta_Max = c(50, 20, 10, 100)
)

# 给每个疾病指定对应方差的基础颜色
color_mapping <- c(
  "Influenza" = "#E41A1C",
  "Smallpox" = "#377EB8",
  "SARS" = "#4DAF4A",
  "HIV" = "#984EA3"
)

# 绘制核心图形
ggplot() +
  # 生成参数范围内的随机点,绘制密度渐变阴影
  stat_density2d(
    data = data %>%
      rowwise() %>%
      reframe(
        R0 = runif(1000, R0_Min, R0_Max),
        Theta = runif(1000, Theta_Min, Theta_Max),
        Disease = Disease
      ),
    aes(x = R0, y = Theta, fill = after_stat(level), color = Disease),
    geom = "polygon",
    alpha = 0.7,
    bins = 5
  ) +
  # 加上参数范围的黑色边框,明确区间边界
  geom_rect(
    data = data,
    aes(
      xmin = R0_Min, xmax = R0_Max,
      ymin = Theta_Min, ymax = Theta_Max
    ),
    fill = NA,
    color = "black",
    linewidth = 0.5
  ) +
  # 颜色映射:填充色对应不确定性密度,边框色对应疾病
  scale_fill_viridis_c(option = "plasma", alpha = 0.6, name = "Uncertainty Level") +
  scale_color_manual(values = color_mapping, name = "Disease") +
  # 坐标轴和标签设置
  labs(
    x = expression(R[0]),
    y = expression(theta~"(%)")
  ) +
  # 主题优化,让图更清爽
  theme_minimal() +
  theme(
    legend.position = "bottom",
    panel.grid = element_blank()
  )

方法二:渐变填充矩形(更贴近“实心阴影区域”的需求)

如果你更想还原原图的实心渐变色块,直接用geom_rect配合颜色映射+透明度调整就行,代码更简洁:

library(ggplot2)

# 你的原始数据
data <- data.frame(
  Disease = c("Influenza", "Smallpox", "SARS", "HIV"),
  R0_Min = c(1, 4, 1, 1),
  R0_Max = c(21, 10, 7, 5),
  Theta_Min = c(30, 5, 0, 80),
  Theta_Max = c(50, 20, 10, 100)
)

# 给每个疾病分配对应方差的渐变颜色
fill_colors <- c(
  "Influenza" = "#ff9999",
  "Smallpox" = "#99ccff",
  "SARS" = "#99ff99",
  "HIV" = "#cc99ff"
)

ggplot(data) +
  # 画渐变填充的参数范围矩形
  geom_rect(
    aes(
      xmin = R0_Min, xmax = R0_Max,
      ymin = Theta_Min, ymax = Theta_Max,
      fill = Disease
    ),
    alpha = 0.6  # 调整透明度,让阴影感更柔和
  ) +
  # 加上疾病名称标注在矩形中心
  geom_text(
    aes(
      x = (R0_Min + R0_Max)/2,
      y = (Theta_Min + Theta_Max)/2,
      label = Disease
    ),
    color = "white",
    fontface = "bold"
  ) +
  # 颜色映射和标签设置
  scale_fill_manual(values = fill_colors, name = "Disease") +
  labs(
    x = expression(R[0]),
    y = expression(theta~"(%)")
  ) +
  theme_minimal() +
  theme(panel.grid = element_blank())

额外小技巧:让颜色直接对应方差值

原图里颜色是对应β(τ)和S(τ)的方差值,你可以给数据加一列方差,然后用渐变颜色映射:

# 给数据添加方差列(示例值,你可以替换成真实的方差数据)
data$Variance <- c(0.8, 0.6, 0.3, 0.9)

ggplot(data) +
  geom_rect(
    aes(
      xmin = R0_Min, xmax = R0_Max,
      ymin = Theta_Min, ymax = Theta_Max,
      fill = Variance
    ),
    alpha = 0.7
  ) +
  scale_fill_gradient(low = "#f7fbff", high = "#08306b", name = "Variance") +
  # 其他设置和之前一致
  labs(x = expression(R[0]), y = expression(theta~"(%)")) +
  theme_minimal()

你可以根据自己的需求选其中一种方法,细节比如颜色、透明度、图例位置都可以随时调整参数哦!

备注:内容来源于stack exchange,提问作者jward183

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.17 08:19:33