[R] ggplot2: how to jitter spaghetti plot so slopes are preserved

David Romano dromano at stanford.edu
Thu Aug 21 01:56:04 CEST 2014


Hi,

Suppose I have a the data frame given by:
> dput(toy.df)
structure(list(id = c(1, 2, 1, 2), time = c(1L, 1L, 2L, 2L),
    value = c(1, 2, 2, 3)), .Names = c("id", "time", "value"), row.names = c(NA,
4L), class = "data.frame")

that is:
> toy.df
  id time value
1  1    1     1
2  2    1     2
3  1    2     2
4  2    2     3

I can create a "spaghetti" plot with the command:
> ggplot(toy.df,aes(x=time,y=value,group=id,color=factor(id))) + geom_line()

What I'd like to be able to do is jitter the lines themselves by
translation so that their slopes are preserved, but so far my attempts
to jitter -- within ggplot, as opposed to first jittering toy.df by
hand -- seem to always jitter the two points for a given id
independently, and thus change the slopes.

I'd be grateful for any guidance!

Thanks,
David



More information about the R-help mailing list