The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
ggguides provides styling functions to customize legend appearance without diving into ggplot2’s theme element hierarchy. The main functions are:
legend_style() - Comprehensive styling (fonts,
backgrounds, borders)legend_keys() - Override key appearance (size, alpha,
shape)legend_order() - Reorder legend entrieslegend_wrap() - Multi-column/row layoutslegend_reverse() - Reverse entry ordercolorbar_style() - Customize continuous color bar
legendsAdjust the overall text size (applies to both title and labels):
p <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point(size = 3) +
labs(color = "Cylinders")
p + ggtitle("Default size")
p + legend_style(size = 14) + ggtitle("size = 14")For legends with long category names, rotate the labels to save space:
p_long <- ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point()
p_long + legend_style(angle = 45)Add visual containers around the legend:
Adjust the size of legend keys (color swatches):
Combine all styling options:
p + legend_style(
size = 11,
title_size = 13,
title_face = "bold",
family = "sans",
key_width = 1.2,
background = "#FFF3E0",
background_color = "#FF9800",
margin = 0.3
)For legends with many entries, legend_wrap() creates
multi-column or multi-row layouts.
When plot aesthetics like small point sizes or low alpha values make
legend keys hard to read, legend_keys() overrides the key
appearance without affecting the plot.
p_small <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point(size = 1) +
labs(color = "Cylinders")
p_small + ggtitle("Small points in legend")
p_small + legend_keys(size = 4) + ggtitle("Enlarged legend keys")p_alpha <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point(alpha = 0.3, size = 3) +
labs(color = "Cylinders")
p_alpha + ggtitle("Transparent legend keys")
p_alpha + legend_keys(alpha = 1) + ggtitle("Opaque legend keys")ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point(alpha = 0.3, size = 1) +
legend_keys(size = 4, alpha = 1)By default, legend_keys() applies to both colour and
fill legends. Target specific aesthetics with the aesthetic
parameter:
ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(cyl))) +
geom_boxplot(alpha = 0.5) +
legend_keys(alpha = 1, aesthetic = "fill")Use named shapes for clarity instead of numeric codes:
p + legend_keys(shape = "square") + ggtitle("Square")
p + legend_keys(shape = "diamond") + ggtitle("Diamond")Available shape names include: "circle",
"square", "diamond", "triangle",
"plus", "cross", "asterisk".
Shapes 21-25 (or names ending in _filled) support both
outline and fill colors, making legends more visible against any
background:
# White fill with colored outline - works with color mapping only
p + legend_keys(shape = "circle_filled", fill = "white", stroke = 1.5) +
ggtitle("White fill, colored outline")
# Colored fill with black outline - requires mapping BOTH color and fill
ggplot(mtcars, aes(mpg, wt, color = factor(cyl), fill = factor(cyl))) +
geom_point(size = 3, shape = 21, stroke = 1) +
legend_keys(colour = "black", stroke = 1) +
ggtitle("Colored fill, black outline")
#> Warning: Duplicated `override.aes` is ignored.Note: For colored fills with a custom outline, you must map both
color and fill in the plot aesthetics. This is
a ggplot2 limitation—override.aes can only set static
values, it cannot make fill inherit from color.
Shape types:
| Type | Shapes | Fill from | Outline from |
|---|---|---|---|
| Outline only | "circle_open", "square_open",
"diamond_open" |
N/A | colour |
| Solid filled | "circle", "square",
"diamond" |
colour |
N/A |
| Fill + outline | "circle_filled", "square_filled",
"diamond_filled" |
fill |
colour |
legend_order() changes the order of legend entries
without modifying factor levels in your data.
Apply functions like rev or sort to the
current order:
For simple reversal, legend_reverse() is a convenient
shorthand:
All ggguides functions compose with +:
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
legend_left() +
legend_style(
size = 11,
title_face = "bold",
background = "#FFF3E0"
)ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
legend_wrap(ncol = 2) +
legend_bottom() +
legend_style(size = 10, title_face = "bold")For continuous scales, colorbar_style() customizes the
color bar appearance.
Create taller, thinner bars:
p_cont + colorbar_style(width = 10, height = 0.5, direction = "horizontal", aesthetic = "fill") +
legend_bottom()
### Adding a Frame
p_cont + colorbar_style(frame = TRUE, aesthetic = "fill") + ggtitle("Black frame")
p_cont + colorbar_style(frame = "#FF9800", aesthetic = "fill") + ggtitle("Orange frame")| Function | Purpose | Key Parameters |
|---|---|---|
legend_style() |
Comprehensive styling | size, title_size, title_face,
family, angle, background,
margin |
legend_keys() |
Override key appearance | size, alpha, shape,
fill, colour, stroke,
aesthetic |
legend_order() |
Reorder entries | order (vector or function), aesthetic |
legend_wrap() |
Multi-column layout | ncol, nrow |
legend_reverse() |
Reverse entry order | None |
colorbar_style() |
Continuous color bar | width, height, frame,
ticks, direction |
Learn more:
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.