adding title and excluding objects using gtable

674 views
Skip to first unread message

Herbert Jägle

unread,
Oct 5, 2012, 12:59:35 AM10/5/12
to ggp...@googlegroups.com
Following is a code example to create two plots with common legend. My questions are:
1. Is it possible to exclude the title & legend from gt1 using gtable_filter?
2. Is there a better or easy way to add just a global title without extracting the ttitle from a plot?

Thanks,
Herbert
---------------
df.d <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100), col=factor(rep(1,100)))

p1 <- ggplot(df.d) + geom_point(aes(x=x, y=y, color=col)) + scale_colour_manual(values=c('red'), guide='legend') +ggtitle('Global title')
p2 <- ggplot(df.d) + geom_point(aes(x=x, y=z, color=col)) + scale_colour_manual(values=c('red'), guide='legend')

gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))

gt <- gtable(widths = unit(c(4, 4, 1), "null"), heights = unit(c(1, 4), "null"))
gt <- gtable_add_grob(gt, gt1[, -5], t = 2, b = 2, l = 1, r = 1, clip='off')
gt <- gtable_add_grob(gt, gt2[, -5], 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, gtable_filter(gt1, "title"), t = 1, b = 1, l = 1, r = 3, clip='off')

grid.newpage()
grid.draw(gt)

Herbert Jägle

unread,
Oct 6, 2012, 2:06:22 AM10/6/12
to ggp...@googlegroups.com
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)

Winston Chang

unread,
Oct 19, 2012, 5:58:53 PM10/19/12
to Herbert Jägle, ggp...@googlegroups.com
I would suggest creating the p1 and p2 plots with the legend. Then extract:
- the plotting region (with ticks and tick labels) for p1
- the plotting region for p2
- the legend from one of the plots

and put them together using a new gtable. In your example, I think that the negative value for the legend.position might be throwing things off.

-Winston

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages