2014-01-05 63 views
0

全部, 我有以下R腳本。爲了表示信任,它基於我從http://www.statmethods.net/graphs/line.html得到的腳本,並應用於我自己的數據。也許你可以知道我對R是新手。以下腳本生成自動圖例並將其標記爲1,2,3。相反,我希望腳本打印「樣本名稱爲文本」而不​​是數字。我想知道是否有人可以幫助我。謝謝。下面如何在R中自動生成線圖的圖例

# Create Line Chart 
fd<- read.csv ("indata", header=TRUE) 

## convert factor to numeric for convenience 
fd$sampleN <- as.numeric(fd$sampleN) 
nfd <- max(fd$sampleN) 

# get the range for the x and y axis 
xrange <- range(fd$gc) 

#yrange <- range(fd$coverage) 
yrange <- range(0,2) #you can customize this one. 

# set up the plot 
plot(xrange, yrange, type="n", xlab="gc", ylab="nc") 
colors <- rainbow(nfd) 
linetype <- c(1:nfd) 
plotchar <- seq(18,18+nfd,1) 

# add lines 
for (i in 1:nfd) { 
    tree <- subset(fd, sampleN==i) 
    lines(tree$gc, tree$coverage, type="b", lwd=1.5, 
    lty=linetype[i], col=colors[i], pch=plotchar[i]) 

} 

# add a title and subtitle 
title("metrics", "") 

# add a legend 
legend(xrange[1], yrange[2], 1:nfd, cex=0.8, col=colors, 
    pch=plotchar, lty=linetype, title="Samples") 

輸入的數據是:

sampleN,gc,coverage 
sample_metrics1,0,0.24558 
sample_metrics1,1,0.05663 
sample_metrics1,2,0.088432 
sample_metrics1,3,0.117296 
sample_metrics1,4,0.169752 
sample_metrics1,5,0.228159 
sample_metrics1,6,0.333096 
sample_metrics1,7,0.427725 
sample_metrics1,8,0.428772 
sample_metrics1,9,0.502811 
sample_metrics1,10,0.580475 
sample_metrics1,11,0.649858 
sample_metrics1,12,0.686928 
sample_metrics1,13,0.76773 
sample_metrics1,14,0.817875 
sample_metrics1,15,0.862198 
sample_metrics1,16,0.878292 
sample_metrics1,17,0.90871 
sample_metrics1,18,0.910914 
sample_metrics1,19,0.949483 
sample_metrics1,20,0.931209 
sample_metrics1,21,0.935704 
sample_metrics1,22,0.945239 
sample_metrics1,23,0.927157 
sample_metrics1,24,0.930656 
sample_metrics1,25,0.935901 
sample_metrics1,26,0.932245 
sample_metrics1,27,0.934365 
sample_metrics1,28,0.937328 
sample_metrics1,29,0.94063 
sample_metrics1,30,0.943312 
sample_metrics1,31,0.950184 
sample_metrics1,32,0.963963 
sample_metrics1,33,0.984982 
sample_metrics1,34,1.003258 
sample_metrics1,35,1.023331 
sample_metrics1,36,1.045045 
sample_metrics1,37,1.057649 
sample_metrics1,38,1.067116 
sample_metrics1,39,1.067653 
sample_metrics1,40,1.063026 
sample_metrics1,41,1.052287 
sample_metrics1,42,1.040282 
sample_metrics1,43,1.020074 
sample_metrics1,44,1.000212 
sample_metrics1,45,0.9896 
sample_metrics1,46,0.985244 
sample_metrics1,47,0.985526 
sample_metrics1,48,0.982893 
sample_metrics1,49,0.981362 
sample_metrics1,50,0.979265 
sample_metrics1,51,0.979916 
sample_metrics1,52,0.979846 
sample_metrics1,53,0.984885 
sample_metrics1,54,0.986803 
sample_metrics1,55,0.994042 
sample_metrics1,56,0.996345 
sample_metrics1,57,1.007551 
sample_metrics1,58,1.003795 
sample_metrics1,59,1.008586 
sample_metrics1,60,1.01787 
sample_metrics1,61,1.02482 
sample_metrics1,62,1.020601 
sample_metrics1,63,1.018051 
sample_metrics1,64,1.035102 
sample_metrics1,65,1.025948 
sample_metrics1,66,1.03562 
sample_metrics1,67,1.040274 
sample_metrics1,68,1.036387 
sample_metrics1,69,1.058105 
sample_metrics1,70,1.06795 
sample_metrics1,71,1.064344 
sample_metrics1,72,1.049132 
sample_metrics1,73,1.071662 
sample_metrics1,74,1.08023 
sample_metrics1,75,1.09954 
sample_metrics1,76,1.14631 
sample_metrics1,77,1.172438 
sample_metrics1,78,1.175826 
sample_metrics1,79,1.211766 
sample_metrics1,80,1.208507 
sample_metrics1,81,1.217303 
sample_metrics1,82,1.262392 
sample_metrics1,83,1.331984 
sample_metrics1,84,1.383047 
sample_metrics1,85,1.416259 
sample_metrics1,86,1.583698 
sample_metrics1,87,1.785849 
sample_metrics1,88,1.857123 
sample_metrics1,89,2.114804 
sample_metrics1,90,1.991428 
sample_metrics1,91,1.976769 
sample_metrics1,92,2.216659 
sample_metrics1,93,1.606086 
sample_metrics1,94,1.821604 
sample_metrics1,95,5.19378 
sample_metrics1,96,2.46372 
sample_metrics1,97,9.844896 
sample_metrics1,98,17.281094 
sample_metrics1,99,10.147151 
sample_metrics1,100,0 
sample_metrics2,0,0.27566 
sample_metrics2,1,0.052443 
sample_metrics2,2,0.060661 
sample_metrics2,3,0.142456 
sample_metrics2,4,0.110315 
sample_metrics2,5,0.195035 
sample_metrics2,6,0.266574 
sample_metrics2,7,0.256115 
sample_metrics2,8,0.332823 
sample_metrics2,9,0.341872 
sample_metrics2,10,0.335247 
sample_metrics2,11,0.480877 
sample_metrics2,12,0.40355 
sample_metrics2,13,0.452441 
sample_metrics2,14,0.522209 
sample_metrics2,15,0.489945 
sample_metrics2,16,0.51272 
sample_metrics2,17,0.538931 
sample_metrics2,18,0.58408 
sample_metrics2,19,0.600836 
sample_metrics2,20,0.615026 
sample_metrics2,21,0.631172 
sample_metrics2,22,0.651189 
sample_metrics2,23,0.653251 
sample_metrics2,24,0.679981 
sample_metrics2,25,0.69569 
sample_metrics2,26,0.71077 
sample_metrics2,27,0.735305 
sample_metrics2,28,0.754265 
sample_metrics2,29,0.778111 
sample_metrics2,30,0.804297 
sample_metrics2,31,0.831779 
sample_metrics2,32,0.863024 
sample_metrics2,33,0.894018 
sample_metrics2,34,0.937245 
sample_metrics2,35,0.985043 
sample_metrics2,36,1.029299 
sample_metrics2,37,1.069061 
sample_metrics2,38,1.094257 
sample_metrics2,39,1.102138 
sample_metrics2,40,1.114504 
sample_metrics2,41,1.11484 
sample_metrics2,42,1.111952 
sample_metrics2,43,1.108918 
sample_metrics2,44,1.090665 
sample_metrics2,45,1.082678 
sample_metrics2,46,1.076516 
sample_metrics2,47,1.08798 
sample_metrics2,48,1.093462 
sample_metrics2,49,1.08452 
sample_metrics2,50,1.090786 
sample_metrics2,51,1.093115 
sample_metrics2,52,1.101703 
sample_metrics2,53,1.107301 
sample_metrics2,54,1.11793 
sample_metrics2,55,1.130198 
sample_metrics2,56,1.141167 
sample_metrics2,57,1.155742 
sample_metrics2,58,1.165464 
sample_metrics2,59,1.157386 
sample_metrics2,60,1.167818 
sample_metrics2,61,1.166827 
sample_metrics2,62,1.135574 
sample_metrics2,63,1.128703 
sample_metrics2,64,1.130197 
sample_metrics2,65,1.089628 
sample_metrics2,66,1.048679 
sample_metrics2,67,1.024862 
sample_metrics2,68,0.951014 
sample_metrics2,69,0.863408 
sample_metrics2,70,0.741022 
sample_metrics2,71,0.661516 
sample_metrics2,72,0.545033 
sample_metrics2,73,0.433509 
sample_metrics2,74,0.367888 
sample_metrics2,75,0.275933 
sample_metrics2,76,0.148513 
sample_metrics2,77,0.104557 
sample_metrics2,78,0.09074 
sample_metrics2,79,0.035399 
sample_metrics2,80,0.038729 
sample_metrics2,81,0.013173 
sample_metrics2,82,0.011767 
sample_metrics2,83,0.019057 
sample_metrics2,84,0.012294 
sample_metrics2,85,0 
sample_metrics2,86,0 
sample_metrics2,87,0 
sample_metrics2,88,0 
sample_metrics2,89,0 
sample_metrics2,90,0 
sample_metrics2,91,0 
sample_metrics2,92,0 
sample_metrics2,93,0 
sample_metrics2,94,0 
sample_metrics2,95,6.012146 
sample_metrics2,96,0 
sample_metrics2,97,10.129887 
sample_metrics2,98,5.080385 
sample_metrics2,99,12.529071 
sample_metrics2,100,0 
genome_windows,0,0.000831175 
genome_windows,1,0.000594994 
genome_windows,2,0.0006862 
genome_windows,3,0.000876388 
genome_windows,4,0.000942013 
genome_windows,5,0.000958863 
genome_windows,6,0.001091706 
genome_windows,7,0.001176513 
genome_windows,8,0.001343131 
genome_windows,9,0.001520906 
genome_windows,10,0.001799756 
genome_windows,11,0.002206363 
genome_windows,12,0.002886519 
genome_windows,13,0.003815775 
genome_windows,14,0.005416344 
genome_windows,15,0.007978863 
genome_windows,16,0.011943919 
genome_windows,17,0.017942331 
genome_windows,18,0.026916138 
genome_windows,19,0.039731306 
genome_windows,20,0.057394938 
genome_windows,21,0.080734088 
genome_windows,22,0.110251175 
genome_windows,23,0.146365406 
genome_windows,24,0.188943563 
genome_windows,25,0.236974131 
genome_windows,26,0.288641038 
genome_windows,27,0.342599325 
genome_windows,28,0.397027756 
genome_windows,29,0.449998694 
genome_windows,30,0.500295781 
genome_windows,31,0.546689806 
genome_windows,32,0.587357375 
genome_windows,33,0.621426481 
genome_windows,34,0.647914206 
genome_windows,35,0.66795535 
genome_windows,36,0.680710806 
genome_windows,37,0.684866219 
genome_windows,38,0.678578188 
genome_windows,39,0.663729056 
genome_windows,40,0.642785275 
genome_windows,41,0.617580269 
genome_windows,42,0.5895275 
genome_windows,43,0.561500825 
genome_windows,44,0.536200638 
genome_windows,45,0.514647113 
genome_windows,46,0.496522031 
genome_windows,47,0.481198119 
genome_windows,48,0.465125163 
genome_windows,49,0.445097106 
genome_windows,50,0.419522256 
genome_windows,51,0.389562975 
genome_windows,52,0.358433044 
genome_windows,53,0.329269363 
genome_windows,54,0.30327115 
genome_windows,55,0.280368106 
genome_windows,56,0.258316269 
genome_windows,57,0.234869275 
genome_windows,58,0.209039444 
genome_windows,59,0.181009944 
genome_windows,60,0.152880644 
genome_windows,61,0.126921669 
genome_windows,62,0.104752206 
genome_windows,63,0.086465069 
genome_windows,64,0.071383019 
genome_windows,65,0.058826631 
genome_windows,66,0.048428619 
genome_windows,67,0.039328369 
genome_windows,68,0.031444125 
genome_windows,69,0.025158775 
genome_windows,70,0.020178806 
genome_windows,71,0.01616445 
genome_windows,72,0.013079681 
genome_windows,73,0.0106774 
genome_windows,74,0.008807444 
genome_windows,75,0.007423456 
genome_windows,76,0.006410344 
genome_windows,77,0.005637675 
genome_windows,78,0.005014725 
genome_windows,79,0.0043783 
genome_windows,80,0.003736631 
genome_windows,81,0.003142294 
genome_windows,82,0.002639056 
genome_windows,83,0.002170913 
genome_windows,84,0.001683113 
genome_windows,85,0.001218638 
genome_windows,86,0.000809938 
genome_windows,87,0.000524731 
genome_windows,88,0.000361769 
genome_windows,89,0.000234463 
genome_windows,90,0.000153681 
genome_windows,91,9.75E-05 
genome_windows,92,0.000057575 
genome_windows,93,3.61E-05 
genome_windows,94,0.00002095 
genome_windows,95,1.03E-05 
genome_windows,96,6.16E-06 
genome_windows,97,4.11E-06 
genome_windows,98,0.00000205 
genome_windows,99,8.31E-07 
genome_windows,100,2.12E-06 

回答

1

?legend,你可以看到第三個參數(目前1:nfd)是被顯示在圖例中的文本。如果您想改爲使用標籤A,B和C,則應將1:nfd替換爲c("A", "B", "C")

+0

嗨,我很欣賞你的建議。它按照你的建議工作,但我有很多數據集,我不想每次都手動輸入。當我處理不同的數據集時,我可以如何放置循環等,以便計算機自動打印它。謝謝。 – Supertech

+1

'legend(...,legend = unique(sampleN),...)' – Henrik

1

我會用ggplot2此:

library(ggplot2) 
ggplot(fd, aes(x = gc, y = coverage, color = sampleN)) + 
    geom_line() 

此運用因子的名字在傳說中,你的情況sample_metrics1

enter image description here

+0

謝謝。你真的很容易! – Supertech

+0

我真的會推薦使用'ggplot2'。一旦你掌握了它的語法,語法就非常直觀。它有一個非常廣泛的情節,你可以做。 –