I am trying to make a grouped barplot with scatterplots colored for individual data points overlaid on the bars (I'm aware of the advantages of boxplots, but they are not standard in my field). As I am new to R, I am mostly working by cutting and pasting bits of code in a semi-logical trial and error process, and here is the closest I have been able to come. It using the sample dataset "Males".
p <- ggplot(Males, aes(factor=year, fill=year, y=wage, x=ethn))
p + stat_summary(fun.y = "mean", geom = "bar", position="dodge") +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", mult=1,
color="yellow", position="dodge") +
geom_jitter(aes(size=.05, col=industry),position=position_dodge(width = 0.8))
ymax not defined: adjusting position using y instead

Unfortunately I do not have enough reputation points to post an image of the output, but basically the dots are all the same color as the column they overlay, instead of having a mix of different industries (colors) over each column.
My understanding is that the ymax error I got has nothing to do with the problem I'm having. Anyway, I'd be glad of any suggestions people can offer.
The problem you are having, I think, is that some geometries, such as geom_point and geom_jitter do not use or allow the factor aesthetic for grouping along the x-axis.
Thus when you plot other geoms on top of a chart where factor is not ignored, such as geom_bar, the factor setting is ignored for some layers but not for bar, and you don't get year-resolved columns of points.
To solve the problem, I would try using facet_grid or facet_wrap to indirectly get the x-axis groupings that you want.
For example:
require(Ecdat)
data(Males)
quartz(height=6, width=12)
ggplot(Males, aes(x=year, y=wage)) +
facet_grid(.~ethn) +
stat_summary(mapping=aes(fill=year), fun.y=mean, geom='bar') +
stat_summary(fun.data = mean_sdl, geom='errorbar', color='yellow') +
geom_jitter(aes(color=industry),
position=position_jitter(width=0.2), alpha=0.8)
quartz.save('SO_29610340.png')

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With