Hello,
i played around with regular expressions and found how i can exclude objects and used textGrob for the title. So far, it does what i was looking for.
But if i just exclude the guide-box from gt1, the space is still occupied and blank. Therefore i positioned the legend into the plotting area of gt1, but then the position of the guide-box in the gtable is also adjusted.
So i still have two questions:
1. Is there a method to re-center the guide-box within the gtable space?
2. And is there a better way to preserve the legend space for plotting when creating the plot p1?
Thanks,
Herbert
-----here my new code ------------------
df.d <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100), col=factor(rep(1,100)))
# position the legend into the plotting area to preserve the legend space for the plot
p1 <- ggplot(df.d) + geom_point(aes(x=x, y=y, color=col)) +
scale_colour_manual(values=c('red'), guide='legend') + theme(legend.position=c(-0.2, 0.6))
p2 <- ggplot(df.d) + geom_point(aes(x=x, y=z, color=col)) +
scale_colour_manual(values=c('red'), guide='none')
gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- textGrob('Global title', gp=gpar(fontsize=20))
gt <- gtable(widths = unit(c(4, 4, 1), "null"), heights = unit(c(1, 4), "null"))
gt <- gtable_add_grob(gt, gtable_filter(gt1, "[^-guidebox]"), t = 2, b = 2, l = 1, r = 1, clip='off')
gt <- gtable_add_grob(gt, gt2, t = 2, b = 2, l = 2, r = 2, clip='off')
gt <- gtable_add_grob(gt, gtable_filter(gt1, "guide-box"), t = 2, b = 2, l = 3, r = 3, clip='off')
gt <- gtable_add_grob(gt, gt3, t = 1, b = 1, l = 1, r = 3, clip='off')
grid.newpage()
grid.draw(gt)