2016-09-15 1 views
1

이것은 매우 기본적인 플로팅 질문 일 수 있지만 많은 게시물을 읽은 후에도 해결할 수 없습니다. 이 데이터에 대한 기본 플롯을 만들었습니다. -열의 값을 기반으로 색상 범례를 추가하십시오.

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 행이 있습니다. 제 2 열에는 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() 

어떤 색이 어떤 두 번째 열 값에 해당하는지 보여주는이 플롯의 색 범례를 추가하려고합니다. 페이지 당 3 개의 플롯으로 모든 열에 대한 선 그래프가있는 pdf 파일로 끝납니다. 나는 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

완벽하게 작동합니다! 고마워. 이제 전설은 PDF로 출력 할 때 마지막 페이지에서 오는 ~ 100 개의 열이 있기 때문에 줄거리 끝에 추가됩니다. 어쨌든 모든 페이지에 이것을 표시 할 수 있습니까? 루프를 깰 필요가 있을까요? – 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 세 이상이면 2 열의 고유 한 카테고리에만 해당하는 색상이 필요합니다. 또한 어쨌든 그것을 다시 라인/히스토그램 스타일 플롯으로 변환 할 수 있습니다. type = "h"는 현재 작동하지 않습니다. – AnkP

관련 문제