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

使用ggplot2绘制农田月度任务堆叠条形图:问题修正与优化建议

用ggplot2绘制农田月度任务安排可视化

需求

需要绘制可视化图表展示3块农田全年各月份的任务安排,目标效果类似分块堆叠的条形图:

  • X轴为月份(按全年顺序排列)
  • Y轴为农田ID
  • 不同任务用4种颜色区分

现有代码

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

# 数据
df34278 <- structure(list(fieldID = c("fieldA", "fieldB", "fieldC"), 
                          January = c("task2", "task3", "task2"), 
                          February = c("task2", "task3", "task2"), 
                          March = c("task2", "task3", "task2"), 
                          April = c("task2", "task3", "task1"), 
                          May = c("task2", "task3", "task4"), 
                          June = c("task1", "task3", "task4"), 
                          July = c("task2", "task3", "task4"), 
                          August1 = c("task2", "task2", "task4"), 
                          August15 = c("task2", "task2", "task2"), 
                          September = c("task1", "task2", "task2"), 
                          October = c("task3", "task2", "task2"), 
                          November = c("task3", "task2", "task2"), 
                          December = c("task3", "task2", "task2")), 
                     class = "data.frame", row.names = c(NA, -3L))

# 转换为长格式并将Task转为因子
df34278_longer <- pivot_longer(df34278,
                               cols = 2:14,
                               names_to = "Month",
                               values_to = "Task")

df34278_longer$Task <- as.factor(df34278_longer$Task)

# 创建绘图
plot34278 <- ggplot(df34278_longer, aes(x = fieldID, y = Month, fill = Task)) +
  geom_bar(stat = "identity", position = "stack") +
  coord_flip()
  
plot34278

遇到的问题

运行代码后结果不符合预期:

  • 月份未按全年顺序排列
  • 月份标签显示异常(如August1、August15格式不统一)
  • 常规堆叠条形图的用法无法匹配需求,需要调整绘图逻辑

运行环境

R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6.9

解决方案建议

你的需求更适合用**瓷砖图(tile plot)**而非堆叠条形图,瓷砖图能清晰展示每个农田在每个时间节点的任务类型,调整步骤如下:

1. 数据预处理:规范月份并设置有序因子

先处理August1和August15的格式,再将Month设置为有序因子,确保按时间顺序排列:

# 统一八月子节点的命名格式
df34278_longer$Month <- gsub("August(\\d+)", "August_\\1", df34278_longer$Month)

# 定义正确的月份顺序
month_order <- c("January", "February", "March", "April", "May", "June", 
                 "July", "August_1", "August_15", "September", "October", 
                 "November", "December")

# 将Month转为有序因子
df34278_longer$Month <- factor(df34278_longer$Month, levels = month_order, ordered = TRUE)

2. 使用geom_tile绘制瓷砖图

瓷砖图直接映射每个(农田, 月份)组合的任务颜色,完全匹配需求:

ggplot(df34278_longer, aes(x = Month, y = fieldID, fill = Task)) +
  geom_tile(color = "white", size = 1) + # 白色边框区分每个任务块
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) + # 避免月份标签重叠
  scale_fill_brewer(palette = "Set2") + # 用预设配色区分任务
  labs(x = "月份", y = "农田ID", fill = "任务类型") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) # 旋转X轴标签提升可读性

3. 若坚持堆叠条形图形式

如果需要类似堆叠条形的视觉效果,可以给每个月份分配连续X轴位置,结合分面实现:

# 给每个月份分配连续的X轴位置
df34278_longer$x_pos <- match(df34278_longer$Month, month_order)

ggplot(df34278_longer, aes(x = x_pos, y = 1, fill = Task)) +
  geom_col(position = "stack", width = 1) +
  facet_grid(fieldID ~ ., switch = "y") + # 按农田分面
  scale_x_continuous(breaks = seq_along(month_order), labels = month_order) +
  scale_fill_brewer(palette = "Set2") +
  labs(x = "月份", y = "", fill = "任务类型") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        strip.text.y.left = element_text(angle = 0)) # 调整分面标签方向

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 20:25:55