Hi, Given a certain data.frame, the lattice xyplot function will plot the data as.is and join the data point in the order of the data frame. It is my (probably flawed) understanding that, using the same data frame, ggplot orders the data by increasing order of the x-axis variable. Can one control this behavior? Thanks Sebastien Code example library(lattice) library(ggplot2) data <- data.frame(x=rep(1:4,each=25), y=rep(1:25,times=4), g=rep(1:4,each=25)) data$x <- data$x + 0.005*(data$y)^2-0.1*data$y+1 col <- 3:7 xyplot(y~x,data=data,groups=g,type='l',col=col) ggplot(data, aes(x,y,group=g)) + geom_point(colour=col[data$g]) + geom_line(colour=col[data$g])
The ggplot function has no display behaviour. You have to couple it with a geom function to define how the data will be displayed. Read ?geom_line and ?geom_poly. --------------------------------------------------------------------------- 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 October 23, 2015 3:46:02 AM GMT+02:00, sbihorel <Sebastien.Bihorel at cognigencorp.com> wrote:>Hi, > >Given a certain data.frame, the lattice xyplot function will plot the >data as.is and join the data point in the order of the data frame. It >is >my (probably flawed) understanding that, using the same data frame, >ggplot orders the data by increasing order of the x-axis variable. Can >one control this behavior? > >Thanks > >Sebastien > >Code example > >library(lattice) >library(ggplot2) > > >data <- data.frame(x=rep(1:4,each=25), > y=rep(1:25,times=4), > g=rep(1:4,each=25)) >data$x <- data$x + 0.005*(data$y)^2-0.1*data$y+1 > >col <- 3:7 > >xyplot(y~x,data=data,groups=g,type='l',col=col) > >ggplot(data, aes(x,y,group=g)) + geom_point(colour=col[data$g]) + > geom_line(colour=col[data$g]) > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
Hi, are you trying to do the same thing with ggplot as with lattice? In this case, your solution looks probably like this: ggplot(data, aes(x, y, group = g)) + geom_path(aes(colour = g)) If you haven?t done so yet, I strongly recommend reading through the ggplot2 documentation: http://docs.ggplot2.org/current/index.html <http://docs.ggplot2.org/current/index.html> It?s a bit of a read, but you?ll need this knowledge to successfully operate ggplot.> Am 23.10.2015 um 03:46 schrieb sbihorel <Sebastien.Bihorel at cognigencorp.com>: > > Hi, > > Given a certain data.frame, the lattice xyplot function will plot the data as.is and join the data point in the order of the data frame. It is my (probably flawed) understanding that, using the same data frame, ggplot orders the data by increasing order of the x-axis variable. Can one control this behavior? > > Thanks > > Sebastien > > Code example > > library(lattice) > library(ggplot2) > > > data <- data.frame(x=rep(1:4,each=25), > y=rep(1:25,times=4), > g=rep(1:4,each=25)) > data$x <- data$x + 0.005*(data$y)^2-0.1*data$y+1 > > col <- 3:7 > > xyplot(y~x,data=data,groups=g,type='l',col=col) > > ggplot(data, aes(x,y,group=g)) + geom_point(colour=col[data$g]) + > geom_line(colour=col[data$g]) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.[[alternative HTML version deleted]]
You have two problems: * geom_line() always draws from right-to-left * you're defining colour outside of the plot in a very non-ggplot2 way. Here's how I'd do it: library(ggplot2) data <- data.frame( x = rep(1:4, each = 25), y = rep(1:25, times = 4), g = rep(1:4, each = 25) ) data$x <- data$x + 0.005 * data$y ^ 2 - 0.1 * data$y + 1 ggplot(data, aes(x, y, colour = factor(g))) + geom_point() + geom_path() Alsonotethatcodeismucheasiertoreadifyouusespaces;) Hadley On Thu, Oct 22, 2015 at 8:46 PM, sbihorel <Sebastien.Bihorel at cognigencorp.com> wrote:> Hi, > > Given a certain data.frame, the lattice xyplot function will plot the data > as.is and join the data point in the order of the data frame. It is my > (probably flawed) understanding that, using the same data frame, ggplot > orders the data by increasing order of the x-axis variable. Can one control > this behavior? > > Thanks > > Sebastien > > Code example > > library(lattice) > library(ggplot2) > > > data <- data.frame(x=rep(1:4,each=25), > y=rep(1:25,times=4), > g=rep(1:4,each=25)) > data$x <- data$x + 0.005*(data$y)^2-0.1*data$y+1 > > col <- 3:7 > > xyplot(y~x,data=data,groups=g,type='l',col=col) > > ggplot(data, aes(x,y,group=g)) + geom_point(colour=col[data$g]) + > geom_line(colour=col[data$g]) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- http://had.co.nz/
Hi, I want to thank Jeff, Dennis, c06n, and Hadley for their replies and explanations. As you probably guessed, I am fairly new to ggplot am trying to loose my lattice reflex while transitioning to ggplot. Thank for the link to the ggplot external documentation. Sebastien On 10/23/2015 7:15 AM, Hadley Wickham wrote:> You have two problems: > > * geom_line() always draws from right-to-left > * you're defining colour outside of the plot in a very non-ggplot2 way. > > Here's how I'd do it: > > library(ggplot2) > data <- data.frame( > x = rep(1:4, each = 25), > y = rep(1:25, times = 4), > g = rep(1:4, each = 25) > ) > data$x <- data$x + 0.005 * data$y ^ 2 - 0.1 * data$y + 1 > > ggplot(data, aes(x, y, colour = factor(g))) + > geom_point() + > geom_path() > > > Alsonotethatcodeismucheasiertoreadifyouusespaces;) > > Hadley > > On Thu, Oct 22, 2015 at 8:46 PM, sbihorel > <Sebastien.Bihorel at cognigencorp.com> wrote: >> Hi, >> >> Given a certain data.frame, the lattice xyplot function will plot the data >> as.is and join the data point in the order of the data frame. It is my >> (probably flawed) understanding that, using the same data frame, ggplot >> orders the data by increasing order of the x-axis variable. Can one control >> this behavior? >> >> Thanks >> >> Sebastien >> >> Code example >> >> library(lattice) >> library(ggplot2) >> >> >> data <- data.frame(x=rep(1:4,each=25), >> y=rep(1:25,times=4), >> g=rep(1:4,each=25)) >> data$x <- data$x + 0.005*(data$y)^2-0.1*data$y+1 >> >> col <- 3:7 >> >> xyplot(y~x,data=data,groups=g,type='l',col=col) >> >> ggplot(data, aes(x,y,group=g)) + geom_point(colour=col[data$g]) + >> geom_line(colour=col[data$g]) >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > >-- Sebastien Bihorel Cognigen Corporation (t) +1 716 633 3463 ext 323 Cognigen Corporation, a wholly owned subsidiary of Simulations Plus, Inc.