Reputation: 2391
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. Overlay geom_points() on geom_boxplot(fill=group)? here is a similar question. But the key problem in mine is the shape
.
Here goes an example:
library(ggplot2)
library(dplyr)
head(mtcars)
data = data.frame(
x = factor(mtcars$vs),
y = mtcars$wt,
fill = factor(mtcars$am)
) %>%
dplyr::arrange(x, fill) %>%
dplyr::mutate(shape = rep(letters[1:4], 8))
set.seed(1)
ggplot(data, aes(x, y, fill = fill)) +
geom_boxplot() +
geom_point(position=position_jitterdodge())
I can get a plot:
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())
Upvotes: 0
Views: 1082
Reputation: 66765
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.
Upvotes: 1