David Romano
2014-Aug-20 23:56 UTC
[R] ggplot2: how to jitter spaghetti plot so slopes are preserved
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.dfid 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
Jeff Newmiller
2014-Aug-21 00:23 UTC
[R] ggplot2: how to jitter spaghetti plot so slopes are preserved
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.