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

如何在ggplot分组堆叠条形图中调整条形轮廓宽度?

问题:ggplot分组堆叠条形图无法修改条形轮廓厚度

你在ggplot分组堆叠条形图中尝试用geom_bar的size参数调整条形轮廓宽度但未生效,核心原因是代码中同时使用了geom_bar和geom_col_pattern,后者会覆盖前者的绘制效果,导致geom_bar的参数无法生效。

解决方案

直接在geom_col_pattern中设置size参数来控制条形轮廓厚度,同时移除重复的geom_bar图层(因为geom_col_pattern已经包含了条形绘制的功能)。

修改后的完整代码如下:

#Order the way we want the plot top to bottom 
Zoop_df_Ab$Class <- factor(Zoop_df_Ab$Class, levels=c("Rotifera","Cladocera","Cyclopoida","Calanoida"))

#order the way we want legend top to bottom
classes <- tibble::tribble(
  ~Class      , ~fill      , ~pattern    , ~pattern_angle, ~order
  , "Calanoida", "#333D47"    , "none"      ,              0,      1
  , "Cyclopoida"  , "lightgrey", "crosshatch"      ,              30,      2
  , "Cladocera" , "lightgrey"    , "none",             0,      3
  , "Rotifera" , "white" , "stripe"    ,             45,      4
)

# 注意:需要提前定义dat_vline和dat_hline数据,否则代码会报错
# dat_vline <- ...
# dat_hline <- ...

Ab_plot_Zoop  =  ggplot(Zoop_df_Ab,                        
                        aes(x = factor(Lake),
                            y = Value,
                            fill = Class,
                            pattern = Class)) + 
  # 移除原geom_bar,直接用geom_col_pattern实现所有条形绘制需求
  geom_col_pattern(
    position = "stack", 
    width = 0.6,
    pattern_size = .05,
    pattern_density = .1,
    pattern_spacing = .02,
    pattern_fill = "black",
    color = "black",  # 轮廓颜色
    size = 0.5        # 轮廓厚度,可根据需求调整数值
  ) +
  geom_vline(data = dat_vline, aes(xintercept = xintercept), lineend = "square") +
  geom_hline(data = dat_hline, aes(yintercept = yintercept), lineend = "square") +
  theme_classic() +
  theme(panel.border = element_rect(colour = NA, fill=NA, size=0),
        text = element_text(size = 8, family = "sans")) +
  scale_y_continuous(expand = c(0,0)) + coord_cartesian(ylim=c(0, 100)) +
  labs(x = "", y = expression(paste("Relative Abundance (%)"))) + 
  facet_wrap(~factor(Season, levels=c('July','August','September'), ), nrow = 1, strip.position = "bottom", scales = "free_x") +
  theme(panel.background = element_blank(),
        panel.spacing = unit(0, "line"), 
        strip.background = element_blank(),
        strip.placement = "outside") +
  theme(plot.title.position = 'plot', 
        plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_x_discrete(NULL, expand = c(0.15, 0.15)) +
  scale_fill_manual(
    values = setNames(classes$fill, classes$Class),
    breaks = classes$Class
  ) +
  scale_pattern_manual(
    "Class",
    values = setNames(classes$pattern, classes$Class),
    breaks = classes$Class
  ) +
  scale_pattern_angle_manual(
    "Class",
    values = setNames(classes$pattern_angle, classes$Class),
    breaks = classes$Class
  ) +
  theme(legend.title=element_blank()) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), 
        axis.text = element_text(face="bold"), 
        text = element_text(size = 10))

Ab_plot_Zoop

关键说明

  • geom_col_pattern是geom_col的扩展,支持图案填充的同时,完全继承了geom_col的所有视觉参数,包括size(控制轮廓厚度)和color(控制轮廓颜色)。
  • 移除重复的geom_bar图层,避免了图层叠加覆盖的问题,确保参数设置直接作用于最终显示的条形。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 17:03:10