Stephan Kolassa
2009-Sep-12 18:34 UTC
[R] ggplot2: deterministic position_jitter & geom_line with position_jitter
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?
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?
--------------------------------------------------------------
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...
Why I'm doing this: example.data actually contains group means across a
covariate x, and they need to be plotted as "dots plus error bars"
(psychologists' convention), so "use boxplots instead" is a
perfectly
correct reply to my questions which unfortunately does not help me.
Thanks for your time!
Stephan
----------------------------------------------------------------------
sessionInfo():
R version 2.9.2 (2009-08-24)
i386-pc-mingw32
locale:
LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252
attached base packages:
[1] grid grDevices datasets splines graphics stats tcltk
utils methods base
other attached packages:
[1] ggplot2_0.8.3 reshape_0.8.3 plyr_0.1.9 proto_0.3-8
svSocket_0.9-43 svMisc_0.9-48 TinnR_1.0.3 R2HTML_1.59-1
Hmisc_3.7-0
[10] survival_2.35-4
loaded via a namespace (and not attached):
[1] cluster_1.12.0 lattice_0.17-25 tools_2.9.2
hadley wickham
2009-Sep-12 19:09 UTC
[R] ggplot2: deterministic position_jitter & geom_line with position_jitter
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/