0

I would like to create a scatterplot such that geom_point has different shapes, all shapes are outlined in the same color, but each distinct shape is filled with it's own color.

Per the example below, I want all of the points to be outlined in blue. I want the squares to be filled in red, the circles to be filled in magenta, and the triangles filled in yellow.

ggplot(mtcars, aes(x = mpg, y = wt, shape = factor(gear), fill = factor(gear))) +
      geom_point(size = 5, color = "blue") + 
      scale_shape_manual(values = c(0,1,2)) + 
      scale_fill_manual(values = c( "red", "magenta", "yellow"))

I have tried adding fill , and scale_fill_manual every which way that I can think of.

hhillman231
  • 125
  • 1
  • 2
  • 9
  • 3
    Those shapes don't have fill. Use `scale_shape_manual(values = c(22, 21, 24))` instead. More info: http://www.sthda.com/english/wiki/r-plot-pch-symbols-the-different-point-shapes-available-in-r – Skaqqs May 19 '22 at 16:37
  • 1
    I've always been fascinated/annoyed by how 0-2 and 15-17 match shape and order, but the 21+ are completely different ... (just a comment, @Skaqqs, I suggest you post an answer with that) – r2evans May 19 '22 at 17:25
  • @r2evans, thanks for your suggestion! I combined the info in your comment and mine as an answer. Feel free to edit or let me know if you have any more comments. Cheers – Skaqqs May 19 '22 at 17:54

1 Answers1

1

Those shapes don't have fill. Use scale_shape_manual(values = c(22, 21, 24)) instead.

ggplot(mtcars, aes(x = mpg, y = wt, shape = factor(gear), fill = factor(gear))) +
      geom_point(size = 5, color = "blue") + 
      scale_shape_manual(values = c(22, 21, 24)) + 
      scale_fill_manual(values = c( "red", "magenta", "yellow"))

enter image description here


Below are all the available shapes in base R with their associated pch codes below in grey. Note that values 0-14 are open (don't have fill); 15-20 are closed (black filled); and 21-25 have a customizable fill attribute (I've specified orange here). Unfortunately 0-2 and 15-17 match shape and order, but 21-25 march to their own beat.

shp <- data.frame(
     x = c(1:7, 1:8, 1:6, 1:5),
     y = c(rep(4, 7), rep(3, 8), rep(2, 6), rep(1, 5)),
     pch = 0:25)
plot(shp$x, shp$y, pch = shp$pch, col = "black", bg = "orange", ylim = c(0,5), cex = 3,
     yaxt="n", xaxt="n", xlab="", ylab="", bty="n")
text(shp$x, shp$y-0.2, shp$pch, pos = 1, cex=1, col="grey40") 

enter image description here

Skaqqs
  • 4,010
  • 1
  • 7
  • 21