Dear all, I am new to lattice, so would appreciate anyone's help on the questions below. I am using xyplot to plot some trend in my dataset. Using the example dataset attached, I am trying to plot variable "y" over variable "time" for each subject "id": dat<-read.table("dat.txt",sep='\t',header=T,row.names=NULL) xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", "l"), ?xlab = "Time", ylab = "Y") It appears that it just worked fine. But if I sort the "dat" first, the plot will look somewhat different! dat<-dat[order(dat$id, dat$time),]xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", "l"), ?xlab = "Time", ylab = "Y") Why is that? Do you need to sort the data first before using xyplot? Why xyplot can not understand the dataset unless it is sorted first? Thanks, John -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: dat.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170501/432a8fd6/attachment.txt>
Yes. type = "l" connects the points in the order given in the data, so if the x's are not already ordered, the plots will be different after ordering the x's. e.g.> x <- c(3,1,2,4,6,5) > y <- 11:16 > xyplot(y~x. type = "l")As for why ... that's just the way it was designed. You can always order the data first, if you don't want this default. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Apr 30, 2017 at 6:07 PM, array chip via R-help <r-help at r-project.org> wrote:> Dear all, I am new to lattice, so would appreciate anyone's help on the questions below. I am using xyplot to plot some trend in my dataset. Using the example dataset attached, I am trying to plot variable "y" over variable "time" for each subject "id": > dat<-read.table("dat.txt",sep='\t',header=T,row.names=NULL) > xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") > > It appears that it just worked fine. But if I sort the "dat" first, the plot will look somewhat different! > dat<-dat[order(dat$id, dat$time),]xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") > Why is that? Do you need to sort the data first before using xyplot? Why xyplot can not understand the dataset unless it is sorted first? > Thanks, > John > ______________________________________________ > 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.
In addition to Berts comments Once you change the order you change the non factored id' ordering. If you make it a factor it may be easier to see what is going on I think I have copied correctly - see the differences # original data xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") # order dat2<-dat[order(dat$id, dat$time),] xyplot(y ~ time, data=dat2, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") # make ID a factor dat3 <- dat1 xyplot(y ~ time, data=dat3, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") # ordered + ID a factor dat4 <- dat3 dat4<-dat[order(dat4$id, dat4$time),] xyplot(y ~ time, data=dat4, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mackay at northnet.com.au -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of array chip via R-help Sent: Monday, 1 May 2017 11:07 To: r-help at r-project.org Subject: [R] Lattice xyplot Dear all, I am new to lattice, so would appreciate anyone's help on the questions below. I am using xyplot to plot some trend in my dataset. Using the example dataset attached, I am trying to plot variable "y" over variable "time" for each subject "id": dat<-read.table("dat.txt",sep='\t',header=T,row.names=NULL) xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") It appears that it just worked fine. But if I sort the "dat" first, the plot will look somewhat different! dat<-dat[order(dat$id, dat$time),]xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab = "Y") Why is that? Do you need to sort the data first before using xyplot? Why xyplot can not understand the dataset unless it is sorted first? Thanks, John
It is not a question of whether lattice "understands" the unsorted data... imagine trying to plot 4 points to form a square instead of a trend line... you would NOT want lattice to sort those points for you. That lattice leaves your data alone gives you more flexibility, even while it adds work for certain applications. -- Sent from my phone. Please excuse my brevity. On May 1, 2017 7:34:09 AM PDT, Bert Gunter <bgunter.4567 at gmail.com> wrote:>Yes. type = "l" connects the points in the order given in the data, so >if the x's are not already ordered, the plots will be different after >ordering the x's. > >e.g. > >> x <- c(3,1,2,4,6,5) >> y <- 11:16 >> xyplot(y~x. type = "l") > > >As for why ... that's just the way it was designed. You can always >order the data first, if you don't want this default. > >Cheers, >Bert > >Bert Gunter > >"The trouble with having an open mind is that people keep coming along >and sticking things into it." >-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > >On Sun, Apr 30, 2017 at 6:07 PM, array chip via R-help ><r-help at r-project.org> wrote: >> Dear all, I am new to lattice, so would appreciate anyone's help on >the questions below. I am using xyplot to plot some trend in my >dataset. Using the example dataset attached, I am trying to plot >variable "y" over variable "time" for each subject "id": >> dat<-read.table("dat.txt",sep='\t',header=T,row.names=NULL) >> xyplot(y ~ time, data=dat, groups=id, aspect = "fill", type = c("p", >"l"), xlab = "Time", ylab = "Y") >> >> It appears that it just worked fine. But if I sort the "dat" first, >the plot will look somewhat different! >> dat<-dat[order(dat$id, dat$time),]xyplot(y ~ time, data=dat, >groups=id, aspect = "fill", type = c("p", "l"), xlab = "Time", ylab >"Y") >> Why is that? Do you need to sort the data first before using xyplot? >Why xyplot can not understand the dataset unless it is sorted first? >> Thanks, >> John >> ______________________________________________ >> 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. > >______________________________________________ >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.