'R ggplot with multiple legends, reduce space between two legends
I have a ggplot with two different legends (two different spatial features) that I would like to combine. They do NOT require any additional text, as irl this is a map and map legends can be combined.
REPREX
ggplot(iris)+
geom_point(aes(x=Petal.Length,y=Sepal.Length, color=Species, size=Petal.Width))+
guides(color=guide_legend(title=NULL, byrow=T),
size=guide_legend(title=NULL,byrow=T))+
theme(legend.spacing.y=unit(0.5,"lines"))
So how do I remove the space between the two legends to make it "look like" there is only one legend.
I've tried title=element_blank() and that doesn't change much (except the printing order of the legends, oddly enough).
Solution 1:[1]
Besides legend.spacing you could reduce or remove the margins via legend.margin:
library(ggplot2)
ggplot(iris) +
geom_point(aes(x = Petal.Length, y = Sepal.Length, color = Species, size = Petal.Width)) +
guides(
color = guide_legend(title = NULL, byrow = T),
size = guide_legend(title = NULL, byrow = T)
) +
theme(
legend.spacing.y = unit(0, "lines"),
legend.margin = margin(0, 0, 0, 0)
)

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stefan |
