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

时间:2016-09-25 14:55:01

标签: r ggfortify

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

这是我的数据框看起来的内容(gene1,gene2和cell_1,cell_2等是colnames和rownames):

re1.startAnimation(blinkanimation)

以下是我尝试实现此目的的代码:

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

1 个答案:

答案 0 :(得分: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')

enter image description here