Reputation: 109
Align the data points with the box plot.
DATA:
data<-structure(list(score = c(0.058, 0.21, -0.111, -0.103, 0.051,
0.624, -0.023, 0.01, 0.033, -0.815, -0.505, -0.863, -0.736, -0.971,
-0.137, -0.654, -0.689, -0.126), clin = structure(c(1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L), .Label =
c("Non-Sensitive",
"Sensitive "), class = "factor"), culture = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Co-culture", "Mono-culture"), class = "factor"),
status = structure(c(2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L), .Label = c("new", "old"
), class = "factor")), .Names = c("score", "clin", "culture",
"status"), class = "data.frame", row.names = c(NA, -18L))
CODES:
p<-ggplot(data, aes(culture, as.numeric(score),fill=status))
p+geom_boxplot(outlier.shape = NA)+
theme_bw()+scale_fill_grey(start = 0.8, end = 1)+
labs(title="title", x="", y="score",fill="", colour="")+
geom_jitter(aes(colour = clin), alpha=0.9,
position=position_jitter(w=0.1,h=0.1))
As you can see, the data points plotted using geom_jitter do not align with the boxplot. I know that I need to provide aes elements to geom_jitter as well - but I not sure how to do it correctly.
Upvotes: 1
Views: 8860
Reputation: 52687
I don't think you can do this because the positions of the boxplots are being driven by the dodge algorithm as opposed to an explicit aesthetic, though I'd be curious if someone else figures out a way of doing it. Here is a workaround:
p<-ggplot(data, aes(status, as.numeric(score),fill=status))
p+geom_boxplot(outlier.shape = NA)+
theme_bw()+scale_fill_grey(start = 0.8, end = 1)+
labs(title="title", x="", y="score",fill="", colour="")+
geom_jitter(aes(colour = clin), alpha=0.9,
position=position_jitter(w=0.1,h=0.1)) +
facet_wrap(~ culture)
By using the facets for culture
, we can assign an explicit aesthetic to status
, which then allows to line up the geom_jitter
with the geom_boxplot
. Hopefully this is close enough for your purposes.
Upvotes: 3