'ggplot2 - filling in manual shapes with manual colors
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.
Solution 1:[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"))
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")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


