'keep' parameter not working in version 0.9.0

24 views
Skip to first unread message

jdegen

unread,
May 8, 2012, 3:48:32 PM5/8/12
to ggplot2
I am trying to have multiple plots share one legend. In the past I've
used opts(keep = "legend_box") to achieve this. I recently ran my old
code again and it no longer works. In the meantime I have updated
ggplot2 to version 0.9.0 and I'm using R 2.15 on Mac OS 10.6. I saw
this post that suggests the 'keep' parameter does not work in version
0.9.0:
http://groups.google.com/group/ggplot2/browse_thread/thread/9ecd549cc6ee6aa7/5644583fa3f17648?lnk=gst&q=keep#5644583fa3f17648

Is this generally the case, and if so, does anybody know of another
way to create a plot of only the legend, suppressing the panel, in
0.9.0? I'm reluctant to move back to 0.8.9...

Thanks,
judith

Etienne B. Racine

unread,
May 9, 2012, 12:56:20 PM5/9/12
to ggp...@googlegroups.com
I don't have an answer, but here's a reproducible example that might illustrate the problem (from http://learnr.wordpress.com/2009/05/26/ggplot2-two-or-more-plots-sharing-the-same-legend/):
library("ggplot2")
library("grid")
p <- qplot(data = mtcars, mpg, hp, geom = "point", colour = cyl)
p1 <- p + opts(legend.position = "none")
p2 <- qplot(data = mtcars, wt, disp, geom = "point", colour = cyl) + opts(legend.position = "none")
legend <- p + opts(keep = "legend_box")
Layout <- grid.layout(nrow = 2, ncol = 2, widths = unit(c(2, 0.4), c("null", "null")), heights = unit(c(1, 1), c("null", "null")))
vplayout <- function(...) {
  grid.newpage()
  pushViewport(viewport(layout = Layout))
}
subplot <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
vplayout()
print(p1, vp = subplot(1, 1))
print(p2, vp = subplot(2, 1))
print(legend, vp = subplot(1:2, 2))

Etienne

baptiste auguie

unread,
May 9, 2012, 5:06:49 PM5/9/12
to jdegen, ggplot2
Have a look at the updated wiki page:
https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs

baptiste
> --
> 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

Etienne B. Racine

unread,
May 10, 2012, 6:13:37 PM5/10/12
to baptiste auguie, jdegen, ggplot2
Right. Thanks !

2012/5/9 baptiste auguie <baptist...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages