0
votes

I would like to customize the color of the margins of the dots of my ggplot. The color of the fill of dots summarize one information while the color of the margins will summarize another one.

Suppose to use the mtcars data:

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
    geom_point(aes(colour = factor(cyl), shape = factor(vs)))

Then suppose you want to modify the color of the margin of dots using the column mtcars$carb.

Is it possible using ggplot?

1
Do you also need to map something to shape?pogibas
You need shapes that have a separate fill and border, like shapes 21:25. So you could use scale_shape_manual(values = c(21, 22, 23) ) or something and then use fill for carb.aosmith

1 Answers

3
votes

Yes, you need to use fill for the inside color and color for the line color. And you need to use shapes that differentiate between the two (that is, shapes 21-25. Search "r pch" or see the help page at ?pch for all the shapes). Unfortunately, for the legends to look right, we need to manually specify shapes for the legends. You may also want to use at least one manual scale (e.g., scale_fill_manual) specifying your own colors so that the fill and line colors are more different.

ggplot(mtcars,
    aes(
      x = wt,
      y = mpg,
      color = factor(carb),
      fill = factor(cyl),
      shape = factor(vs)
    )) + geom_point(size = 2, stroke = 1.5) +
  scale_shape_manual(values = c(21, 24)) +
  scale_fill_hue(guide = guide_legend(override.aes = list(shape = 21))) +
  scale_color_hue(guide = guide_legend(override.aes = list(shape = 21)))

enter image description here See also the example at the bottom of the ?geom_point help page:

# For shapes that have a border (like 21), you can colour the inside and
# outside separately. Use the stroke aesthetic to modify the width of the
# border
ggplot(mtcars, aes(wt, mpg)) +
  geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 5)