【问题标题】:ggplot geom_line background colorggplot geom_line 背景颜色
【发布时间】:2021-11-09 23:53:42
【问题描述】:

我在long format (data_long) 中有 3 列基因表达数据。它看起来像这样,其中 column1 表示基因名称,column2:Sample,column 3 表示表达值:

Genes sample value
A44 A 7357.891
AAR A 6950.868
A44 B 6771.266
AAR B 4978.734
A44 C 9441.985
AAR C 6061.017

我想生成一个线图,根据样本对所有基因进行分组,每个样本中所有基因的 + 均值以不同颜色绘制。这是我能够使用的东西

ggplot(data_long, aes(x=sample, y=value)) +
  geom_line(aes(group=Genes), size=0.5, alpha=0.3, color="black") +
  stat_summary(aes(x=as.numeric(sample)), fun=mean, geom='line',size=1, color='orange') +
  theme_classic() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
  scale_y_continuous(limits = c(0, 40000))

现在,我想添加 Group 列并根据 Group 填充背景。

Genes Group sample value
A44  A1 A 7357.891
AAR  A1 A 6950.868
A44 A1 B 6771.266
AAR A1 B 4978.734
A44 A2 C 9441.985
AAR A2 C 6061.017

当我说 fill = "Group" 时出现错误。这就是我想要的。请帮我解决这个问题。

【问题讨论】:

  • 查看收到错误的代码以及错误消息会很有帮助

标签: r ggplot2


【解决方案1】:

我不太确定你的情况出了什么问题:当我试图用你的代码重现情节时,我遇到了错误。经过一些调整(下面的代码),我设法得到了一个情节,然后添加背景颜色看起来很简单。

library(ggplot2)

data_long <- "Genes Group sample value
A44  A1 A 7357.891
AAR  A1 A 6950.868
A44 A1 B 6771.266
AAR A1 B 4978.734
A44 A2 C 9441.985
AAR A2 C 6061.017"
data_long <- read.table(text = data_long, header = TRUE)

# example toy data
data_long <- data.frame(expand.grid(Genes = c("A1","A2","B1","B2"),
                                    sample = c("a1","a2","b1")),
                        value = rnorm(n = 12, mean = 15000, sd = 3000))
data_long$Group <- factor(ifelse(data_long$sample %in% c("a1","a2"), "A", "B"))


ggplot(data_long, aes(x=sample, y=value)) +
  geom_tile(aes(fill = Group, y = 7000), height = Inf, alpha = 0.3) +
  geom_line(aes(group=Genes), size=0.5, alpha=0.3, color="black") +
  stat_summary(aes(group = -1), fun=mean, geom='line',size=1, color='orange') +
  theme_classic()

[![enter image description here][1]][1]

reprex package (v2.0.1) 于 2021-09-14 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2016-04-07
    相关资源
    最近更新 更多