2012-05-01 4 views
2

내 데이터 플롯여러 플롯 구성은 여기

nmar <- seq (1, 100, 5) 
position= rep(nmar, 5) 
n = length (nmar) 
chr = rep(1:5, each = n) 

mapdata <- data.frame (chr, position, 
snpname = paste("SNP-", 1:length (position), sep = "")) 
mapdata 


chr.lab = 1 ; mbar.col = "blue" 
layout(matrix(c(1,1,2),nc=1)) # works for two but I need to extend it to 
     n (which is level of chr = 5) 

# plot level 1 
mapdata1 <- mapdata[mapdata$chr == 1,] 
m <- par()$mar 
m[1] <- m[3] <- 0 
par(mar=m) 
# Set the limits of the plot 
plot(mapdata1$position,mapdata1$position-mapdata1$position, type="n", 
    axes=FALSE, 
xlab="", ylab="Chromsome", yaxt="n") 

polygon(
    c(0,max(mapdata1$position + 0.08 * max(mapdata1$position)),max(mapdata1$position)+ 
    0.08 * max(mapdata1$position),0), 
    .2*c(-0.3,-0.3,0.3,0.3), 
    col= mbar.col 
) 
segments(mapdata1$position, -.3, mapdata1$position, .3) 
text(mapdata1$position, -.7, mapdata1$snpname, srt = 90, cex.lab = chr.lab) 
text(mapdata1$position, .7, mapdata1$position,cex.lab = chr.lab) 
text(0, labels = c("Chr 2")) 

2 수준이다

# plot level 2 
mapdata2 <- mapdata[mapdata$chr == 2,] 
m <- par()$mar 
m[1] <- m[3] <- 0 
par(mar=m) 
# Set the limits of the plot 
plot(mapdata2$position,mapdata2$position-mapdata1$position, type="n", axes=FALSE, 
xlab="", ylab="Chromsome", yaxt="n") 

polygon(
    c(0,max(mapdata2$position + 0.08 * max(mapdata2$position)),max(mapdata2$position)+ 
0.08 * max(mapdata2$position),0), 
    .2*c(-0.3,-0.3,0.3,0.3), 
    col= mbar.col 
) 
segments(mapdata2$position, -.3, mapdata2$position, .3) 
text(mapdata2$position, -.7, mapdata2$snpname, srt = 90, cex.lab = chr.lab) 
text(mapdata2$position, .7, mapdata2$position,cex.lab = chr.lab) 
text(0, labels = c("Chr 2")) 

출력 enter image description here

(1) 어떻게 나는 n 레벨을위한 프로세스를 자동화한다. 비슷한 레벨을 ch 레벨 n 레벨로 확장한다. (2) 동일한 스펙을 가진 barsize를 보면 c 교수형, 다른 음모 지역으로 인해 수 있습니다. 모든 플롯을 동일하게 만드는 방법은 무엇입니까?

+0

어떤 특별한 이유 함수가 대신 CHR의 값을 가져 있도록 수정할 수 있을까? ggplot2와 격자는 이러한 종류의 복합 플롯을 쉽게 만들 수있는 솔루션을 제공합니다. 많은 링크를 참조하십시오 http://learnr.wordpress.com/2009/06/28/ggplot2-version-of-figures-in-lattice-multivariate -data-visualization-with-r-part-1/http://learnr.wordpress.com/2009/06/29/ggplot2-version-of-figures-in-lattice-multivariate-data-visualization-with-r -part-2/http://learnr.wordpress.com/2009/07/02/ggplot2-version-of-figures-in-lattice-multivariate-data-visualization-with-r-part-3/ –

+0

동의합니다. ggplot2와 격자가 더 좋지만 조작하기가 어렵다고 생각합니까? 아닙니다. – SHRram

+0

일단 ggplot2를 사용하면 매우 쉽게 사용할 수 있습니다. 비록 ggplot2를 선호하지만, ggplot2를 사용하기로 결정한 후에도 ggplot2 또는 lattice로 바꿀만한 가치가 있습니다. –

답변

2

ggplot은 확실히 여기로가는 길입니다. 당신이 정말로 기본 plot을 고수 할 경우에,이 함수는 작동합니다 : 당신이 볼 수 있듯이

plot.as.stack= function(mapdata1, mbar.col = "blue"){ 
    # mapdata1 <- mapdata[mapdata$chr == chr,] 
    m <- par()$mar 
    m[1] <- m[3] <- 0 
    par(mar=m) 
    # Set the limits of the plot 
    plot(mapdata1$position,mapdata1$position-mapdata1$position, type="n", 
     axes=FALSE, 
    xlab="", ylab="Chromsome", yaxt="n") 

    polygon(
     c(0,max(mapdata1$position + 0.08 * max(mapdata1$position)),max(mapdata1$position)+ 
     0.08 * max(mapdata1$position),0), 
     .2*c(-0.3,-0.3,0.3,0.3), 
     col= mbar.col 
    ) 
    segments(mapdata1$position, -.3, mapdata1$position, .3) 
    text(mapdata1$position, -.7, mapdata1$snpname, srt = 90, cex.lab = chr.lab) 
    text(mapdata1$position, .7, mapdata1$position,cex.lab = chr.lab) 
    text(0, labels = paste("Chr",unique(mapdata1$chr))) 
} 

# Example Run. 
par(mfrow=c(length(unique(mapdata$chr)),1)) 
x=by(mapdata,factor(mapdata$chr),plot.as.stack) # Assigned to x to prevent output 
par(mfrow=c(1,1)) 

, 난 그냥 코드를했다 함수에 넣어 한 다음에 by을 달렸다. 이것은 chr 레벨의 모두에있는 기능을 실행합니다.

plot.as.stack = function(chr){ 
    mapdata1 <- mapdata[mapdata$chr == chr,] 
    ... 
} 

을 그리고 CHR의 값으로 기능을 실행합니다 : 기본 플롯에 충실하고 싶었을위한

par(mfrow=c(5,1)) 
sapply(1:5,plot.as.stack) 
par(mfrow=c(1,1)) 
+0

답변을 수락하기를 기다릴 수 없습니다. – SHRram