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

R语言热力图优化:筛选2022年1月后数据及消除月份间隙

问题与解决方案

需求概述

基于给定数据集绘制热力图,要求:

  • X轴展示2022年1月起的time字段
  • Y轴按字母顺序排列branques字段
  • 颜色填充规则:数值越负越红,数值为正时显示绿色

示例数据集:

structure(list(time = structure(c(19327, 19358, 19389, 19417, 
19448, 12053), class = "Date"), branques = c("index", "index", 
"index", "index", "index", "Indústries extractives"), values = c(-1.1, 
4, -1.6, 1.1, -6.5, 20)), row.names = c(252L, 253L, 254L, 255L, 
256L, 269L), class = "data.frame")

现有代码存在两个问题:

  1. 如何仅筛选2022年1月之后的数据进行可视化?
  2. 如何消除月份之间的空隙?

解决方案

1. 筛选2022年1月之后的数据

直接在传入ggplot前对数据集做子集筛选,两种方式任选:

  • 基础R写法:
# 筛选符合时间条件的数据
filtered_data <- varIPI_branques_CAT_long[varIPI_branques_CAT_long$time >= as.Date("2022-01-01"), ]
  • dplyr包写法(需先加载dplyr):
library(dplyr)
filtered_data <- varIPI_branques_CAT_long %>% filter(time >= as.Date("2022-01-01"))

2. 消除月份间的空隙

空隙是因为日期的时间间隔与geom_tile的默认宽度不匹配,两种解决方法:

方法一:将日期转为离散因子(适合月度数据)

把日期格式化为"月份 年份"的字符串并转成因子,让X轴按离散月份排列,彻底消除空隙:

# 处理日期为离散因子
filtered_data$time_fct <- factor(format(filtered_data$time, "%b %y"),
                                 levels = unique(format(filtered_data$time, "%b %y")))

# 绘图时替换X轴字段
ggplot(filtered_data, aes(x = time_fct, y = branques, fill=values)) +
  geom_tile(width = 1, height = 1) + # 让tile填满单元格
  scale_y_discrete(expand=c(0,0), limits = sort(unique(filtered_data$branques))) + # 按字母排序Y轴
  labs(x = "% de variació interanual", y = "") +
  # 实现负红正绿的颜色映射
  scale_fill_gradient2(low = "darkred", mid = "white", high = "darkgreen",
                       midpoint = 0, na.value = "#FBF2F0")

方法二:设置geom_tile的width参数

保留日期类型X轴,将width设为月度间隔的天数(约30天):

ggplot(filtered_data, aes(x = time, y = branques, fill=values)) +
  geom_tile(width = 30) + # 匹配月度时间间隔
  scale_x_date(expand=c(0,0), breaks = "2 months", labels=scales::date_format("%b %y"),
               limits = c(as.Date("2022/01/01"), NA)) +
  scale_y_discrete(expand=c(0,0), limits = sort(unique(filtered_data$branques))) + # 字母顺序排序Y轴
  labs(x = "% de variació interanual", y = "") +
  scale_fill_gradient2(low = "#c41e3a", mid = "white", high = "#2d6a4f",
                       midpoint = 0, na.value = "#FBF2F0")

补充:颜色映射优化

原代码用单色系的scale_fill_distiller,要实现负红正绿的效果,必须换成scale_fill_gradient2,通过midpoint=0区分正负值的颜色区间。


完整可运行代码示例

# 加载依赖包
library(ggplot2)
library(scales)

# 筛选数据
filtered_data <- varIPI_branques_CAT_long[varIPI_branques_CAT_long$time >= as.Date("2022-01-01"), ]

# 按字母顺序排序Y轴类别
sorted_branques <- sort(unique(filtered_data$branques))

# 绘制热力图
ggplot(filtered_data, aes(x = time, y = branques, fill=values)) +
  geom_tile(width = 30) +
  scale_x_date(expand=c(0,0), breaks = "2 months", labels=date_format("%b %y"),
               limits = c(as.Date("2022/01/01"), NA)) +
  scale_y_discrete(expand=c(0,0), limits = sorted_branques) +
  labs(x = "% de variació interanual", y = "") +
  scale_fill_gradient2(low = "#c41e3a", mid = "white", high = "#2d6a4f",
                       midpoint = 0, na.value = "#FBF2F0") +
  theme_minimal()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 23:52:46