绘图由R中的因子着色 - 循环未特别指定

时间:2017-07-10 16:08:16

标签: r plot

如果之前有人问过这个道歉,我试过看,但是在R中搜索颜色分配是一个非常广泛的主题。此外,我没有为您添加数据,因为它有相当多的数据(我不认为它将是必需的,因为它不是错误消息问题)。编辑数据样本:

      PC1                 PC2                colour
[1,] "-25.5987648792402" "3.41673760040586"  "blue" 
[2,] "-18.1520720669261" "2.09819717146663"  "green"
[3,] "-22.1308426042819" "3.46021550506459"  "green"
[4,] "-27.7950466967456" "-3.64374133124297" "gold" 
[5,] "-25.2414233662759" "-1.75802702655264" "green"
[6,] "-13.5641450885194" "2.70296369323088"  "green"
[7,] "-27.1954041315429" "6.44022518338822"  "blue" 
[8,] "-14.0474589982648" "3.00996494287365"  "red"  
[9,] "-40.1604532835109" "-27.6333759665025" "gold" 
[10,] "-26.6708536765817" "-5.0610016520552"  "red"  
[11,] "-21.4727890084845" "4.51825527258536"  "green"
[12,] "-21.2239456479504" "8.48862307072586"  "black"
[13,] "-25.0901196732896" "0.609916921307137" "blue" 
[14,] "-32.7132657746404" "-17.00161594225"   "gold" 
[15,] "-27.0815551977454" "10.5198515619958"  "black"
[16,] "-21.2961394099536" "9.53841507233795"  "black"
[17,] "-23.3675032692574" "3.36809622698634"  "black"
[18,] "-22.9904562811844" "11.9580087306051"  "black"
[19,] "-22.5073852494145" "1.30688136244357"  "gold" 
[20,] "-18.6259166632196" "5.51825251437074"  "gold" 
[21,] "-24.7138325079293" "-5.88117174408046" "red"  
[22,] "-19.2386902822007" "6.74347285527281"  "gold" 
[23,] "-29.3603972846236" "1.42048147132848"  "red"  
[24,] "-9.8526393614268"  "7.24233907420527"  "red"  
[25,] "-34.6289373905791" "3.655343260247"    "red"  

我一直在尝试使用分类变量($ BIO)绘制主要成分1和2。首先我选择了颜色:

my.colours <- c("blue", "red", "green", "gold", "black")
names(my.colours) = c("WNT", "SHH", "G3", "G4", "unknown")

我为每个$ BIO因子分配了一种颜色,并在csv(我的数据列表)上为每个样本的这些$颜色创建了一列。

for (csv in 1:length(csvs)){
for (r in 1:nrow(csvs[[csv]])){
csvs[[csv]]$colour <- my.colours[which(csvs[[csv]][r ,]$BIO == names(my.colours))]}}
for (csv in 1:length(csvs)){
for (r in 1:nrow(csvs[[csv]])){
csvs[[csv]][r ,]$colour <- my.colours[which(csvs[[csv]][r ,]$BIO == names(my.colours))] }}

输出正确:

csvs[[csv]]$colour [1] "blue"  "green" "green" "gold"  "green" "green" "blue"  "red"   "gold"  "red"   "green" "black" "blue"  "gold"  "black" "black" "black" "black" "gold" [20] "gold"  "red"   "gold"  "red"   "red"   "red"  

然后我尝试使用此列绘制颜色图。

for (csv in 1:length(csvs)){
for (pc in 1:length(pcas)){
pdf(paste("coloured 2D PCA plot of bio",types[pc],".pdf"))
plot(pcas[[pc]]$x[,c(1, 2)], col=csvs[[csv]]$colour, main =paste("2D PCA plot of ",types[pc]))
 dev.off()}}

图表的颜色正确,但它们不是由哪个样本分配的(例如,有5个蓝点 - 样本中只有3个蓝色)。似乎通过循环my.colours而不是$ color列为样本指定了颜色?我该如何解决这个烂摊子?

enter image description here

感谢您的帮助,

0 个答案:

没有答案