我无法对齐使用构面的图和没有构图的图。
问题在于我想要将患者的结果(带有刻面的情节)与基因模型(没有刻面的情节)对齐,但我需要为不同颜色的患者标签着色(患者变量为#34;刻面&# 34)。到目前为止,我可以使用tracks
包中的函数ggbio
对齐2个图,或者我可以使用不同颜色为标签着色。
这是我的代码:
# 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(p_pat@ggplot)
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(dumGM@ggplot)
# draw the two plots : they are not aligned...
grid.arrange(gtable_p_pat, gtable_dumGM, heights = unit(c(0.8, 0.2), "npc"))
我无法将tracks
函数与gtable
对象一起使用,我无法通过颜色修改重建初始对象p_pat
。
因此,我使用grid.arrange
但是尽管有很多尝试,例如,在第二个绘图中创建另一个列并调整宽度,我无法对齐绘图。
答案 0 :(得分: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(dumGM@ggplot)
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"))