0

I can't manage to align a plot that uses facets and a plot that does not.

The problem is that I want to align results for patients (plot with facets) with a gene model (plot without facets) but I need to colour patients labels with different colours (patients variable being the one "facetted"). So far, I can align the 2 plots with the function tracks from ggbio package or I can colour the labels with different colours.

Here is my code :

# 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"))

I cannot use tracks function with the gtable object and I can't manage to reconstruct the initial object p_pat with the colours modification.

Hence, I use grid.arrange but despite a lot of attempts to, for example, create another column in the second plot and adjust the width, I can't align the plot.

Cath
  • 23,906
  • 5
  • 52
  • 86
  • 1
    i didn't run your code because it has so many dependencies, but [maybe this helps](http://stackoverflow.com/questions/17736434/aligning-distinct-non-facet-plots-in-ggplot2-using-rpy2-in-python/17768224#17768224) – baptiste Oct 14 '14 at 10:48
  • @baptiste, thank you for the link, I'll try and understand what the code does and hopefully take inspiration from it to solve my problem – Cath Oct 15 '14 at 07:14
  • 1
    for what it's worth `egg::ggarrange` could potentially help with this kind of alignment – baptiste Oct 21 '16 at 19:52

1 Answers1

3

If anyone is ever interested in the answer, I finally solved my problem :

# 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")) 
Cath
  • 23,906
  • 5
  • 52
  • 86