【问题标题】:Adding multiple lines of means for two separate groups using ggplot / stat_summary使用 ggplot / stat_summary 为两个单独的组添加多行均值
【发布时间】:2021-06-29 03:11:54
【问题描述】:

这里是新手。

我正在尝试为两个不同的基因以及代表均值的两条单独的线创建基因表达 2^-dCt 值(数据框中的 p2ndCt)的抖动图。我的样本是一系列有序的细胞系。我在 data.frame 中有如下数据:

       Sample  gene   dCt       p2ndCt
1  K562 naive e14a2 -1.34 2.531513e+00
2   DMSO ctrl e14a2 -0.40 1.319508e+00
3      0.5 nM e14a2 -1.93 3.810552e+00
4        1 nM e14a2 -3.06 8.339726e+00
5        2 nM e14a2 -4.17 1.800094e+01
6      3.5 nM e14a2 -4.70 2.599208e+01
7        5 nM e14a2 -3.58 1.195879e+01

...

67       5 nM  e6a2 -2.06 4.169863e+00
68      10 nM  e6a2 -0.02 1.013959e+00
69      15 nM  e6a2  7.52 5.448217e-03
70      25 nM  e6a2  0.75 5.946036e-01

我想让 stat_summary() 将每个样本中的 e14a2 均值与 e6a2 均值分开,但是我不确定如何在现有的 data.frame 中执行此操作。我试过这段代码:

##BCR-ABL1 dCt qPCR graph
BCRABL1_dCt <- read.csv('~/Manjaro/workspace/BCR-ABL1_dCt.csv')
BCRABL1_dCt$Sample <- factor(BCRABL1_dCt$Sample, levels = c('K562 naive', 'DMSO ctrl', '0.5 nM', '1 nM', '2 nM', '3.5 nM', '5 nM', '10 nM', '15 nM', '25 nM', '50 nM', '200 nM'))

BCRABL1_dCt_plot <- ggplot(BCRABL1_dCt, aes(x = Sample, y = p2ndCt, color = gene)) + geom_jitter(width = 0.1, height = 0) + stat_summary(aes(y = p2ndCt, group = 1), fun = mean, geom = 'line', size = 1, color = 'red')
e14a2_dCt_plot

...这只是给了我一条线来平均两个基因。我已经读到我可以将基因放在不同的 data.frames 中,但最好将所有这些都放在一个框架中。提前感谢您的帮助!

【问题讨论】:

  • 也许是group=gene 而不是stat_summary 中的group=1
  • 我已经尝试过 group = 'gene' 和 group = gene,不幸的是它们都产生了与 group = 1 相同的图表
  • 在阅读了 group 参数之后,我明白了为什么 group = gene 应该在那里工作。我不确定 stat_summary 是直接从 data.frame 调用,还是通过 ggplot()
  • 嗯。诡异的。我刚刚添加了我的建议作为答案。也许这有助于找出或澄清问题所在。

标签: r ggplot2


【解决方案1】:

使用所提供的数据的 sn-p 运行代码会通过Sample 提供一种方法:


library(ggplot2)

ggplot(BCRABL1_dCt, aes(x = Sample, y = p2ndCt, color = gene)) + 
  geom_jitter(width = 0.1, height = 0) + 
  stat_summary(aes(y = p2ndCt, group = 1), fun = mean, geom = 'line', size = 1, color = 'red')

但是,使用 group=gene 而不是 group=1 为每个 gene 提供了一系列方法:

ggplot(BCRABL1_dCt, aes(x = Sample, y = p2ndCt, color = gene)) + 
  geom_jitter(width = 0.1, height = 0) + 
  stat_summary(aes(y = p2ndCt, group = gene), fun = mean, geom = 'line', size = 1, color = 'red')

数据

BCRABL1_dCt <- read.table(text = "       Sample  gene   dCt       p2ndCt
1  K562_naive e14a2 -1.34 2.531513e+00
2   DMSO_ctrl e14a2 -0.40 1.319508e+00
3      0.5_nM e14a2 -1.93 3.810552e+00
4        1_nM e14a2 -3.06 8.339726e+00
5        2_nM e14a2 -4.17 1.800094e+01
6      3.5_nM e14a2 -4.70 2.599208e+01
7        5_nM e14a2 -3.58 1.195879e+01
67       5_nM  e6a2 -2.06 4.169863e+00
68      10_nM  e6a2 -0.02 1.013959e+00
69      15_nM  e6a2  7.52 5.448217e-03
70      25_nM  e6a2  0.75 5.946036e-01", header = TRUE)

BCRABL1_dCt$Sample <- gsub("_", " ", BCRABL1_dCt$Sample)
BCRABL1_dCt$Sample <- factor(BCRABL1_dCt$Sample, levels = c('K562 naive', 'DMSO ctrl', '0.5 nM', '1 nM', '2 nM', '3.5 nM', '5 nM', '10 nM', '15 nM', '25 nM', '50 nM', '200 nM'))

【讨论】:

  • 嗨 Stefan,我又试了一次,效果很好,非常感谢!
猜你喜欢
  • 2019-05-11
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2013-02-18
  • 2020-03-09
  • 1970-01-01
相关资源
最近更新 更多