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

如何绘制与DataFrame单元格位置一致、按值着色的可视化图表?

按DataFrame单元格位置绘制自定义着色的热力图(R语言)

完全可以实现!我们可以用ggplot2配合数据格式转换,做出和你原DataFrame单元格位置一一对应的热力图,同时自定义颜色映射——0对应蓝色,±1对应红色。

步骤说明:

  1. 先把宽格式的DataFrame转换成长格式,让ggplot2能识别行列位置和对应的值;
  2. 自定义颜色渐变尺度,指定0为蓝色,向±1渐变到红色;
  3. 调整图表主题,让坐标轴的标签和位置完全匹配原DataFrame的行列。

完整代码:

# 加载所需包
library(tidyverse)

# 你的DataFrame(确保数据加载正确)
df <- structure(list(`0` = 1:8, `1` = c(0.885, 0.695, 0.828, 0.888, 0.823, 0.231, 0.599, 0.153), `2` = c(0.834, 0.68, 0.857, 0.802, 0.734, 0.205, 0.62, 0.044), `3` = c(0.854, 0.66, 0.83, 0.829, 0.729, 0.159, 0.559, 0.081), `4` = c(0.87, 0.583, 0.778, 0.853, 0.75, 0.087, 0.515, -0.011), `5` = c(0.922, 0.739, 0.787, 0.805, 0.635, -0.017, 0.498, -0.204), `6` = c(0.815, 0.535, 0.833, 0.784, 0.803, 0.092, 0.502, -0.419), `7` = c(0.859, 0.517, 0.8, 0.829, 0.557, 0.22, 0.368, -0.42), `8` = c(0.86, 0.701, 0.701, 0.786, 0.567, 0.414, 0.324, -0.396), `9` = c(0.774, 0.781, 0.805, 0.862, 0.405, 0.852, 0.1, -0.448), `10` = c(0.869, 0.788, 0.837, 0.838, 0.481, 0, -0.072, -0.48), `11` = c(0.816, 0.795, 0.807, 0.744, 0.217, 0, 0.096, -0.346), `12` = c(0.829, 0.792, 0.774, 0.778, 0.003, 0, 0, 0), `13` = c(0.799, 0.84, 0.775, 0.66, -0.024, 0, 0, 0), `14` = c(0.842, 0.765, 0.852, 0.679, 0, 0, 0, 0), `15` = c(0.804, 0.811, 0.818, 0.468, 0, 0, 0, 0), `16` = c(0.801, 0.757, 0.715, 0.091, 0, 0, 0, 0), `17` = c(0.807, 0.786, 0.799, -0.042, 0, 0, 0, 0), `18` = c(0.595, 0.795, 0.73, 0, 0, 0, 0, 0), `19` = c(0.822, 0.789, 0.623, 0, 0, 0, 0, 0), `20` = c(0.829, 0.822, 0.048, 0, 0, 0, 0, 0), `21` = c(0.805, 0.788, -0.205, 0, 0, 0, 0, 0), `22` = c(0.788, 0.791, -0.065, 0, 0, 0, 0, 0), `23` = c(0.839, 0.786, -0.217, 0, 0, 0, 0, 0), `24` = c(0.804, 0.815, 0, 0, 0, 0, 0, 0), `25` = c(0.789, 0.784, 0, 0, 0, 0, 0, 0), `26` = c(0.754, 0.787, 0, 0, 0, 0, 0, 0), `27` = c(0.832, 0.741, 0, 0, 0, 0, 0, 0), `28` = c(0.846, 0.778, 0, 0, 0, 0, 0, 0), `29` = c(0.797, 0.69, 0, 0, 0, 0, 0, 0), `30` = c(0.843, 0.644, 0, 0, 0, 0, 0, 0), `31` = c(0.825, 0.622, 0, 0, 0, 0, 0, 0), `32` = c(0.824, 0.726, 0, 0, 0, 0, 0, 0), `33` = c(0.749, 0.493, 0, 0, 0, 0, 0, 0), `34` = c(0.774, -0.082, 0, 0, 0, 0, 0, 0), `35` = c(0.652, -0.255, 0, 0, 0, 0, 0, 0), `36` = c(0.833, 0, 0, 0, 0, 0, 0, 0), `37` = c(0.795, 0, 0, 0, 0, 0, 0, 0), `38` = c(0.864, 0, 0, 0, 0, 0, 0, 0), `39` = c(0.226, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -8L), class = "data.frame")

# 转换为长格式,保留行号和列名信息
df_long <- df %>%
  mutate(row = row_number()) %>%  # 添加行号,对应原DataFrame的行
  pivot_longer(cols = -row, names_to = "col", values_to = "value") %>%
  mutate(col = as.integer(col))  # 把列名转为整数,保证顺序正确

# 绘制热力图
ggplot(df_long, aes(x = col, y = row, fill = value)) +
  geom_tile(color = "white") +  # 白色边框区分单元格
  # 自定义颜色渐变:0为蓝色,-1和1为红色
  scale_fill_gradient2(low = "red", mid = "blue", high = "red", 
                       midpoint = 0, limits = c(-1, 1)) +
  # 调整坐标轴,让行号从上到下和原DataFrame一致(ggplot默认y轴从下到上)
  scale_y_reverse(breaks = 1:8) +
  scale_x_continuous(breaks = 0:39) +
  # 调整主题,去掉不必要的元素,让图表更简洁
  theme_minimal() +
  theme(
    axis.text = element_text(size = 8),
    panel.grid = element_blank(),
    legend.position = "right"
  ) +
  labs(x = "列", y = "行", fill = "值")

代码解释:

  • pivot_longer把宽格式转成ggplot需要的长格式,同时用row_number()记录原DataFrame的行位置;
  • scale_fill_gradient2是核心,它支持设置中点颜色(这里0对应蓝色)、两端颜色(±1对应红色),完美匹配你的需求;
  • scale_y_reverse反转y轴,因为ggplot默认y轴从下到上,反转后行位置和原DataFrame完全对应;
  • geom_tile的color="white"给每个单元格加白色边框,和常规表格样式更贴近。

运行这段代码后,你就能得到一个和原DataFrame单元格位置完全一致、颜色符合要求的图表啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:06:38