2

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

user438383
  • 5,716
  • 8
  • 28
  • 43
zze86
  • 31
  • 5
  • 5
    Try `ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, fill = drv), color = "white", stroke = 3, shape = 21)`. Default points have no fill. Therefore use `shape=21` = points with fill. Additionally if you want to set a color, stroke or shape do so outside of aes(). – stefan Feb 09 '22 at 18:41

1 Answers1

2

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)

Dan Adams
  • 4,971
  • 9
  • 28