Increase space between legend keys without increasing legend keys
One solution is to replace lines with points (requires additional geom layer):
Create plot with invisible points (size = 0
and rectangle shape shape = 15
).
p <- ggplot(d, aes(x, ..density..)) +
geom_histogram(fill = "lightblue", color = "black") +
geom_vline(data = vlines, mapping = aes(xintercept = x, colour = stat)) +
geom_point(data = vlines, aes(0, 0, colour = stat), size = 0, shape = 15)
Add legend theme to:
- Mask background color in legend (
legend.key = element_rect(fill = "white")
) - Create large legend (
legend.key.height = unit(3, "cm")
) - Remove lines (
linetype = 0
) and make large points (size = 5
)
Code:
p +
theme(legend.direction = "vertical",
legend.position = "right",
legend.key = element_rect(fill = "white"),
legend.key.height = unit(3, "cm")) +
guides(color = guide_legend(override.aes = list(linetype = 0, size = 5)))
PS.:
- This is not a perfect solution as there's a gap between legend label and boxes.
- If you want lines instead of rectangles use
shape = 73
Increase Vertical Spacing between Legend Key in ggplot2
After browsing ggplot2's source code for a bit, I come to the conclusion that the legend.spacing.y
is only applied when the byrow = TRUE
as argument to the legend.
Simplied example below.
library(ggplot2)
ggplot(iris, aes(Sepal.Width)) +
geom_density(aes(fill = Species)) +
guides(fill = guide_legend(byrow = TRUE)) +
theme(legend.spacing.y = unit(1, "cm"))
With regards to the labels, just remove the values from the breaks
argument in scale_y_continuous()
that you don't want to show, you're already specifying them manually.
ggplot2 increase space between legend keys
An alternative (and probably easier) solution is using legend.key
and legend.key.size
in the theme
part of your code:
ggplot(data = mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() +
guides(color = guide_legend(nrow = 2)) +
theme(legend.direction = 'horizontal',
legend.position = 'bottom',
legend.key = element_rect(size = 5),
legend.key.size = unit(1.5, 'lines'))
this gives:
In case you are calling theme_bw
or theme_classic
before manipulating the legend, you should set the color of the legend rectangle:
legend.key = element_rect(size = 5, color = 'white') #or: color = NA
Spacing between legend keys in ggplot
Adding a margin to adjust element_text
mtcars %>%
mutate(transmission = ifelse(am, "manual", "automatic")) %>%
ggplot() +
aes(x = transmission, fill = transmission) +
geom_bar() +
labs(fill = NULL) +
theme(
#legend.spacing.x = unit(.5, "char"), # adds spacing to the left too
legend.position = "top",
legend.justification = c(0, 0),
legend.title = element_blank(),
legend.margin = margin(c(5, 5, 5, 0)),
legend.text = element_text(margin = margin(r = 10, unit = "pt")))
Modify spacing between key glyphs in vertical legend whilst keeping key glyph border
There is a way to do this, but it is not very intuitive. It depends on the byrow
setting in the legend guide, whether the legend spacing is respected or not (don't ask me why!).
library(ggplot2)
ggplot(dplyr::filter(msleep, grepl("^C", order)),
aes(sleep_total, sleep_rem, color = order)) +
geom_line(na.rm = TRUE) +
guides(
color = guide_legend(byrow = TRUE)
) +
theme(legend.key = element_rect(color = "black"),
legend.spacing.y = unit(1, "cm"))
Created on 2021-12-30 by the reprex package (v2.0.0)
I've complained raised the suggestion that this is unintuitive elsewhere.
If you want the title to be in a relatively normal position, you can use the following incantation to the theme function, wherein 11 pt is the default legend spacing:
legend.title = element_text(
margin = margin(
b = -1 + grid::convertUnit(unit(11, "pt"), "cm", valueOnly = TRUE),
unit = "cm")
)
Vertical spacing legend without changing key height
Something like this? I modified the legend.key
as well. You could place the legend on top and not worry about the space anymore.
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = disp, color = as.factor(gear))) +
geom_point() +
theme(legend.key = element_rect(size = 6),
legend.key.height = unit(1, "cm"),
legend.key.width = unit(1, "cm"))
Related Topics
How to Determine If a Url Object Returns '404 Not Found'
Is There Something Like a Pmax Index
How to Check If Multiple Strings Exist in Another String
Non-Equi-Joins in R with Data.Table - Backticked Column Name Trouble
Format a Vector of Rows in Italic and Red Font in R Dt (Datatable)
Using Ggplot2 with Columns That Have Spaces in Their Names
R Cleaning Up a Character and Converting It into a Numeric
How to Plot a Boxplot with Correctly Spaced Continuous X-Axis Values in Ggplot2
Warnings When Running an Lmer in R
Place Text Values to Right of Sankey Diagram
Understanding Bandwidth Smoothing in Ggplot2
Grouping Factor Levels in a Data.Table
As.Date Produces Unexpected Result in a Sequence of Week-Based Dates