remove the legend but keeping the space

908 views
Skip to first unread message

Dani Linares

unread,
May 20, 2009, 11:43:14 PM5/20/09
to ggplot2
Hi all,

I think I read the answer in some place but I cant find it. Is there a
way to remove the legend but keeping the space associate to it?

I have two plots in the same window. The legend is the same for both,
so I only want to display it in the first plot. The problem is that if
I use opts(legend.position="none") for the second plot, then this
second plot is bigger.

thanks

dani

--
Daniel Linares
web: www.dlinares.org

Stavros Macrakis

unread,
May 21, 2009, 8:44:45 AM5/21/09
to Dani Linares, ggplot2
I wonder if you can set the transparency of all the legend items to alpha=0 (fully transparent)....

             -s

baptiste auguie

unread,
May 21, 2009, 9:19:24 AM5/21/09
to Dani Linares, ggplot2
Hi,

I can think of two situations but it's hard to tell without reproducible code,

1) the page layout is a result of facetting (possibly with a dummy
factor) or wrapping, in which case there should be one common legend
and its default position should be nicely aligned in the page

2) you don't want to use the first option, so you've placed two
unrelated ggplots on a page using grid.layout or other viewport
functions. Perhaps you can do something like this,

- create your two plots p1 and p2

- save the legend of p1 as a grob,

p3 <- p1 + opts(keep= "legend_box")

- strip the legend of p1 and p2,

p1 <- p1 + opts(legend.position = "none")
p2 <- p2 + opts(legend.position = "none")

- create three viewports on the page and print p1, p2, p3.


Example:

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
d <- qplot(carat, price, data=dsamp, colour=clarity)

p3 <- d + opts(keep= "legend_box")

p1 <- d + opts(legend.position = "none")
p2 <- d + opts(legend.position = "none")

p3

pushViewport(viewport(x=0.4,y=0.75, width = 0.8, height = 0.5, angle = 0,
name = "topleftvp"))
grid.rect()
print(p1, vp="topleftvp")
upViewport()
pushViewport(viewport(x=0.4,y=0.25, width = 0.8, height = 0.5, angle = 0,
name = "botleftvp"))
grid.rect()
print(p2, vp="botleftvp")
upViewport()



HTH,

baptiste
--

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________

Dani Linares

unread,
May 21, 2009, 11:14:26 AM5/21/09
to baptiste auguie, ggplot2
On Thu, May 21, 2009 at 11:19 PM, baptiste auguie
<bapt...@googlemail.com> wrote:
> Hi,
>
> I can think of two situations but it's hard to tell without reproducible code,
>
> 1) the page layout is a result of facetting (possibly with a dummy
> factor) or wrapping, in which case there should be one common legend
> and its default position should be nicely aligned in the page
>
> 2) you don't want to use the first option, so you've placed two
> unrelated ggplots on a page using grid.layout or other viewport
> functions. Perhaps you can do something like this,

Yes, that was the case. I used grid.layout. I should have specified
that. The below solution you proposed works. Thanks a lot.

dani
Reply all
Reply to author
Forward
0 new messages