R-群抖动在因子箱图中?

时间:2014-06-03 14:04:00

标签: r ggplot2

是否可以将抖动分组到像我这样的箱线图中,以便数据点与每个市场的因子对齐?现在它正按市场名称排队。我给它们上色以显示哪些应该分组。

enter image description here

我的代码

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

1 个答案:

答案 0 :(得分:4)

Didzis Elferts的评论中提供的解决方案以及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())