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

如何为ggplot基因表达条形图添加填充图案/阴影并绘制上方误差线

Solution for Adding Error Bars and Fill Patterns to Your Bar Plot

Got it, let's fix up your bar plot to meet your requirements—adding top-aligned error bars and fill patterns/shadows to distinguish your groups. Here's a complete, reproducible solution:

Step 1: Install & Load Required Packages

First, we'll use ggpattern for fill patterns (since ggplot2 doesn't support this natively) and stick with ggplot2 for the core plot:

install.packages(c("ggplot2", "ggpattern"))
library(ggplot2)
library(ggpattern)

Step 2: Full Plot Code with Error Bars & Patterns

We’ll adjust your original code to add both features. Below, I map patterns to the exp variable (qPCR vs RNA-seq) and keep color/fill for comp—you can swap these mappings if you want patterns for comp instead.

set.seed(42)
f1 <- expand.grid(
  comp = LETTERS[1:3],
  exp = c("qPCR", "RNA-seq"),
  geneID = paste("Gene", 1:4)
)
f1$logfc <- rnorm(nrow(f1))
f1$SE <- runif(nrow(f1), min=0, max=1.5)

# Create the plot
p <- ggplot(f1, aes(x=geneID, y=logfc, fill=comp, pattern=exp)) +
  # Bar plot with patterns, matched dodge position
  geom_bar_pattern(
    stat="identity",
    position = position_dodge2(preserve="single", padding=0.1),
    pattern_fill = "black",
    pattern_angle = 45,
    pattern_density = 0.1,
    pattern_spacing = 0.02,
    color = "black" # Bar outline for better clarity
  ) +
  # Top-aligned error bars (matches bar dodge position)
  geom_errorbar(
    aes(ymax = logfc + SE, ymin = logfc), # Only upper error bars as requested
    position = position_dodge2(preserve="single", padding=0.1),
    width = 0.2,
    color = "black"
  ) +
  # Clean up theme
  theme_bw() +
  theme(
    axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1),
    legend.position = "bottom" # Move legend to bottom for readability
  ) +
  # Customize pattern styles (swap types as needed)
  scale_pattern_manual(values = c(qPCR = "stripe", "RNA-seq" = "crosshatch"))

print(p)

Key Details Explained:

  • Error Bars: We use geom_errorbar with ymin=logfc and ymax=logfc+SE to only show upper error bars (matching your reference plot request). The position_dodge2 parameters mirror the bar plot to keep everything perfectly aligned.
  • Fill Patterns: geom_bar_pattern replaces the default geom_bar, and we map pattern to exp (swap to comp if you want patterns for comparison groups instead). Tweak pattern_density, pattern_angle, or pattern_type (try "dot", "wave", or "grid") to adjust the shadow/pattern look.
  • Alignment: position_dodge2(preserve="single") ensures all bars stay the same width even if some groups have missing data, and matching the padding parameter between bars and error bars keeps them aligned.

Quick Customization Tips:

  • To use patterns for comp instead of exp, swap pattern=comp in the aes() and adjust scale_pattern_manual accordingly.
  • For softer shadows, change pattern_fill to "gray" instead of "black".
  • If you need both upper and lower error bars, set ymin=logfc-SE instead of logfc.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 13:52:43