2014-10-14 1 views
0

패싯을 사용하는 플롯과 그렇지 않은 플롯을 정렬 할 수 없습니다.grid.arrange를 사용하여 패싯이있는 플롯을 패싯없이 정렬 할 수 있습니다.

문제는 유전자 모델 (패싯없는 플롯)으로 환자 (패싯으로 플롯)의 결과를 정렬하려는 것이지만 다른 색상 (환자 변수가 하나의 "패싯"인)으로 환자 라벨을 색칠해야한다는 것입니다. 지금까지 두 개의 그림을 tracksggbio 패키지에서 정렬하거나 여러 색상으로 레이블을 지정할 수 있습니다. 나는 gtable 개체와 tracks 기능을 사용할 수 없습니다 나는 색상 수정과 초기 객체 p_pat를 재구성 관리 할 수 ​​없습니다

# load the libraries 
library(ggbio) 
library(Homo.sapiens) 
library(GenomicRanges) 
library(grid) 
library(gridExtra) 

# specify the chromosome area 
zone <- GRanges("chr1", IRanges(1000000, 1100000)) 

# create the data for the patients 
patcolors <- c("green", "red", "darkred") 
detseg <- GRanges(seqnames = "chr1", 
        IRanges(start = rep(1000000, 3), end = rep(1100000, 3)), 
        strand = rep("*",3), 
        patient = paste("pat", 1:3, sep = "_"), 
        CN = c(-1,0.5,1)) 
p_pat <- autoplot(detseg, aes(color = CN, fill = CN), 
        facets = patient ~ seqnames) + 
    xlim(zone) + 
    scale_fill_gradient2(low = "blue", mid = "white", high = "red") + 
    scale_color_gradient2(low = "blue", mid = "white", high = "red") + 
    theme(panel.background = element_rect(fill = "white"), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     legend.position = "none", 
     strip.background = element_rect(fill = "white"), 
     strip.text.x = element_blank(), 
     strip.text.y = element_text(angle = 0, hjust = 0, color = "darkgrey")) 

# create the gene model 
dumGM <- autoplot(Homo.sapiens, which = zone, stat = "reduce", 
        color = "darkblue", fill = "darkblue", label = FALSE) 

# align the 2 plots (but with darkgrey labels for each patient...) 
p_track <- tracks(p_pat, dumGM, xlim = zone, heights = c(0.8, 0.2), 
        scale.height = unit(1.3, "lines")) + 
    theme(panel.background = element_rect(fill = "white"), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank()) 
p_track 

# modify the "gtable" object to colour differently each patient 
gtable_p_pat <- ggplotGrob([email protected]) 
recup_child <- lapply(gtable_p_pat$grobs,function(x) names(x[]$children)) 
stripy <- which(sapply(lapply(recup_child,function(x) grepl("strip.text.y",x)), 
         any) == 1) 
for (i in 1:length(stripy)){ 
    colo <- patcolors[i] 
    j <- stripy[i] 
    gtable_p_pat$grobs[[j]]$children[[2]][]$gp$col <- colo 
} 
# draw the patients'plot with correctly colored labels 
grid.draw(gtable_p_pat) 

# get the gtable object from the gene model 
gtable_dumGM <- ggplotGrob([email protected]) 
# draw the two plots : they are not aligned... 
grid.arrange(gtable_p_pat, gtable_dumGM, heights = unit(c(0.8, 0.2), "npc")) 

:

여기 내 코드입니다.

따라서 grid.arrange을 사용합니다. 그러나 예를 들어 두 번째 플롯에서 다른 열을 작성하고 너비를 조정하려는 시도가 많았지 만 플롯을 정렬 할 수 없습니다. 사람이 대답 적 관심이 있다면

+1

을 (HTTP [아마이 도움]을 : // stackoverflow.com/questions/17736434/aligning-distinct-non-facet-plots-in-ggplot2-using-rpy2-in-python/17768224#17768224) – baptiste

+0

@baptiste, 링크 해 주셔서 감사 드리며 노력하겠습니다. 코드가 무엇을하는지, 내 문제를 해결하기 위해 영감을 얻길 바랍니다. – Cath

+1

'egg :: ggarrange'가 – baptiste

답변

3

, 나는 마침내 내 문제 해결 : 너무 많은 의존성을 가지고 있지만, 때문에 내가 코드를 실행하지 않은

# starting from dumGM, I modify the xlim to have the same xlim as p_pat 
dumGM <- dumGM + xlim(1000000,1100000) 

# then I "edit" the grob, add a column to have the same numbers of columns as p_pat, modify the layout and the widths, also according to p_pat. (I also write a blank in the new column so it's not empty) 

gtable_dumGM <- ggplotGrob([email protected]) 
gtable_dumGM_add <- gtable_add_cols(gtable_dumGM,unit(1, "lines"), 6) 
gtable_dumGM_add$layout[1, 4] <- 6 
gtable_dumGM_add <- gtable_add_grob(gtable_dumGM_add, textGrob(" ", gp=gpar(col="white")), 3, 5) 
gtable_dumGM_add$widths <- gtable_p_pat$widths 

# Finally, I draw the 2 plots and, yes, they are aligned ! 
grid.arrange(gtable_p_pat, gtable_dumGM_add, heights=unit(c(0.8, 0.2), "npc")) 
관련 문제