[R] ggplot2: deterministic position_jitter & geom_line with position_jitter

hadley wickham h.wickham at gmail.com
Sat Sep 12 21:09:30 CEST 2009


On Sat, Sep 12, 2009 at 1:34 PM, Stephan Kolassa <Stephan.Kolassa at gmx.de> wrote:
> Dear guRus,
>
> I am starting to work with the ggplot2 package and have two very dumb
> questions:
>
> 1) deterministic position_jitter - the jittering is stochastic; is there any
> way to get a deterministic jittering? For instance:
>
> example.data <-
> data.frame(group=c("foo","bar","foo","bar","foo","bar"),x=c(1,1,2,2,3,3),y=c(1,1,0,2,1,1))
> set.seed(2009)
> qplot(x,y,data=example.data,shape=group,position=position_jitter(w=0.1,h=0))
>
> For x=1, the foo point is to the left of the bar point, and for x=3 the
> other way around. I would like to have all foo points at seq(1,3)-epsilon
> and all bar points at seq(1,3)+epsilon. Do I need to manually modify
> example.data$x groupwise for this?

Yes.  The plyr package generally makes this sort of manipulation pain free.

> 2) geom_line with position_jitter - when I call multiple geoms with
> position_jitter, each geom gets its own jittering. For example (continuing
> with example.data above):
>
> set.seed(2009)
> qplot(x,y,data=example.data,geom=c("point","line"),shape=group,position=position_jitter(w=0.1,h=0))
>
> The lines do not connect the points - is there any way to have the geom_line
> connect all the foo points on the one hand and all the bar points on the
> other hand?

Again, you'll have to adding the jittering yourself.

> --------------------------------------------------------------
>
> What I've done: searched through HW's book, googled, searched RSeek. For
> point 2) above, I tried using multiple layers and resetting the seed in
> between, to wit:
>
> pp <- ggplot(example.data,aes(x,y,shape=group))
> set.seed(2009)
> pp <- pp+layer(geom="point",position=position_jitter(w=0.1,h=0))
> set.seed(2009)
> pp <- pp+layer(geom="line",position=position_jitter(w=0.1,h=0))
> print(pp)
>
> This doesn't do what I want, either...

The jittering isn't evaluated until render time, so this won't help.
You'll notice if you draw the plot again, you'll get a different
rendering.

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list