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())