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

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Aug 21 02:23:12 CEST 2014


Do the jittering yourself before you give the data to ggplot. 
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On August 20, 2014 4:56:04 PM PDT, David Romano <dromano at stanford.edu> wrote:
>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
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list