首页 > 解决方案 > 向 ggplot 条添加影线或图案

问题描述

假设我想在条形图中显示基于 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)相对应的条上获取任何标记模式或阴影,并添加上部误差条,如下图所示:

在此处输入图像描述

请问有什么帮助吗?

标签: rggplot2bar-chartlegend

解决方案


按照链接的答案,如何将其扩展到您的案例似乎很自然。在下面的示例中,我使用了一些结构类似于head()您提供的数据的虚拟数据,因为 csv 链接给了我一个 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 包创建于 2021-04-19 (v1.0.0)

编辑:如果您想在填充图例中重复阴影,您可以制作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 包创建于 2021-04-19 (v1.0.0)

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 包(v1.0.0)于 2021-04-22 创建


推荐阅读