How to specify legend width in ggplot

Using ggplot, I have multiple plots, each of which has a legend box. I want the legend box to be the same width for each plot, but ggplot dynamically sizes the legend box based on the legend name, key values, etc. (which are unique to each plot).

The various plots must fit into a specified publication slot, with a specified width for the legend, and the plots must be made separately (so faceting to guarantee identical legend widths across the plots isn't possible).
In essence, I have a fixed 'template' for the plot, such that some external elements outside the plot's boundaries are 'lined-up' correctly.

Looking at theme I couldn't find an option to specify the legend box width ... any ideas?
I played around with the legend margin, but really I want to specify the legend 'minimal' bounding-box plus have the margin be dynamically padded such that the sum of the two is a fixed-width box.
Alternatively, I thought to specify the document width, then specify the plot and/or legend widths, left-justify those elements, then let there be 'free space' on the right side of the document, but I can't seem to find any existing set of options to let me easily do this.

For that matter, I can't seem to figure out how to specify margin width, either. theme(plot.margin = margin(...)) seems to allow some control of the margin, but I can't seem to guarantee the amount of space between the actual plotting region (i.e. the data region or 'viewport') and the edge of the device (a PDF, in this case).
This is pretty easy to do with base R plotting, I'm curious if I'm just missing something obvious with ggplot?

Try using

theme(legend.key.width=unit(4,"cm"))

2 Likes

Thanks for the suggestion, but that simply makes the legend itself wider.
I'd like the legend to still be its default size, but with dynamic padding.
In essence, I want the legend to be as compact as it's programmed to be, but have that legend's bounding box plus the dynamic padding to fill a specified width (or rectangle).

In base R plotting, this is accomplished with relatively straight-forward techniques like setting the oma and mar parameters (via par()), and then, given some fixed margins, one can go ahead and fill them up as best as possible.
Ultimately, I'll have the same problem with the margins around the (data) plot area, e.g. when filling those margins with axis labels.

Basically, I have a fairly precise template to which each plot must conform (e.g. in a publication or a web image slot with elements external to the plot that must line up with the plot).
Traditionally, I've used base R to create these plots, but I've tried moving towards ggplot recently and find the inability to specify a 'scaffold' for bounding boxes of the figure to be the largest hurdle.

I think that the patchwork package (GitHub only) takes care of that detail! The cowplot package may as well... Both contain ways to combine ggplots that have the elements align nicely. Hope that helps!

Look also at the cowplot package

The vignettes on CRAN give nice examples

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.