match jitter from points and error bars

3,616 views
Skip to first unread message

mrwawa

unread,
Jan 24, 2012, 1:34:17 PM1/24/12
to ggplot2
Can anyone recommend how to match the jitter from points and error
bars? I have a data set with categories on the x axis and continuous
variables on the Y axis. Estimates are plotted as points with error
bars. To avoid overlap, I want to jitter the points, but can't get the
jittered points and error bars to match up.

Any help would be appreciated.

Wade

Winston Chang

unread,
Jan 25, 2012, 6:29:42 PM1/25/12
to mrwawa, ggplot2
Instead of using separate points and error bars, check out geom_pointrange -- it's a single geom with a point and lines, so jittering should move both together.

Another option is to jitter the data yourself, by using runif() to add small random values to the x (or y) coordinate.

-Winston


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

mrwawa

unread,
Jan 26, 2012, 9:45:33 AM1/26/12
to ggplot2
That was exactly what I was looking for. Thanks.

On Jan 25, 5:29 pm, Winston Chang <winstoncha...@gmail.com> wrote:
> Instead of using separate points and error bars, check out geom_pointrange
> -- it's a single geom with a point and lines, so jittering should move both
> together.http://had.co.nz/ggplot2/geom_pointrange.html

Scott Jackson

unread,
Jan 25, 2012, 12:57:57 PM1/25/12
to ggplot2
could you just jitter your x?

ggplot(your.data, aes(jitter(x), y)) + geom_point() +
geom_errorbar(aes(ymin = your.ymin.var, ymax = your.ymax.var))

-scott

Jean-Olivier Irisson

unread,
Jan 26, 2012, 3:28:20 AM1/26/12
to Winston Chang, mrwawa, ggplot2
On 2012-Jan-26, at 00:29 , Winston Chang wrote:
>
> Another option is to jitter the data yourself, by using runif() to add small random values to the x (or y) coordinate.

Or use the jitter() function directly.

But are you sure jittering is the best solution for you? If you jitter points and bars and want lines connecting the points in the different levels of the factor, the graph will quickly become unreadable. Is it something like this:
http://dl.dropbox.com/u/1047321/ggplot/fig-description_by_scale.pdf
that you are trying to achieve? If yes, I would advise you to use a fixed offset per factor level (as I did: green always on the left, red always on the right) rather than jittering. I tried jittering and it was messy. Or best, if you can afford the extra space, use facets, it will be clearer.

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Reply all
Reply to author
Forward
0 new messages