aho aho - 1 month ago 20
R Question

filled / open circle by group

I have trouble making a filled / open circle plot by group.

My data looks like:

Body_weight Gender
50 M
60 M
55 M
45 F
43 F
42 F


I want to plot the male weights in filled circle and the female weights in open.

I tried:

ggplot(data, aes(x=Gender, y=Body_weight))+
geom_point(aes(fill=Gender))


But, this only gives filled circles for both gender.
I have no idea why this does not work.

geom_point(aes(shape=Gender))
works and gives a circle / triangle plot.

Answer

The aesthetic "fill" does not determine if the point is filled with colour or not, but fills it with a different colour for each gender. The way to do what you want is using "shape" and mapping the shapes to open and closed circle instead of the default circle/triangle you get. To do this you need to use scale_shape_manual.

ggplot(data, aes(x=Gender, y=Body_weight)) + 
    geom_point(aes(shape=Gender)) +
    scale_shape_manual(values = c(16, 21))

Have a look at http://sape.inf.usi.ch/quick-reference/ggplot2/shape for other shapes available enter image description here

Comments