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

如何在R中创建指定轴与填充变量的自定义热力图?

制作指定样式的热力图R代码

需求说明

  • 参考样式:色块型热力图,以横轴和纵轴变量的组合划分区域,色块颜色对应指定数值
  • 数据映射规则:
    • 横轴(x轴):mean_temp
    • 纵轴(y轴):total_count
    • 色块填充颜色:lwd_duration

可复现数据

df <- structure(
  list(
    total_count = c(
      10L,
      0L,
      15L,
      0L,
      20L,
      0L,
      0L,
      50L,
      0L,
      6L,
      1L,
      10L,
      7L,
      0L,
      0L,
      29L,
      0L,
      2L,
      11L,
      3L,
      0L,
      12L,
      0L,
      30L,
      0L,
      0L,
      29L,
      44L,
      10L,
      5L,
      2L,
      145L,
      0L,
      70L
    ),
    mean_temp = c(
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82
    ),
    lwd_duration = c(
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43
    )
  ),
  row.names = c(NA,-34L),
  class = c("tbl_df", "tbl", "data.frame"),
  na.action = structure(
    c(
      `4` = 4L,
      `5` = 5L,
      `6` = 6L,
      `7` = 7L,
      `8` = 8L,
      `9` = 9L,
      `78` = 78L,
      `87` = 87L,
      `96` = 96L,
      `105` = 105L,
      `114` = 114L,
      `123` = 123L,
      `132` = 132L,
      `141` = 141L,
      `150` = 150L,
      `159` = 159L,
      `168` = 168L,
      `177` = 177L,
      `186` = 186L,
      `849` = 849L,
      `850` = 850L,
      `851` = 851L,
      `852` = 852L,
      `891` = 891L,
      `892` = 892L,
      `893` = 893L,
      `894` = 894L,
      `921` = 921L,
      `922` = 922L,
      `923` = 923L,
      `924` = 924L,
      `937` = 937L,
      `938` = 938L,
      `939` = 939L,
      `940` = 940L,
      `969` = 969L,
      `970` = 970L,
      `971` = 971L,
      `972` = 972L,
      `985` = 985L,
      `986` = 986L,
      `987` = 987L,
      `988` = 988L,
      `1017` = 1017L,
      `1018` = 1018L,
      `1019` = 1019L,
      `1020` = 1020L,
      `1033` = 1033L,
      `1034` = 1034L,
      `1035` = 1035L,
      `1036` = 1036L
    ),
    class = "omit"
  )
)

实现代码

使用ggplot2包绘制色块型热力图,先对数据去重避免重复绘制,再设置颜色映射和主题样式:

# 加载所需包
library(ggplot2)
library(dplyr)

# 数据去重:同一mean_temp和total_count组合只保留一条记录
df_clean <- df %>% 
  distinct(mean_temp, total_count, lwd_duration)

# 绘制热力图
ggplot(df_clean, aes(x = mean_temp, y = total_count)) +
  # 添加色块层
  geom_tile(aes(fill = lwd_duration), color = "white", size = 0.5) +
  # 设置颜色渐变(可根据需求调整配色)
  scale_fill_viridis_c(option = "plasma", name = "lwd_duration") +
  # 设置坐标轴标签
  labs(x = "mean_temp", y = "total_count") +
  # 设置主题,贴近参考样式(可根据喜好调整)
  theme_minimal() +
  theme(
    panel.grid = element_blank(),
    axis.text = element_text(size = 10),
    axis.title = element_text(size = 12, face = "bold"),
    legend.title = element_text(face = "bold")
  )

代码说明

  1. 数据去重:原数据存在大量重复的mean_temp和total_count组合,去重后避免色块重叠
  2. geom_tile():绘制矩形色块,color参数设置色块边框颜色,size调整边框粗细
  3. 颜色映射:使用viridis配色方案,保证颜色辨识度和色盲友好性,可替换为其他配色如scale_fill_gradient()
  4. 主题调整:移除网格线,调整文字大小和样式,让图表更简洁清晰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:24:55