Scale geom_point size to increase size based on distance from zero

You can override the legend guides to make it work. The idea is that you need to specify the number of legend's size.

library(tidyverse) 

set.seed(2018)
year <- rep(c(2015:2018), each = 3)
parameters <- rep(c("length", "weight", "condition"), 4)
z_score <- runif(12, min = -5, max = 5)
df <- tibble(year, parameters, z_score)

# The size of the legends
legend_size <- c(10,8,5,1,5,8,10)

cols <- c("#d73027",
          "darkgrey",
          "#4575b4")

ggplot(df, aes(year, parameters)) +
  geom_point(aes(colour = z_score, size = z_score)) +
  scale_color_continuous(low = "blue", high = "red", breaks = c(-3:3) ) +
  scale_size(breaks = c(-3:3), range = c(1,15)) +
  guides(
    color= guide_legend(), 
    size=guide_legend(override.aes = list(size = legend_size))
  )

Created on 2018-12-19 by the reprex package (v0.2.1)

I wonder why would you want to use 2 aesthetics (size and color) for 1 variable? Is there any extra information added compare to only 1 aesthetic?.

This answer was based on a StackOverflow answer

4 Likes