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.
CodePudding user response:
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).