【问题标题】:Ellipses for groups on PCA from DESeq2来自 DESeq2 的 PCA 组的省略号
【发布时间】:2017-11-23 19:38:43
【问题描述】:

我想在下图中添加围绕我的三个组的省略号(基于变量“结果”)。请注意,vsd 是一个 DESeq2 对象,其中包含因子结果和批次:

pcaData <- plotPCA(vsd, intgroup=c("outcome", "batch"), returnData=TRUE)
percentVar <- round(100 * attr(pcaData, "percentVar"))
ggplot(pcaData, aes(PC1, PC2, color=outcome, shape=batch)) +
  geom_point(size=3) +
  xlab(paste0("PC1: ",percentVar[1],"% variance")) +
  ylab(paste0("PC2: ",percentVar[2],"% variance")) + 
  geom_text(aes(label=rownames(coldata_WM_D56C)),hjust=.5, vjust=-.8, size=3) +
  geom_density2d(alpha=.5) +
  coord_fixed()

我尝试添加一个椭圆,认为它会从顶部继承美学,但它尝试为每个点制作一个椭圆。

stat_ellipse() +

计算椭圆的点太少

geom_path:每个组仅包含一个观察值。需要调整群体审美吗?

stat_density2d() 中的计算失败:缺少需要 TRUE/FALSE 的值

建议?提前致谢。

> dput(pcaData)
structure(list(PC1 = c(-15.646673151638, -4.21111051849254, 13.1215703467274, 
-6.5477433859415, -3.22129766721873, 4.59321517871152, 1.84089686598042, 
37.8415172383233, 40.9996810499267, 37.6089348653721, -24.5520575763498, 
-46.5840253031228, -4.01498554781508, -31.227922394463), PC2 = c(31.2712754127142, 
5.89621557021357, -10.2425538634254, -3.44497747426626, 2.21504480008043, 
0.315695833259479, -4.66467589267529, -4.27504355920903, -1.08666029542243, 
-2.69753368235982, 5.89767436709778, -24.2836532766506, 4.43980653642228, 
0.659385524221137), group = structure(c(4L, 5L, 6L, 7L, 8L, 5L, 
8L, 1L, 2L, 3L, 6L, 9L, 9L, 9L), .Label = c("ctrl : 1", "ctrl : 2", 
"ctrl : 3", "non : 1", "non : 2", "non : 3", "preg : 1", "preg : 2", 
"preg : 3"), class = "factor"), outcome = structure(c(2L, 2L, 
2L, 1L, 1L, 2L, 1L, 3L, 3L, 3L, 2L, 1L, 1L, 1L), .Label = c("preg", 
"non", "ctrl"), class = "factor"), batch = structure(c(1L, 2L, 
3L, 1L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("1", 
"2", "3"), class = "factor"), name = structure(1:14, .Label = c("D5-R-N-1", 
"D5-R-N-2", "D5-R-N-3", "D5-R-P-1", "D5-R-P-2", "D5-Z-N-1", "D5-Z-P-1", 
"D6-C-T-1", "D6-C-T-2", "D6-C-T-3", "D6-Z-N-1", "D6-Z-P-1", "D6-Z-P-2", 
"D6-Z-P-3"), class = "factor")), .Names = c("PC1", "PC2", "group", 
"outcome", "batch", "name"), row.names = c("D5-R-N-1", "D5-R-N-2", 
"D5-R-N-3", "D5-R-P-1", "D5-R-P-2", "D5-Z-N-1", "D5-Z-P-1", "D6-C-T-1", 
"D6-C-T-2", "D6-C-T-3", "D6-Z-N-1", "D6-Z-P-1", "D6-Z-P-2", "D6-Z-P-3"
), class = "data.frame", percentVar = c(0.47709343625754, 0.0990361123451665
))

正如 Maurits Evers 建议的那样,我添加了一个组 aes,它只为 3 种结果类型中的 2 种绘制椭圆。

【问题讨论】:

  • dput(pcaData)
  • 您的示例仍然不可重现且自洽:coldata_WM_D56C 未在任何地方定义。无论哪种方式,基于我的解决方案的情节都符合预期。 您无法计算/绘制只有 3 个点的置信椭圆。 不确定您的预期。您可以在 ?stat_ellipse 中找到详细信息,它链接到 car::ellipse 以及 Fox 和 Weisberg 的“An R Companion to Applied Regression”。
  • 我没有意识到你需要 >3 个点来计算一个椭圆,所以感谢你提供的信息和帮助。

标签: r ggplot2 pca ellipse


【解决方案1】:

由于您没有提供任何示例数据,因此这里是一个使用 faithful 数据的示例。

关键是要添加group 审美。

require(ggplot2);

# Generate sample data
df <- faithful[1:10, ];
df$batch <- as.factor(rep(1:5, each = 2));

# This will throw a similar error/warning to yours
#ggplot(df, aes(waiting, eruptions, color = eruptions > 3, shape = batch)) + geom_point() + stat_ellipse();

# Add a group aesthetic and it works
ggplot(df, aes(waiting, eruptions, color = eruptions > 3, shape = batch, group = eruptions > 3)) + geom_point() + stat_ellipse();

因此,在您的情况下,请尝试添加 aes(..., group = outcome)

【讨论】:

  • 感谢您的帮助。这几乎奏效了,但只适用于三个组中的两个。我已经在上面添加了 dput(),并附上了带有您的建议的更新图表。
  • 是的,正如预期的那样。置信椭圆只能绘制 >3 个点。请参阅我对原始问题的评论。
猜你喜欢
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 2022-07-07
相关资源
最近更新 更多