向 ggplot 条添加阴影线或图案

Adding hatches or patterns to ggplot bars

假设我想在条形图中显示基于 RNA-seq 和 q-PCR 分析的基因表达结果 (logFC)。我的数据集如下所示:

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)

我的 R 命令行


p=ggplot(f1, aes(x=geneID, y=logfc, fill= comp,color=exp))+
  geom_bar(stat="identity", position =position_dodge2(preserve="single"))+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))```

我有这个输出:

我想获得与其中一个变量(exp 或 comp)对应的条形图上的任何标记模式或影线,并添加上部误差线,如下图所示:

有什么帮助吗?

根据 linked 的回答,如何将其扩展到您的案例似乎很自然。在下面的示例中,我使用了一些虚拟数据,其结构类似于您提供的 head() 数据,因为 csv link 给了我 404.

library(ggplot2)
library(ggpattern)
#> 
#> Attaching package: 'ggpattern'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     flip_data, flipped_names, gg_dep, has_flipped_aes, remove_missing,
#>     should_stop, waiver

# Setting up some dummy data
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))

ggplot(f1, aes(x = geneID, y = logfc, fill = comp)) +
  geom_col_pattern(
    aes(pattern = exp),
    colour = "black",
    pattern_fill = "black",
    pattern_angle = 45,
    pattern_density = 0.1,
    pattern_spacing = 0.01,
    position = position_dodge2(preserve = 'single'),
  ) +
  scale_pattern_manual(
    values = c("none", "stripe"),
    guide = guide_legend(override.aes = list(fill = "grey70")) # <- make lighter
  ) +
  scale_fill_discrete(
    guide = guide_legend(override.aes = list(pattern = "none")) # <- hide pattern
  )

reprex package (v1.0.0)

于 2021-04-19 创建

编辑:如果你想在填充图例中重复阴影线,你可以制作一个interaction()然后自定义一个手动填充比例。

ggplot(f1, aes(x = geneID, y = logfc)) +
  geom_col_pattern(
    aes(pattern = exp,
        fill = interaction(exp, comp)), # <- make this an interaction
    colour = "black",
    pattern_fill = "black",
    pattern_angle = 45,
    pattern_density = 0.1,
    pattern_spacing = 0.01,
    position = position_dodge2(preserve = 'single'),
  ) +
  scale_pattern_manual(
    values = c("none", "stripe"),
    guide = guide_legend(override.aes = list(fill = "grey70")) # <- make lighter
  ) +
  scale_fill_manual(
    # Have 3 colours and repeat each twice
    values = rep(scales::hue_pal()(3), each = 2),
    # Extract the second name after the '.' from the `interaction()` call
    labels = function(x) {
      vapply(strsplit(x, "\."), `[`, character(1), 2)
    },
    # Repeat the pattern over the guide
    guide = guide_legend(
      override.aes = list(pattern = rep(c("none", "stripe"), 3))
    )
  )

reprex package (v1.0.0)

于 2021-04-19 创建

EDIT2:现在有错误栏:

library(ggplot2)
library(ggpattern)
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)

ggplot(f1, aes(x = geneID, y = logfc)) +
  geom_col_pattern(
    aes(pattern = exp,
        fill = interaction(exp, comp)), # <- make this an interaction
    colour = "black",
    pattern_fill = "black",
    pattern_angle = 45,
    pattern_density = 0.1,
    pattern_spacing = 0.01,
    position = position_dodge2(preserve = 'single'),
  ) +
  geom_errorbar(
    aes(
      ymin = logfc,
      ymax = logfc + sign(logfc) * SE,
      group = interaction(geneID, comp, exp)
    ),
    position = "dodge"
  ) +
  scale_pattern_manual(
    values = c("none", "stripe"),
    guide = guide_legend(override.aes = list(fill = "grey70")) # <- make lighter
  ) +
  scale_fill_manual(
    # Have 3 colours and repeat each twice
    values = rep(scales::hue_pal()(3), each = 2),
    # Extract the second name after the '.' from the `interaction()` call
    labels = function(x) {
      vapply(strsplit(x, "\."), `[`, character(1), 2)
    },
    # Repeat the pattern over the guide
    guide = guide_legend(
      override.aes = list(pattern = rep(c("none", "stripe"), 3))
    )
  )

reprex package (v1.0.0)

于 2021-04-22 创建