r - Overlaying jittered points on boxplot conditioned by a factor using ggplot2 -
i making boxplot conditioned factor similar example:
p <- ggplot(mtcars, aes(factor(cyl), mpg)) p + geom_boxplot(aes(fill = factor(am)))
there few points in data set, , i'd express visually overlaying data points. want overlay points colored same factor "am" try this:
p + geom_boxplot(aes(fill = factor(am))) + geom_jitter(aes(colour = factor(am)))
the points colored factor "am" not spaced lay on box plots associated with. rather mix , cover both. know how condition geom_jitter
points associate factor "am"?
welcome so! here's attempt. it's bit clumsy, job. trick map x
dummy variable manually constructed offset. i'm adding fill scale highlight point positioning.
mtcars$cylpt <- as.numeric(factor(mtcars$cyl)) + ifelse(mtcars$am == 0, -0.2, 0.2) ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(aes(fill = factor(am))) + geom_point(aes(x = cylpt, colour = factor(am)), position = "jitter") + scale_fill_manual(values = c("white", "gray"))
Comments
Post a Comment