R - ggplot - geom_point: fill with border only one color

Viewed 815

I'm using the mpg dataset in R. Trying to do a scatterplot graph with points filled based on 'drv' category with a white border. First image shown below is what I want (taken from https://r4ds.had.co.nz/data-visualisation.html section 3.6). But the graph I get is just one color (2nd picture). Where am I going wrong? Thank you.

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, fill = drv, color = "white", stroke = 3))

What I want

What I'm getting

1 Answers

As @stefan mentioned in the comments the default shape for geom_point() is solid and only has a color aesthetic but not a fill or stroke attribute. To use those you need to change the shape to something else such as 21. See here for more details on some of the available shapes... there are MANY.

library(tidyverse)

ggplot(data = mpg) +
  geom_point(
    mapping = aes(x = displ, y = hwy, fill = drv),
    color = "white",
    stroke = 3,
    shape = 21,
    size = 4
  )

Created on 2022-02-09 by the reprex package (v2.0.1)

Related