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

使用ggplot2为含杀虫剂处理组添加交叉影线的问题排查

问题:给含杀虫剂的处理组柱状图添加交叉影线失败

我尝试制作一幅柱状图,希望为包含杀虫剂的处理组添加交叉影线,但当前代码无法正常运行,排查不出问题所在。

数据子集

dput(peak.hillmeans1[1:12,])
structure(list(Subplot_Descriptions = c("ambient", "ambient", 
"ambient", "ambient", "drought", "drought", "drought", "drought", 
"drought_insecticide", "drought_insecticide", "drought_insecticide", 
"drought_insecticide"), hill.number.type = c("H0", "H1", "H2", 
"Hinf", "H0", "H1", "H2", "Hinf", "H0", "H1", "H2", "Hinf"), 
    hill.number.mean = c(203.425, 41.2441606562857, 12.737168631955, 
    4.18742498977372, 243.933333333333, 47.6826726791051, 14.0547025275759, 
    4.50664860697016, 259.016666666667, 47.6003166273996, 13.7637529655749, 
    4.55027488751543), hill.number.sd = c(60.6538518974682, 6.51934912505994, 
    1.47899803604672, 0.379117399794485, 56.4631708874614, 9.62482790860152, 
    2.24482412930567, 0.537764198394058, 48.9080225184649, 8.0756230742672, 
    1.26719037679015, 0.478045892509355), insecticide = c("ambient_i", 
    "ambient_i", "ambient_i", "ambient_i", "ambient_i", "ambient_i", 
    "ambient_i", "ambient_i", "insecticide", "insecticide", "insecticide", 
    "insecticide")), row.names = c(NA, 12L), class = "data.frame")

当前使用的代码

library(ggplot2)
library(ggpattern)

ggplot(peak.hillmeans1, aes(x = hill.number.type, 
                            y = hill.number.mean, 
                            fill = Subplot_Descriptions)) + 
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_errorbar(aes(ymin = hill.number.mean - hill.number.sd, 
                    ymax = hill.number.mean + hill.number.sd), 
                width = 0.2, position = position_dodge(0.9)) + 
  geom_bar_pattern(aes(pattern = insecticide),
                   position = position_dodge(preserve = "single"),
                   pattern = "stripe",
                   pattern_density = 0.1,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.6,
                   color = "black") +
  scale_pattern_manual(values = c(insecticide = "stripe", ambient_i = "none")) +
  scale_fill_manual(values = c("ambient" = "darkblue",
                               "drought" = "gray58",
                               "warmed" = "red2",
                               "warmed_drought" = "darkred",
                               "drought_insecticide" = "dodgerblue4", 
                               "insecticide" = "sienna1", 
                               'warmed_drought_insecticide' = "orangered", 
                               'warmed_insecticide' = "salmon")) +
  labs(title = "Mean Hill Numbers for Peak Drought", 
       x = "Hill Number", y = "Mean # of Species", fill = "Treatment") +
  theme_minimal()

问题分析与修正代码

原代码核心问题是同时叠加geom_bar和geom_bar_pattern导致柱子重叠错位,且position_dodge参数设置不一致,另外geom_bar_pattern中硬编码的pattern = "stripe"覆盖了映射逻辑。

修正后的代码如下:

library(ggplot2)
library(ggpattern)

# 定义统一的dodge宽度
dodge_width <- 0.9

ggplot(peak.hillmeans1, aes(x = hill.number.type, 
                            y = hill.number.mean, 
                            fill = Subplot_Descriptions,
                            pattern = insecticide)) + 
  # 用geom_bar_pattern绘制所有柱子,替代原geom_bar
  geom_bar_pattern(stat = "identity",
                   position = position_dodge(width = dodge_width),
                   pattern_density = 0.1,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.6,
                   color = "black") +
  # 误差棒position与柱子保持一致
  geom_errorbar(aes(ymin = hill.number.mean - hill.number.sd, 
                    ymax = hill.number.mean + hill.number.sd), 
                width = 0.2, 
                position = position_dodge(width = dodge_width)) +
  # 手动指定图案:杀虫剂组用stripe,其他用none
  scale_pattern_manual(values = c(insecticide = "stripe", ambient_i = "none")) +
  scale_fill_manual(values = c("ambient" = "darkblue",
                               "drought" = "gray58",
                               "warmed" = "red2",
                               "warmed_drought" = "darkred",
                               "drought_insecticide" = "dodgerblue4", 
                               "insecticide" = "sienna1", 
                               'warmed_drought_insecticide' = "orangered", 
                               'warmed_insecticide' = "salmon")) +
  labs(title = "Mean Hill Numbers for Peak Drought", 
       x = "Hill Number", y = "Mean # of Species", 
       fill = "Treatment", pattern = "Insecticide") +
  theme_minimal()

关键修改点:

  • 移除单独的geom_bar,直接用geom_bar_pattern绘制所有柱子,避免图层重叠
  • 统一position_dodge的width参数,确保柱子和误差棒对齐
  • 删除geom_bar_pattern中硬编码的pattern = "stripe",让scale_pattern_manual根据insecticide变量控制图案
  • 给图例添加pattern标题,提升图表可读性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 18:45:10