【问题标题】:Coloring subsets in PCA biplotPCA 双图中的着色子集
【发布时间】:2020-05-29 08:23:14
【问题描述】:

我正在研究一个名为expression 的基因表达数据框。我的样本属于不同的子组,在 colname 中表示(即所有在 colname 中包含“adk”的样本都属于同一个子组)

       adk1  adk2  bas1  bas2  bas3  non1  ...
gene1   1.1   1.3   2.2   2.3   2.8   1.6
gene2   2.5   2.3   4.1   4.6   4.2   1.9
gene3   1.6   1.8   0.5   0.4   0.9   2.2
...

我已经定义了子集

adk <- expression[grepl('adk', names(expression))]

然后我使用

对这个数据集进行了 PCA
pca = prcomp (t(expression), center = F, scale= F)

我现在想在 PCA 双图中绘制我从 PCA 获得的 PC。为此,我希望属于同一子组的所有样本都具有相同的颜色(例如,所有“adk”样本应该是绿色,所有“bas”样本应该是红色,所有“非”样本应该是蓝色)。我尝试使用 ggfortify 中 autoplot 函数的 color 参数,但我无法让它使用我定义的子集。

如果有人可以帮助我,我会很高兴!谢谢:)

编辑:我想给你一个我想做的例子,使用 USArrests 数据集:

head(USArrests)
           Murder Assault UrbanPop Rape
Alabama      13.2     236       58 21.2
Alaska       10.0     263       48 44.5
Arizona       8.1     294       80 31.0
Arkansas      8.8     190       50 19.5
California    9.0     276       91 40.6
Colorado      7.9     204       78 38.7

## Doing a PCA on the USArrests dataset

US.pca = prcomp(t(USArrests), center = F, scale = F)

## Now I can create a PCA biplot of PC1 and PC2 using the autoplot function (since I have ggfortify installed)

biplot1 = autoplot(US.pca,data=t(USArrests), x=1, y=2)

我希望所有列名中包含“e”的样本(在本例中为“Murder”和“Rape”)都具有相同的颜色。 “UrbanPop”和“Assault”样本也应该是单独的颜色。我希望这能让事情变得更清楚:)

附:我在 Windows 10 上的最新版本 RStudio 中运行 R

【问题讨论】:

  • 请提供一些数据给我们。请参阅this post 获取有关如何提供简单的自包含示例的建议。

标签: r subset pca


【解决方案1】:

欢迎来到 SO! 像这样的东西怎么样,使用ggbiplot包:

# PCA
pca <- prcomp (t(expressions), center = F, scale= F)
# first you get the vector of the names
# gr <- substr(rownames(t(expressions)),1,3)
# EDIT
gr <-gsub(".*(adk|bas|non).*$", "\\1",rownames(t(expressions)), ignore.case = TRUE)

library(ggbiplot)
# plot it
ggbiplot(pca, groups = gr)+ 
  scale_color_manual(values=c("green", "red"," blue")) + 
  theme_light()

编辑
如果您使用的是 R 4.0.0,您将按照以下两行安装软件包:

library(devtools)
install_github("vqv/ggbiplot", force = TRUE)

有数据:

expressions <- read.table(    text = "adk1  adk2  bas1  bas2  bas3  non1 
                               gene1   1.1   1.3   2.2   2.3   2.8   1.6
                               gene2   2.5   2.3   4.1   4.6   4.2   1.9
                               gene3   1.6   1.8   0.5   0.4   0.9   2.2", header = T )

【讨论】:

  • 感谢您的快速回答!不幸的是,当我输入 library("ggbiplot") 时,它说 ggbiplot 没有安装,当我尝试使用 install.packages("ggbiplot") 安装 ggbiplot 时,我收到一个错误,说 ggbiplot 不适用于 R 版本 4.0.0。你知道如何在 R 版本 4.0.0 上安装它吗?
  • 你可以运行这个library(devtools); install_github("vqv/ggbiplot", force = TRUE)从作者的repo安装它。您无法更新其他软件包。
  • 谢谢!这对我有用。不幸的是,我简化了问题中的列名。这些名称实际上更像“Lung.cancer.adk1”、“Lung.cancer.bas1”和“Lung.non.tum1”。所以我不能使用前三个字母或任何其他字母位置来清楚地创建组,因为一些 colnames 仅在第 24 到第 27 个字母中彼此不同,而其他 colnames 比这更短。那么有没有办法通过向量名称中出现的某些单词来定义组,而无需在 colname 中的特定位置搜索该单词?抱歉有点慢
  • @Marius,没问题,请参阅编辑:您可以使用正则表达式获取所需的部分。
  • 太棒了,谢谢!非常适合我
【解决方案2】:

您可以尝试使用库factoextra

下面是一个例子。

      library("factoextra")
      library("FactoMineR")
      data("decathlon2")
      df <- decathlon2[1:23, 1:10]
      res.pca <- PCA(df,  graph = FALSE)
      fviz_pca_biplot(res.pca, repel = TRUE)

【讨论】:

  • 不错的答案,也许有了这个fviz_pca_biplot(res.pca, repel = TRUE, habillage=decathlon2[1:23,]$Competition),您可以按照OP的要求按组着色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 2018-12-24
  • 1970-01-01
相关资源
最近更新 更多