R:使用自动绘图时基于组的 PCA 中的颜色点

R: Color points in PCA based on groups when using autoplot

我制作了一个 PCA 图,其中我根据各种基因的表达绘制了许多细胞。在此图中,我想用单独的颜色为某些点着色。我试图通过创建 "groups" 来实现这一点,我在其中根据 "gene1" 的表达或缺乏表达对细胞进行排序。

这是我的数据框的样子(gene1、gene2 和 cell_1、cell_2 等是列名和行名):

          gene1      gene2      gene3      gene4      gene5
cell_1   0.0000   0.279204  25.995400  46.171700  94.234100
cell_2   0.0000  23.456000  77.339800 194.241000 301.234000
cell_3   2.0000  13.100000  45.309200   0.776565   0.000000
cell_4   0.0000  10.500000 107.508000   3.032500   0.000000
cell_5   3.0000   0.000000   0.266139   0.762981 123.371000

这是我用来尝试实现此目的的代码:

library(ggplot2)
library(ggfortify)

# Group cells based on expression of a certain gene (to use for color labels in the next step)
groups <- factor(ifelse(df$gene1 > 0, "Positive", "Others"))

#Calculate PCs and plot PCA
autoplot(prcomp(log(df[]+1)), colour="Positive")

当我 运行 这段代码时,出现以下错误:

Error in grDevices::col2rgb(colour, TRUE) : invalid color name 'Positive'

这个怎么样?

df$groups <- factor(ifelse(df$gene1 > 0, "Positive", "Others"))

head(df)
      gene1     gene2    gene3     gene4      gene5   groups
1 0.5638534  8.968558 94.40170  62.93106 290.442698 Positive
2 0.0000000 15.248374 45.87507 204.21703 291.501669   Others
3 1.9059518 19.488162 75.89302  97.69643 177.833347 Positive
4 1.9449987  6.358773 54.97159  41.54307 164.835188 Positive
5 0.0000000 16.568077 31.62370  23.72278  31.774541   Others
6 1.7199368  3.788276 80.51450 102.82221   6.259461 Positive

autoplot(prcomp(log(df[1:5]+1)), data=df, colour='groups')