Reputation: 721
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.
Upvotes: 5
Views: 1855
Reputation: 23767
Legend spacing is always kind of an issue and gets many votes e.g. here.
I think one of the issues in your particular example could be the continuous nature of gear
. Factorising might help (if you have more values than gear
, use cut()
instead) and then change the legend spacing within your guides
call using unit()
. I have boiled down your code a bit and also replace title = ''
with title = NULL
, as otherwise ggplot is actually drawing an empty object.
library(tidyverse)
mtcars_f <- mtcars %>% mutate(gear_f = factor(gear)) #factorising gear
ggplot(mtcars_f, aes(disp, mpg)) +
geom_point(aes(color = gear_f), size = 10) +
geom_line(aes(size = disp/mpg*100)) +
guides(size = guide_legend(title = NULL, order = 1,
keyheight = unit(0.1, 'inch')),
color = guide_legend(title = NULL, order = 2,
keyheight = unit(0.1, 'inch'))) +
scale_color_brewer(palette = 'Blues') +
theme(legend.key= element_blank())
Created on 2019-07-19 by the reprex package (v0.3.0)
Upvotes: 2
Reputation: 29143
I am not exactly sure what you need, but I think you want the points in the legend be smaller. In that case, override.aes()
is the function you need.
If your question is different, please clarify further so we can help you.
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,
override.aes = list(size=9))) +
labs(x = "disp", y = "mpg") +
geom_text(size=2.7, color = "grey29", vjust=-0.8) +
theme_bw()
Created on 2019-07-08 by the reprex package (v0.3.0)
Upvotes: 1