Description Usage Arguments Value Examples
Align the legend keys relative to the legend title
1 | align_legend(p, hjust = 0.5)
|
p |
The plot on which the legend should be aligned |
hjust |
Horizontal alignment parameter. 0 means left-aligned, 0.5 centered, and 1 right-aligned. |
A gtable
object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # single legend
p <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Petal.Width)) + geom_point()
ggdraw(align_legend(p)) # centered
ggdraw(align_legend(p), hjust = 1) # right aligned
# multiple legends
p2 <- ggplot(mtcars, aes(disp, mpg, fill = hp, shape = factor(cyl), size = wt)) +
geom_point(color = "white") +
scale_shape_manual(values = c(23, 24, 21), name = "cylinders") +
scale_fill_continuous_carto(palette = "Emrld", name = "power (hp)", breaks = c(100, 200, 300)) +
xlab("displacement (cu. in.)") +
ylab("fuel efficiency (mpg)") +
guides(shape = guide_legend(override.aes = list(size = 4, fill = "#329D84")),
size = guide_legend(override.aes = list(shape = 21, fill = "#329D84"),
title = "weight (1000 lbs)")) +
theme_half_open() + background_grid()
# works but maybe not the expected result
ggdraw(align_legend(p2))
# more sensible layout
ggdraw(align_legend(p2 + theme(legend.position = "top", legend.direction = "vertical")))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.