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!

3
  • fill doesn't apply to points (generally), but color does. So you have to use color with the points. Also, this might be a good reference (just use fill for the boxplots). Commented Feb 5, 2015 at 21:45
  • You may have a look at different plot symbols here. For pch 21-25 you may specify a fill colour. Commented Feb 5, 2015 at 21:56
  • Thanks for the reference joran. It turned out to be really useful. Commented Feb 6, 2015 at 21:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Hmm, still doesn't work. Interestingly though, now at least some of the points have the correct horizontal alignment.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.