2

I'm having trouble plotting a geom_point() layer on top of a geom_boxplot() layer in ggplot2, and have done some research and there don't seem to be any reported issues precisely of this nature. There are 3 factors in my data set: name, genotype, and region, and my response variable is volume. I have working code to produce a plot with both layers. The problem is that the points ignore the fill factor for geom_point(), but not for geom_boxplot(). The result is that the points are all plotted in the middle of a set of boxplots for each value of name. Here is my code for constructing the plot.

meansPlot = ggplot(data=meansData,aes(x=factor(name), y=volume, fill=factor(genotype)))
meansPlot = meansPlot + 
geom_boxplot() +
geom_point() +
facet_wrap( ~ region, scales='free')

My apologies for not creating a reproducible data set -- I am not really well versed in simulating data quite yet. If there isn't an easy answer (which, I expect there is, and I'm probably just missing something), I will add simulated data to help answer the question.

Thanks!

user26665
  • 325
  • 3
  • 14
  • `fill` doesn't apply to points (generally), but `color` does. So you have to use `color` with the points. Also, [this](http://stackoverflow.com/q/10493084/324364) might be a good reference (just use `fill` for the boxplots). – joran Feb 05 '15 at 21:45
  • You may have a look at different plot symbols [**here**](http://www.cookbook-r.com/Graphs/Shapes_and_line_types/). For `pch` 21-25 you may specify a fill colour. – Henrik Feb 05 '15 at 21:56
  • Thanks for the reference joran. It turned out to be really useful. – user26665 Feb 06 '15 at 21:06

2 Answers2

2

I ended up mostly solving it. This code staggers geom_point() to be inline with geom_boxplot().

meansPlot = ggplot(data=meansData, aes(x=name, y=volume, fill=genotype, color=genotype))  
meansPlot = meansPlot +
geom_point(position=position_jitterdodge(dodge.width=0.9)) +
geom_boxplot(fill="white", position=position_dodge(width=0.9), alpha=0.5) +
facet_wrap( ~ region, scales='free')

Thanks everybody for your efforts.

user26665
  • 325
  • 3
  • 14
1

geom_point() should use the color attribute, not the fill attribute (unless you are using unusual shapes). See if this works for you:

meansPlot = ggplot(data=meansData,aes(x=factor(name), y=volume, fill=factor(genotype)), color = factor(genotype))
meansPlot = meansPlot + 
geom_boxplot() +
geom_point() +
facet_wrap( ~ region, scales='free')
Curt F.
  • 4,690
  • 2
  • 22
  • 39
  • Hmm, still doesn't work. Interestingly though, now at least some of the points have the correct horizontal alignment. – user26665 Feb 06 '15 at 20:03
  • Also, whenever the plot is made I get the following warning: `Warning in levels<-(*tmp*, value = if (nl == nL) as.character(labels) else paste0(labels, : duplicated levels in factors are deprecated` – user26665 Feb 06 '15 at 20:21