I am just trying to plot a ggplot
figure of overlaying a points plot on a boxplot. I got very strange result and hope someone can tell me why and how to fix it.
Then I add shape mapping. You can see that all points changed completely. What I want is a same plot like above with only the point's shapes changed. i.e, the location of points should not change. I don't know why after adding shape mapping, the points are improperly assigned to the box group.
set.seed(1)
ggplot(data, aes(x, y, fill = fill))
geom_boxplot()
geom_point(aes(shape = shape), position=position_jitterdodge())
CodePudding user response:
In this case we want the position jittering to be "aware" of the two fill values which are distinguished the the fill
aesthetic. Since the shapes here don't have a fill
aesthetic, the layer doesn't automatically separate the two fill values before applying the jitter. To make the layer "aware" of the fill values, we could use
geom_point(aes(shape = shape, group = fill),
position=position_jitterdodge())
where group = fill
tells the layer to group the points based on that variable, which will then be reflected in the way the points are jittered.