10

Is it possible to group the jitter in a boxplot like mine so that the data points line up with the factor for each market? Right now it's lining up by market name. I colored them to show which ones should be grouped.

enter image description here

My code

p<-ggplot(droplevels(subset(sData,STORE_TYPE=='Test')),aes(factor(MARKET_NAME),DST_UNITS))
p + 
  geom_boxplot(aes(fill=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM")), outlier.shape=NA) + 
  geom_jitter(aes(color=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM"))),position=position_jitter(width=0))
JohnB
  • 1,743
  • 5
  • 21
  • 40
  • 1
    My guess is that your problem is that the boxplots are being dodged based on the `fill` aesthetic, but `geom_jitter` is unaware of that aesthetic. Probably using either `fill` or `group` with the jitter will be the way to go. But it would be best if you could provide a reproducible example. – joran Jun 03 '14 at 14:09
  • You're missing a `)` in there somewhere. – jbaums Jun 03 '14 at 14:10
  • 3
    Here is [solution](http://stackoverflow.com/questions/10493084/ggplot2-jitter-and-position-dodge-together) (second answer) that works in ggplot2 version 1.0.0 - using new position - jitterdodge – Didzis Elferts Jun 03 '14 at 14:56
  • jbaums, it looks like I was missing a ) somewhere. I posted the working answer below – JohnB Jun 03 '14 at 15:58

1 Answers1

10

Solution provided in comments by Didzis Elferts and link to this question

sData<-droplevels(subset(sData,STORE_TYPE=='Test'))

ggplot(sData,aes(x=factor(MARKET_NAME),y=DST_UNITS,fill=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM")))) +
  geom_boxplot(outlier.shape=NA) + 
  geom_point(position=position_jitterdodge())
Community
  • 1
  • 1
JohnB
  • 1,743
  • 5
  • 21
  • 40