There's a similiar thread posted here: Change point size of a ggplot object after being returned from a function
But this user posted a custom function, and I'm looking for a solution that exists within ggplot2.
If I have a ggplot2 object, named plot
, that has a default geom_point()
size of 5. How can I change the size without having to re-create the original object. I thought something like this would work:
plot + geom_point(size = 2)
But it still does not override the size.
Here's a more concrete example:
data:
test.data <- structure(list(Cars = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710",
"Hornet 4 Drive", "Hornet Sportabout", "Valiant", "Duster 360"
), mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3), cyl = c(6,
6, 4, 6, 8, 6, 8), disp = c(160, 160, 108, 258, 360, 225, 360
), hp = c(110, 110, 93, 110, 175, 105, 245), drat = c(3.9, 3.9,
3.85, 3.08, 3.15, 2.76, 3.21), wt = c(2.62, 2.875, 2.32, 3.215,
3.44, 3.46, 3.57), qsec = c(16.46, 17.02, 18.61, 19.44, 17.02,
20.22, 15.84)), row.names = c(NA, -7L), class = c("tbl_df", "tbl",
"data.frame"))
> test.data
# A tibble: 7 x 8
Cars mpg cyl disp hp drat wt qsec
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Mazda RX4 21 6 160 110 3.9 2.62 16.5
2 Mazda RX4 Wag 21 6 160 110 3.9 2.88 17.0
3 Datsun 710 22.8 4 108 93 3.85 2.32 18.6
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.22 19.4
5 Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.0
6 Valiant 18.1 6 225 105 2.76 3.46 20.2
7 Duster 360 14.3 8 360 245 3.21 3.57 15.8
test.data %>% ggplot(aes(x = mpg, y = cyl)) + geom_point(size = 5)
test.plot <- test.data %>% ggplot(aes(x = mpg, y = cyl)) + geom_point(size = 5)
test.plot + geom_point(size = 1)