Home > OS >  Is there a way to get position_jitterdodge in ggplot2 to sort by two categorical variables when usin
Is there a way to get position_jitterdodge in ggplot2 to sort by two categorical variables when usin

Time:05-19

I'm trying to make a jittered boxplot from a data set with one continuous variable and two categorical variables. I've separated the data into two data frames because I don't want the means of data frame 2 to be included in the boxplot and because I want to show them in a different colour. I used position_jitterdodge() so that the points would sit on top of the box plot that corresponded to their variable "b" category, but it doesn't seem to be working. For instance, all the red dots should be centred on the purple boxplots but they seem to be spread across the whole graph. I don't have enough reputation to include an image, but I have posted the question on the R community page with an image included if it would help enter image description here

EDIT

Add points to grouped boxplots without jittering. Note that several points are overlapping here.

ggplot(data = df, aes(x=c, y=a, fill=b, color=grp))   
  geom_boxplot(data = df[df$grp == 1,], outlier.shape = NA)  
  geom_point(aes(group=b), position = position_dodge(0.75))  
  scale_fill_manual("b", values=c("#FFD54F","#B2EBF2","#7B1FA2"))  
  scale_color_manual(guide = "none", values = c("1" = "black", "2" = "red"))

enter image description here

  • Related