1

I want to generate bubble plots where the bubbles have a black outline. However, for some reason, I'm having trouble getting geom_point to accept scale_fill. This gives me a nice plot where the bubble color scales with a continuous variable, color:

age <-c(16, 5, 6, 22, 11, 12, 11, 13, 4, 8)
y <- c(0.53, 0.50, 0.50, 0.46, 0.44, 0.44, 0.44, 0.43, 0.40, 0.40) 
s <- c(11.5, 78.0, 753.5, 44.5, 372.0, 62.0, 163.0, 25.0, 56.0, 80.5)
color <- c(29, 15, 7, 30, 15, 26, 8, 14, 17, 12) 

df <- data.frame(age, y, s, color)  

p <- ggplot(df)+
   geom_point(aes(x = age, y = y, size = s, color = color))+
   labs(x = "age", y = "rate")+
   scale_size(range = c(.1, 10), name="s")+
   scale_color_viridis(limits = c(5, 20), oob = squish, option = "magma")+
   scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
   scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
   theme_minimal()

But if I just switch color to fill, I get all black bubbles:

   p <- ggplot(df)+
      geom_point(aes(x = age, y = y, size = s, fill = color))+
      labs(x = "age", y = "rate")+
      scale_size(range = c(.1, 10), name="s")+
      scale_fill_viridis(limits = c(5, 20), oob = squish, option = "magma")+
      scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
      scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
      theme_minimal()

If I specify shape:

p <- ggplot(df)+
      geom_point(aes(x = age, y = y, size = s, shape = 21, fill = color))+
      labs(x = "age", y = "rate")+
      scale_size(range = c(.1, 10), name="s")+
      scale_fill_viridis(limits = c(5, 20), oob = squish, option = "magma")+
      scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
      scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
      theme_minimal()
      

I get

Error in scale_f(): ! A continuous variable can not be mapped to shape

If I understand correctly, fill should specify bubble color and color should let me specify a black outline around the bubbles. What am I missing here? Thanks.

user237554
  • 79
  • 8

2 Answers2

3

Your error says that you can't assign a continuous variable to shape. You should place shape outside your aes like this:

age <-c(16, 5, 6, 22, 11, 12, 11, 13, 4, 8)
y <- c(0.53, 0.50, 0.50, 0.46, 0.44, 0.44, 0.44, 0.43, 0.40, 0.40) 
s <- c(11.5, 78.0, 753.5, 44.5, 372.0, 62.0, 163.0, 25.0, 56.0, 80.5)
color <- c(29, 15, 7, 30, 15, 26, 8, 14, 17, 12) 

df <- data.frame(age, y, s, color)  

library(ggplot2)
library(viridis)
library(scales)
p <- ggplot(df)+
  geom_point(aes(x = age, y = y, size = s, fill = color), shape = 21)+
  labs(x = "age", y = "rate")+
  scale_size(range = c(.1, 10), name="s")+
  scale_fill_viridis(limits = c(5, 20), oob = squish, option = "magma")+
  scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
  scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
  theme_minimal()
p
#> Warning: Removed 1 rows containing missing values (geom_point).

Created on 2022-08-29 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53
1

I am not quite sure if this is your expectec output, but please recognize this:

Note that shapes 21-24 have both stroke colour and a fill. The size of the filled part is controlled by size, the size of the stroke is controlled by stroke. Each is measured in mm, and the total size of the point is the sum of the two. Note that the size is constant along the diagonal in the following figure.

source: vignette("ggplot2-specs")

library(tidyverse)

ggplot(df, aes(x = age, y = y, size = s, color = color))+
  geom_point(fill = color, shape=21)+
  labs(x = "age", y = "rate")+
  scale_size(range = c(.1, 10), name="s")+
  scale_color_viridis(limits = c(5, 20), oob = squish, option = "magma")+
  scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
  scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
  theme_minimal()

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66