2

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

这是我的数据框的外观(gene1、gene2 和 cell_1、cell_2 等是 colnames 和 rownames):

          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'
4

1 回答 1

0

这个怎么样?

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')

在此处输入图像描述

于 2016-09-25T17:16:11.623 回答