2016-09-25 41 views
1

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

这里就是我的数据帧的外表(基因1,基因2和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' 
+0

您应该使用组变量,而不是一个特定的标签,从组变量。 – Pieter

+0

@Pieter谢谢。我应该提到 - 我尝试过与“团体”和“其他人”也有同样的错误。 (错误grDevices :: col2rgb(color,TRUE):无效的颜色名称'groups') – Galaffer

回答

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

+0

谢谢!它为我返回错误: 错误在$ < - 。data.frame('* tmp *',“groups”,value = integer(0)): 替换有0行,数据有253 > autoplot (df [1:5] + 1)),data = df,color ='groups') df [1:5]中的错误:'closure'类型的对象不是子集合 但是,你的解决方案,我想出了这产生一个准确的情节,但也有一个令人困惑的警告: autoplot(prcomp(log(df [] + 1)),color = ifelse(df $ gene1> 0,“red”,“black” )) 警告消息: In if(%%列中的值%){: 该条件的长度> 1并且只有第一个元素将被使用 – Galaffer

+0

str(df)给你什么?用随机生成的df更新了帖子,如果你的df具有相同的结构,它应该没有错误地工作。对我来说,df是一个data.frame,所有基因列的类都是数字,组列是类因子。 –