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

如何调整ggplot热力图填充颜色:按元主题区分并映射rel_impact

问题

需要制作一张热力图,展示某报纸一段时间内6个不同元主题(Metatopic)的报道强度,要求每个元主题有专属颜色,颜色深浅对应rel_impact变量(该主题相对其他主题的报道强度)。当前仅能修改方块边框颜色区分主题,希望将方块填充色改为「Meta topics」图例对应的颜色渐变,而非灰色渐变。

当前代码:

my_palette <- RColorBrewer::brewer.pal(6, 'Dark2')

metatopic_data %>%
  ggplot(aes(x = date, y = Metatopic, color=Metatopic,fill = rel_impact)) + 
geom_tile() +
  scale_x_date(date_breaks = "1 year", date_labels = "%Y",expand = c(0,0)) +
  scale_y_discrete(expand=c(0,0)) +
  scale_colour_brewer(palette = "Dark2", name="Meta topics") +
  scale_fill_gradient(low = "white",high = "black", name=NULL) +
  guides(color = guide_legend(override.aes = list(fill = my_palette))) +
  theme_light(base_size = 11) +
  labs(x=NULL, y=NULL)

数据结构复现代码:

structure(list(date = structure(c(14760, 14760, 14760, 14760, 
14760, 14760), class = "Date"), Metatopic = c("Career", "Economics", 
"Industries", "Leisure", "Politics", "Sport"), abs_impact = c(0.00531062385448913, 
0.0569595367458113, 0.0459819861634464, 0.00889034813748066, 
0.0750210871815098, 0.00406422677142547), sum = c(0.196227808854163, 
0.196227808854163, 0.196227808854163, 0.196227808854163, 0.196227808854163, 
0.196227808854163), rel_impact = c(0.0270635639540571, 0.290272500510587, 
0.234329611240884, 0.0453062600525087, 0.382316286461037, 0.0207117777809261
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), groups = structure(list(date = structure(14760, class = "Date"), 
    .rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))
解决方案

核心思路是为每个元主题生成专属的颜色渐变(从白色到主题色),将rel_impact数值映射到对应渐变上,实现主题颜色随报道强度深浅变化的效果。

修改后的完整代码

library(ggplot2)
library(RColorBrewer)
library(dplyr)
library(purrr)

# 为每个Metatopic分配专属基础颜色,并命名匹配主题
my_palette <- brewer.pal(6, 'Dark2')
names(my_palette) <- c("Career", "Economics", "Industries", "Leisure", "Politics", "Sport")

# 为每个主题创建从白色到主题色的颜色渐变函数
color_ramp_funs <- lapply(my_palette, function(col) scales::colour_ramp(c("white", col)))

# 预处理数据:将rel_impact转换为对应主题的渐变填充色
metatopic_data_processed <- metatopic_data %>%
  mutate(fill_color = pmap_chr(list(Metatopic, rel_impact), function(topic, val) {
    rgb(color_ramp_funs[[topic]](val), maxColorValue = 255)
  }))

# 绘制热力图
ggplot(metatopic_data_processed, aes(x = date, y = Metatopic)) + 
  geom_tile(aes(fill = fill_color), color = "white") + # 白色边框增强方块区分度
  scale_x_date(date_breaks = "1 year", date_labels = "%Y", expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0)) +
  # 配置填充色图例,对应rel_impact的百分比显示
  scale_fill_identity(
    name = "Relative Impact",
    guide = guide_colorbar(barwidth = 15, barheight = 0.5),
    labels = scales::percent_format(accuracy = 1)
  ) +
  # 配置元主题颜色图例
  scale_color_manual(
    name = "Meta topics",
    values = my_palette,
    guide = guide_legend(override.aes = list(fill = my_palette))
  ) +
  theme_light(base_size = 11) +
  labs(x = NULL, y = NULL) +
  # 调整图例顺序:主题图例优先显示
  guides(color = guide_legend(order = 1), fill = guide_colorbar(order = 2))

关键改动说明

  1. 主题渐变函数生成:使用scales::colour_ramp为每个Metatopic创建从白色到主题色的渐变规则,确保颜色深浅与rel_impact数值正相关。
  2. 数据颜色映射:通过pmap_chr将每个数据点的rel_impact转换为对应主题的具体颜色值。
  3. 图例优化:保留元主题的颜色图例,同时添加rel_impact的颜色条图例,调整顺序让主题信息更突出。
  4. 视觉区分:将方块边框设为白色,避免不同主题的色块混淆。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 10:27:50