Would anyone know how to reduce the distance between the keys of the size legend?
There's no need to adjust the distance of the other (color) legend.
BTW, abbreviated code looks like this:
data %>%
ggplot( aes(x=x, y=y, size=z, color=c, alpha=d))
geom_point()
scale_size_area(name="Cumulative excess deaths (thousands)",
max_size = 70, limits=c(NA, 10000),
breaks=c(1, 10, 100,500,1000, 5000))
geom_abline(slope=1, intercept = 0, size=0.2)
scale_color_manual(name="World Bank Income group")
scale_alpha_manual(values=c(0.55,0.2))
labs(...)
theme_bw()
theme(
plot.margin = margin(0.5, 0.2, 0.5, 0.5, "cm"),
plot.background = element_rect(size = 1),
plot.caption.position="plot",
legend.position="right",
legend.box.margin = margin(2,0,0,0, "cm"),
legend.title=element_text(size=8,color="black", hjust=0),
legend.text =element_text(size=8,color="black"),
legend.spacing = unit(-0.5, "cm"),
legend.key.size = unit(1,"line"),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.border = element_blank())
coord_cartesian(clip = 'off')
guides(alpha=FALSE)
guides(size=guide_legend(override.aes=list(shape=1),order=1),
color = guide_legend(override.aes = list(size=6, alpha=0.55),
order=2))
CodePudding user response:
Whilst it's hard to be sure without the real data, the 3 examples below (with made-up data and minimal code) suggest to me it's the scale_size_area
with larger max_size
and smaller limit
which cause the issue (second example).
The first and third examples look reasonable by comparison.
library(tidyverse)
df <- tribble(
~x, ~y, ~z, ~c,
200, 250, 1, 1,
300, 270, 20, 1,
400, 300, 500, 2,
600, 325, 50000, 3
)
# Scale_size with trans
df |>
mutate(c = factor(c)) |>
ggplot(aes(x = x, y = y, size = z, color = c))
geom_point()
scale_size_continuous(
name = "Cumulative excess\ndeaths (thousands)",
range = c(0, 20),
trans = "sqrt",
breaks = c(1, 10, 100, 1000, 5000)
)
# Scale_size_area with large max_size and smaller limit
df |>
mutate(c = factor(c)) |>
ggplot(aes(x = x, y = y, size = z, color = c))
geom_point()
scale_size_area(
name = "Cumulative excess\ndeaths (thousands)",
max_size = 70, limits = c(NA, 10000),
breaks = c(1, 10, 100, 500, 1000, 5000)
)
# Scale_size_area with smaller max_size and larger limit
df |>
mutate(c = factor(c)) |>
ggplot(aes(x = x, y = y, size = z, color = c))
geom_point()
scale_size_area(
name = "Cumulative excess\ndeaths (thousands)",
max_size = 30, limits = c(NA, 50000),
breaks = c(1, 10, 100, 500, 1000, 5000)
)
Created on 2022-06-16 by the reprex package (v2.0.1)