reposition_legend | R Documentation |
Repositions a legend onto a panel, by either taking it from the same ggplot, or by using another. Works on both ggplot2 and gtable objects, and can accept any grob as legend.
reposition_legend(
aplot,
position = NULL,
legend = NULL,
panel = "panel",
x = NULL,
y = NULL,
just = NULL,
name = "guide-box",
clip = "on",
offset = c(0, 0),
z = Inf,
plot = TRUE
)
aplot |
a ggplot2 or gtable object. |
position |
Where to place the legend in the panel.
Overrules |
legend |
The legend to place, if |
panel |
Name of panel in gtable. See description. |
x |
horisontal coordinate of legend, with 0 at left. |
y |
vertical coordiante of legend, with 0 at bottom. |
just |
'Anchor point' of legend; it is this point of the legend that is
placed at the |
name , clip , z |
Parameters forwarded to
|
offset |
Numeric vector, sets distance from edge of panel.
First element for horisontal distance, second for vertical.
Not used by arguments |
plot |
Logical, when |
To modify the look of the legend, use themes and the natural ggplot functions
found in guide_legend
.
Positioning is done by argument position
which places the panel
relative in panel
(see below).
position
resolves to three variables, x
, y
, and just
.
x
and y
is the coordinate in panel
, where the anchorpoint of
the legend (set via just
) is placed.
In other words, just='bottom right'
places the bottom right corner of
the legend at coordinates (x,y)
.
The positioning can be set by argument position
alone, which can be
further nudged by setting position
, x
, and y
.
Alternatively, manually positioning can be obtained by setting arguments.
x
, y
, and just
.
Panel name is by default panel
, but when using facets it typically
takes the form panel-{col}-{row}
, but not for wrapped facets.
Either print result from ggplotGrob
or use
gtable_show_names
to display all the names of the gtable
object.
panel
takes multiple names, and will then use these components'
extremes for placing the legend.
If panel
is an integer vector of length 2 or 4, these elements are
used directly for top-left and bottom-right coordinates.
gtable object, invisibly, with legend repositioned.
Can be drawn with grid.draw
.
Stefan McKinnon Edwards <sme@iysik.com>
g_legend
, grid_arrange_shared_legend
and gtable_show_names
for displaying names of facet's panels.
library(ggplot2)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
(d <- ggplot(dsamp, aes(carat, price)) +
geom_point(aes(colour = clarity)))
reposition_legend(d + theme(legend.position='bottom'), 'bottom right')
# To change the orientation of the legend, use theme's descriptors.
reposition_legend(d + theme(legend.position='bottom'), 'top left')
# Use odd specifications, here offset the legend with half its height from the bottom.
reposition_legend(d + theme(legend.position='bottom'), x=0.3, y=0, just=c(0, -0.5))
# For using with facets:
reposition_legend(d + facet_grid(.~cut), 'top left', panel = 'panel-1-5')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.