首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用grid.arrange将绘图与面对齐,并绘制没有面的绘图

使用grid.arrange将绘图与面对齐,并绘制没有面的绘图
EN

Stack Overflow用户
提问于 2014-10-14 08:13:41
回答 1查看 936关注 0票数 0

我无法将使用方面的绘图与不使用方面的绘图对齐。

问题是,我想使病人的结果(小面图)与基因模型(无面图)对齐,但我需要用不同颜色的病人标签(病人变量是“面”)。到目前为止,我可以用tracks函数对齐ggbio包中的两个情节,也可以用不同的颜色对标签。

这是我的代码:

代码语言:javascript
复制
# 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,但是尽管有很多尝试,例如,在第二个图中创建另一个列并调整宽度,但我无法对齐绘图。

EN

Stack Overflow用户

回答已采纳

发布于 2014-11-17 13:07:14

如果有人对答案感兴趣,我终于解决了我的问题:

代码语言:javascript
复制
# 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")) 
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26355806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档