jitter error

296 views
Skip to first unread message

J H

unread,
Mar 4, 2014, 2:03:32 PM3/4/14
to ggp...@googlegroups.com
Hi- I noticed that if I use geom_boxplot, I see in addition, single data points outside the boxes, which is OK.  however, when I include geom_jitter, the data points within the boxes now become visible, which is fine, however, the jitter adds duplicate data only for those points outside of the boxes.  How can I get around this?  Thanks.

Dennis Murphy

unread,
Mar 4, 2014, 3:00:11 PM3/4/14
to J H, ggplot2
The usual remedy is to suppress the points outside the box inside the
boxplot; try

... + geom_boxplot(..., outlier.colour = NA)

where ... is a placeholder for other pertinent code. You have to use
the Commonwealth spelling, since outlier.color = NA has no effect.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Richard Zijdeman

unread,
Mar 4, 2014, 4:54:19 PM3/4/14
to ggp...@googlegroups.com, J H
Hi, this basically is what Murphy's says: but here are some examples of what might go wrong and how to fix it when combining box plots and a jittered geom_point plot:

# load library
library(ggplot2)

# draw a boxplot with '+' symbol for outliers
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p1 <- p + geom_boxplot(outlier.shape=3)
p1

# overlay with points (with horizontal jitter)
p1 + geom_point(position = position_jitter(width=0.2))
# this indeed provides duplicate outlier points

# fix duplicates, by not showing outliers in initial boxplot
p2 <- p + geom_boxplot(outlier.colour=NA)
p2 + geom_point(position = position_jitter(width=0.2))

# overlay boxplots on geom_points: points or obscured
p3 <- p + geom_point(position = position_jitter(width=0.2))
p3 +  geom_boxplot(outlier.colour=NA)

# remove fill of box plots to uncover obscured points:
p3 + geom_boxplot(outlier.colour=NA, fill = NA)

Best,

Richard
Reply all
Reply to author
Forward
0 new messages