3

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)

enter image description here

 test.plot <- test.data %>% ggplot(aes(x = mpg, y = cyl)) + geom_point(size = 5)
 test.plot + geom_point(size = 1)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Steveman30290
  • 511
  • 7
  • 17
  • Hey Death Metal, I updated with a concrete example. @MrFlick - I reworded my question because saying it was "outdated" wasn't accurate. – Steveman30290 Sep 23 '20 at 20:58
  • 5
    This should give you a hint: [Change the color of a ggplot geom a posteriori (after having specified another color)](https://stackoverflow.com/questions/48209025/change-the-color-of-a-ggplot-geom-a-posteriori-after-having-specified-another-c) The parameter you would need to change can be found here: `test.plot$layers[[1]]$aes_params$size` – markus Sep 23 '20 at 21:12
  • Note that when running `+ geom_point(size = 1)` you are adding a second point layer not changing the existing one. A plot can have multiple point layers. – MrFlick Sep 23 '20 at 21:16
  • 3
    A second option would be to [remove the point layer](https://stackoverflow.com/questions/50434608/remove-geoms-from-an-existing-ggplot-chart) `test.plot$layers[[1]] <- NULL` and then add a new one `test.plot + geom_point(color = "red", size = 7)` – markus Sep 23 '20 at 21:25

0 Answers0