Decrease legend size?

4,929 views
Skip to first unread message

Nick S

unread,
Jan 28, 2010, 2:38:00 PM1/28/10
to ggplot2
Is it currently possible to specify the size/dimensions of the legend
on a graph? I am currently packing multiple graph objects in the same
window and find that the legend size stays the same. This eventually
becomes disproportionate to the size of the graph itself.

Thanks.

JiHO

unread,
Jan 28, 2010, 4:09:17 PM1/28/10
to Nick S, ggplot2

I don't think you can... but you can make the plotting window larger ;)

Without kidding, assuming that this is meant to be output to a device
(pdf, png etc.), what you can do is make the device larger; the legend
(which has a fixed size) would then be relatively smaller. The rest of
the plot will be shrinked and that can be a problem for the text but
you can increase its size with:
theme_set(theme_gray(16))
All your lines will still appear thiner but that would be more complex
to sort out.

That's a clunky workaround but this is the only way I know to change
the relative size of items (in base R or in ggplot for that matter).

JiHO
---
http://maururu.net

baptiste auguie

unread,
Jan 28, 2010, 4:23:55 PM1/28/10
to ggplot2
On a similar spirit, you could use grid viewports to place the various
ggplot2 objects on the page. Because ggplot2 objects always take up
the full viewport space, you could use this to shrink / expand the
relative size of the various plots and legends. Using opts(drop /
keep) you can remove / isolate the legend of a given plot and place it
in a separate viewport with the size you want.

HTH,

baptiste

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

Andreas Christoffersen

unread,
Sep 19, 2010, 9:53:20 AM9/19/10
to Nick S, ggplot2
Hi all,

I really don't know if it is inappropriate to raise this issue again. However, since messing around with viewports seams very messy (to me at least), i'd like to add this as a feature request. Unless of course hadly has a specific reason not to?

I am only asking because the issue arises quite often when e.g. working with complex facets - and for me anyways - legends is basically the only quirk I have with ggplot. 

Sincerely

Andrie de Vries

unread,
Sep 22, 2010, 10:00:37 AM9/22/10
to ggplot2
Andreas

Building on Baptiste's suggestion, and using some of his code that is
widely available on the net, I have create a custom function that does
what he suggests.

It's not perfect, but probably a good enough workaround for many
purposes. You should be able to use or customize to get your desired
results.

Andrie


library(ggplot2)
library(grid)

# Create a function with three arguments
# p : pre-defined plot
# legend_size : legend size, expressed as percentage of window width
# legend_text_size : font size in points

ggplot_size_legend <- function(p, legend_size=0.1, legend_text_size=10)
{
Layout <- grid.layout(nrow = 1,
ncol = 2,
widths = unit(c(1-legend_size, legend_size),
c("null", "null")),
heights = unit(1, "null"))

vplayout <- function(...) {
grid.newpage()
pushViewport(viewport(layout = Layout))
}

subplot <- function(x, y) viewport(layout.pos.row = x,
layout.pos.col = y)

#create two plots, one with legend, one without

pl <- p + opts(legend.position = "none")
pn <- p + theme_grey(legend_text_size) + opts(keep = "legend_box")

# print the plot

vplayout()
print(pl, vp = subplot(1, 1))
print(pn, vp = subplot(1, 2))
}


# create plot

p <- qplot(data = mtcars, mpg, hp, geom = "point", colour = cyl)

# large legend
ggplot_size_legend(p, 0.2, 20)

# tiny legend
ggplot_size_legend(p, 0.1, 8)

Andreas Christoffersen

unread,
Sep 24, 2010, 3:56:03 AM9/24/10
to Andrie de Vries, ggplot2
You sir, are an absolute star! - This will make my life easier (thank you) and since I will have it as an R template I will always look at really good example of how to use grid. Hopefulle this will make me grok it, for world benefit. 

Hope to help someone, like you just helped me.

Thanks again!

Felipe Carrillo

unread,
Sep 24, 2010, 10:22:03 AM9/24/10
to Andrie de Vries, ggplot2
It is nice to have this option Andrie and this  will work nicely particularly
when the legend
labels are quite long. Would this function work if one wants to position the
legend somewhere inside
the plot area, for instance on the upper right or left corner?
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

> --
> You received this message because you are subscribed to the ggplot2 mailing
>list.

> Please provide a reproducible example: http://gist.github.com/270442
>
> 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