I searched through internet and I couldn't find a solution for my issue.
So to make it more solid say that we have a graph with two legends like below:
library(ggplot2)
ggplot() +
geom_point(data = mtcars, aes(x = disp, y = mpg, color = gear),
pch =20, size=18) +
geom_line(data = mtcars, aes(x = disp, y = mpg, size = disp/mpg*100)) +
scale_size(range = c(0,3.5)) +
guides(size = guide_legend("", order = 1, keywidth = 2, keyheight = 1.5),
color = guide_legend("", order = 2, keywidth = 1, keyheight = 1 )) +
labs(x = "disp", y = "mpg") +
geom_text(size=2.7, color = "grey29", vjust=-0.8) +
theme_bw()
# ggsave("trial.png", width = 11.5, height = 8.5)
I can change the spacing for the first legend group related with size by using
size
option in guides
. However, for the second group which indicates color, I can't neither make the whole group closer to the graph or reduce to size between the colored circles.
I have also tried the legend options in theme such as legend.spacing.x/y
and legend.key.width/height
. These options only worked for the first legend group.
Is there a way to reduce the sizes between the different color keys? Changing the sizes of the keys would be also good to discover.
To make my request clearer, here is the gaps that I want to adjust:
Thanks in advance.