1

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.

enter image description here

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))
Philip
  • 57
  • 5
  • very related https://stackoverflow.com/questions/11366964/is-there-a-way-to-change-the-spacing-between-legend-items-in-ggplot2 – tjebo Jun 14 '22 at 10:28
  • my 2 cents worth: this will not look good. The distance between your key glyphs is varying, thus this might result in overlap. Of another note, although optically maybe pleasing, coding quantitative information as area, and in particular "bubbles", is tricky (is the "5000" circle really 5x bigger than the "1000" circle? ) – tjebo Jun 14 '22 at 10:30
  • P.s. if you wanna stick to the bubbles, maybe a logarithmic scale would make sense too – tjebo Jun 14 '22 at 10:32
  • Regarding the comment about the bubbles showing quantitative information, what would be a better alternative? – Philip Jun 15 '22 at 11:10
  • I neglected to add this to the code, but I had originally in there: scale_size_area(name="Cumulative excess deaths (thousands)", max_size = 70, limits=c(NA, 10000), breaks=c(1, 10, 100,500,1000, 5000))+ Perhaps that caused the problem. – Philip Jun 15 '22 at 11:12
  • check for example https://clauswilke.com/dataviz/aesthetic-mapping.html chapter 2 and 6. The entire book is really really good and I truly recommend to read it completely. You might consider using color instead (sort of a heatmap). If you want to code as size, Square areas are easier to gauge. Or remove one dimension from the graph and facet into differet categories (e.g., low, middle and high excess death) with arbitraty cut-offs but only one size. The idea of this would be to de-clutter the graph and make the actual message behind it clearer – tjebo Jun 15 '22 at 11:24
  • Yes. I deleted the comment and put it below. – Philip Jun 16 '22 at 08:57

1 Answers1

1

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)

Carl
  • 4,232
  • 2
  • 12
  • 24
  • As noted above, the solution works but doesn't give control over the range of variation in the bubbles. Is there a way to control their size (using 5,000 not 50,000 as the maximum observation and still have the visual pattern as in the original chart?). One can use range or max_size, but that triggers the variable space between keys. Code: scale_size_continuous(range = c(0,40),breaks = c(1, 10, 100, 1000, 5000)) or scale_size_area(max_size = 40, limits=c(NA, 10000),breaks=c(1, 10, 100,1000,5000) – Philip Jun 15 '22 at 11:41
  • Not sure I follow the "triggers the variable space between keys". I've used max 5,000 above, but I'm not sure I'm understanding your concern with the keys. – Carl Jun 15 '22 at 11:57
  • If you want the bubbles to grow more, then use a transformation, e.g. `trans = "sqrt"` – Carl Jun 15 '22 at 12:07
  • The issue with the space between the keys: compare the legend in the original chart I posted with the legend in the chart in the answer above. The space between items grows as the bubble becomes larger in the first chart, whereas it doesn't in the second. – Philip Jun 15 '22 at 13:16
  • Do you have other code that's not in the abbreviated version, e.g. different theme, that could be causing the different behaviour? If so, could you try incrementally adding those lines to the answer version to see which, if any, may be causing the problem? – Carl Jun 15 '22 at 13:25
  • Thanks for this suggestion. Note: I have updated the original post to add all the relevant lines. Don't see really what could be causing the problem, but will try your suggestion. – Philip Jun 16 '22 at 08:56
  • Added my thoughts with 3 examples. – Carl Jun 16 '22 at 09:33