2016-09-15 78 views
1

这可能是一个非常基本的绘图问题,但即使阅读了很多帖子后,我仍无法解决它。我为此数据创建了一个基本图 -根据列中的值添加颜色图例R

ID ID_chr IVC10_BB_0048 IVC10_BB_0049 ....... 
mrna5.cds1 mal_mito_2 295.53 362.80 
mrna4.cds1 mal_mito_3 297.33 359.69 
mrna3.cds1 mal_mito_3 292.88 361.13 
mrna2.cds1 mal_mito_4 298.19 360.76 
mrna1.cds1 mal_mito_4 295.43 359.47 
mrna5.cds1 mal_mito_5 429.18 520.89 
mrna4.cds1 mal_mito 419.21 518.53 
mrna3.cds1 mal_mito 431.56 527.69 
mrna2.cds1 mal_mito 429.69 521.14 
mrna1.cds1 mal_mito 423.87 509.44 
mrna5.cds1 mal_mito 231.26 246.93 
mrna4.cds1 mal_mito 206.76 231.48 
mrna3.cds1 mal_mito 234.60 260.17 
mrna2.cds1 mal_mito 230.75 254.36 
mrna1.cds1 mal_mito 233.56 254.04 
mrna5.cds8 PF3D7_01 5.745 8.022 
mrna5.cds7 PF3D7_01 3.821 4.744 
mrna5.cds6 PF3D7_01 3.847 4.794 
mrna5.cds5 PF3D7_01 3.821 4.645 
mrna5.cds4 PF3D7_02 5.542 7.004 
mrna5.cds3 PF3D7_03 4.479 5.663 
mrna5.cds2 PF3D7_04 4.252 5.266 
     . 
. 

。 。

该数据有大约100列和20000行。第二列有14个独特的类别,分别是mal_mito,PF3D7_01,PF3D7_02,PF3D7_03 ...等,并基于这些数据为图表着色。

IVC_all = read.table("input.txt") 
pdf(file="test.pdf") 
par(mfrow =c(3,1)) 
family <- as.factor(IVC_all[,1]) 
for (i in seq(2,length(IVC_all),1)) plot(IVC_all[,i],ylab=names(IVC_all[i]),col=family,pch=19) 
dev.off() 

我想在此图中添加一个颜色图例,显示哪个颜色对应于哪个第二列值。我最终得到一个pdf文件,其中包含每页3个图的所有列的线图。我尝试使用image.plot,但我无法做到正确。谢谢!

enter image description here

回答

0

更新你的代码在底部添加传说:

#update 
par(mfrow =c(4,1)) 
for (i in seq(2,length(IVC_all),1))  
    plot(IVC_all[,i],ylab=names(IVC_all[i]),col=family,pch=19) 
#add 
unique.family <- unique(family) 
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n") 
legend("bottom", as.character(unique.family), 
     lwd=rep(2,length(unique.family)), 
     col=unique.family, horiz=TRUE) 
+0

这工作完美!非常感谢。现在这个图例被添加到了图的末尾,因为我有大约100列,所以当我输出为pdf时,它出现在最后一页。无论如何要在每一页上绘制这个图表吗?我需要打破这个循环吗? – AnkP

1

使用legend();见例如

# Generate data 
x = rnorm(1:10000) 

# Default palette() only contains 8 colors. 
library(RColorBrewer) 

# Plot, change `Spectral` to whatever you palette you want in `?brewer.pal` 
plot(x, col = rep(brewer.pal(10, "Spectral"), each = 1000)) 

# Manually add legend, you need to set the x, y coordinates. `legend` args are the labels, so you need something like `unique(IVC_all[,1])` 
legend(x = 1, y = 2, legend = c("hoho", "haha", paste(8:10)), col = brewer.pal(10, "Spectral"), lty = 1, cex = 1) 

enter image description here

+0

由于VLO,我能够得到的情节,但颜色类别中的情节现在超过14个,我需要一个仅在第二列中具有唯一类别的颜色。无论如何,还有将它转换回线条/直方图风格的情节。类型=“h”现在似乎不起作用。 – AnkP