Dear latticists,
I would like to spread a lattice conditioned plot over multiple pages, keeping the same layout as if I had only one page as shown in the code below. My workaround is to divide the dataframe into subset that fit on one page, but the code is ugly. Is there a build-in way to achieve this? Dieter library(lattice) nsubj = 13 # This number is variable dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:nsubj]) dt$val = rnorm(nrow(dt)) #pdf(file="multpageOk.pdf") # How it should look: xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3), subset=as.integer(subj) <= 10) #dev.off() # What to do if it stretches over multiple pages, but I want the same # layout as above? pdf(file="multpage.pdf") xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3)) dev.off() |
Hi Dieter:
If I read your intention correctly, you need a third element in layout = . Here's a little example: df <- data.frame(month = rep(month.abb, each = 20), time = rep(1:20, 12), y = rnorm(240)) xyplot(y ~ time | month, data = df, layout = c(2, 2, 3)) This produces 3 pages of 2 x 2 plots. Hope this is what you had in mind.. Dennis On Sun, Dec 19, 2010 at 8:23 AM, Dieter Menne <[hidden email]>wrote: > > Dear latticists, > > I would like to spread a lattice conditioned plot over multiple pages, > keeping the same layout as if I had only one page as shown in the code > below. > > My workaround is to divide the dataframe into subset that fit on one page, > but the code is ugly. > > Is there a build-in way to achieve this? > > Dieter > > > > library(lattice) > nsubj = 13 # This number is variable > dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:nsubj]) > dt$val = rnorm(nrow(dt)) > > #pdf(file="multpageOk.pdf") > # How it should look: > xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3), > subset=as.integer(subj) <= 10) > #dev.off() > > # What to do if it stretches over multiple pages, but I want the same > # layout as above? > pdf(file="multpage.pdf") > xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3)) > dev.off() > > -- > View this message in context: > http://r.789695.n4.nabble.com/Layout-of-mulitpage-conditioned-lattice-plots-tp3094581p3094581.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
In reply to this post by Dieter Menne
On Dec 19, 2010, at 11:23 AM, Dieter Menne wrote: > > Dear latticists, > > I would like to spread a lattice conditioned plot over multiple pages, > keeping the same layout as if I had only one page as shown in the code > below. > > My workaround is to divide the dataframe into subset that fit on one > page, > but the code is ugly. > > Is there a build-in way to achieve this? > > Dieter > > > > library(lattice) > nsubj = 13 # This number is variable > dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:nsubj]) > dt$val = rnorm(nrow(dt)) > > #pdf(file="multpageOk.pdf") > # How it should look: > xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3), > subset=as.integer(subj) <= 10) > #dev.off() > > # What to do if it stretches over multiple pages, but I want the same > # layout as above? > pdf(file="multpage.pdf") > xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3)) > dev.off() What's not working? I see two pages output with "the same layout". The difference is that in the second case your numbers of groups (subj x comp) is not an even multiple of your layout numbers, so the 13 subj levels push 3 of the A's onto the new row of panels and so one.....and then the second page is partially filled with the 9 remaining "C"'s. I suppose the fact that I have a default time-stamp for my lattice output could have some sort of side-effect. In my .Rprofile is this line: lattice.options(default.args = list(page = function(n) { panel.text(lab = sprintf("%s", date()), x = 0.01, y = 0.01, adj = 0, srt=90) })) -- David Winsemius, MD West Hartford, CT > sessionInfo() R version 2.12.0 (2010-10-15) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] grid splines stats graphics grDevices utils datasets methods base other attached packages: [1] survey_3.22-4 lubridate_0.2.3 circular_0.4 boot_1.2-43 ggplot2_0.8.8 proto_0.3-8 [7] reshape_0.8.3 plyr_1.2.1 gridExtra_0.7 gdata_2.8.1 Hmisc_3.8-3 survival_2.36-1 [13] sos_1.3-0 brew_1.0-4 lattice_0.19-13 loaded via a namespace (and not attached): [1] cluster_1.13.2 digest_0.4.2 gtools_2.6.2 stringr_0.4 tools_2.12.0 ______________________________________________ [hidden email] 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. |
In reply to this post by djmuseR
Dennis,
Thank you; this helps me, too! Tom On 12/19/10 11:45 AM, Dennis Murphy wrote: > Hi Dieter: > > If I read your intention correctly, you need a third element in layout = . > Here's a little example: > > df<- data.frame(month = rep(month.abb, each = 20), > time = rep(1:20, 12), > y = rnorm(240)) > xyplot(y ~ time | month, data = df, layout = c(2, 2, 3)) > > This produces 3 pages of 2 x 2 plots. > > Hope this is what you had in mind.. > > Dennis > > On Sun, Dec 19, 2010 at 8:23 AM, Dieter Menne > <[hidden email]>wrote: > >> Dear latticists, >> >> I would like to spread a lattice conditioned plot over multiple pages, >> keeping the same layout as if I had only one page as shown in the code >> below. >> >> My workaround is to divide the dataframe into subset that fit on one page, >> but the code is ugly. >> >> Is there a build-in way to achieve this? >> >> Dieter >> >> >> >> library(lattice) >> nsubj = 13 # This number is variable >> dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:nsubj]) >> dt$val = rnorm(nrow(dt)) >> >> #pdf(file="multpageOk.pdf") >> # How it should look: >> xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3), >> subset=as.integer(subj)<= 10) >> #dev.off() >> >> # What to do if it stretches over multiple pages, but I want the same >> # layout as above? >> pdf(file="multpage.pdf") >> xyplot(val~time|subj+comp, data=dt,type="l",layout=c(10,3)) >> dev.off() >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/Layout-of-mulitpage-conditioned-lattice-plots-tp3094581p3094581.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> [hidden email] 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. >> > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. > -- Thomas E Adams National Weather Service Ohio River Forecast Center 1901 South State Route 134 Wilmington, OH 45177 EMAIL: [hidden email] VOICE: 937-383-0528 FAX: 937-383-0033 ______________________________________________ [hidden email] 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. |
In reply to this post by djmuseR
Not really. Note that my example is conditioned on 2 variables, and the layout on the first page is the correct one, and by design I use nsubj=13 to show what happens if a page is not filled. Dieter |
In reply to this post by David Winsemius
As you noted, the last page is the problem, and nsubj=13 was chosen by design. I am currently looking if ggplot2 can handle this case without large coding overhead. Dieter |
Here is an example with ggplot2, which can also be used in a similar way with lattice. Again, the last page is the problem: the arrangement is correct here, but the last page (with 1 instead of 5 plots) has a different panel size which makes a comparison difficult.
And, since I have much more points per panel: ggplot2 is slow compared to lattice. Dieter library(ggplot2) nsubj = 11 dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:nsubj]) dt$val = rnorm(nrow(dt)) nPerPage = 5 for (i in seq(1,nsubj,by=nPerPage)) { subjs = i:max(i+nPerPage-1,nPerPage) print(subjs) p = qplot(time,val,data=subset(dt,as.integer(subj) %in% subjs )) + geom_line()+ facet_grid(comp ~ subj)+opts(aspect.ratio=1) print(p) } |
In reply to this post by Dieter Menne
On Dec 19, 2010, at 1:53 PM, Dieter Menne wrote: > > > David Winsemius wrote: >> >> >> >> What's not working? I see two pages output with "the same layout". >> The >> difference is that in the second case your numbers of groups (subj x >> comp) is not an even multiple of your layout numbers, so the 13 subj >> levels push 3 of the A's onto the new row of panels and so >> one.....and >> then the second page is partially filled with the 9 remaining "C"'s. >> >> > > As you noted, the last page is the problem, and nsubj=13 was chosen by > design. I am currently looking if ggplot2 can handle this case > without large > coding overhead. I'm obviously not seeing what you think would be an acceptable solution. Can you explain in words what you want, rather than what you don't want in failed code? Here's my latest guess at what you may want: pdf(file="multpage.pdf") xyplot(val~time|subj + comp, data=dt,type="l", layout=c(3,5, 3), skip=rep(c(rep(FALSE,13), TRUE, TRUE), 3) ) dev.off() -- David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
Not really, but "skip" was the right idea. I added another idea of Deepayan from a cited thread, first to plot all, then to update indexed parts with a computed skip. The code has become a bit lengthy because I added a more flexible orphan-avoiding scheme. Dieter library(lattice) # Distribute panels on page, so that each panel has the same size, # even on last page # Use adjustCol to adjust colPerPage to avoid orphans on the last page # # --------------------- adjustedColPerPage ------------------------------------- adjustedColPerPage = function(colPerPage, ncols){ # Allow for 20% or plus/minus 2 searchRange = max(2L,as.integer(colPerPage*0.2)) colsPerPage = (colPerPage-searchRange):(colPerPage+searchRange) nColLast = ncols %% colsPerPage nPages = (ncols %/% colsPerPage)+ as.integer(nColLast!=0) # Prefer solution with equal number on a page matchPage = which(nColLast==0) if (length(matchPage) >0) { colsPerPage[matchPage[which.min(abs(matchPage-searchRange))]] } else { colsPerPage[which.max(nColLast)] # not perfect } } # --------------------- xyPaged ---------------------------------------------- xyPaged = function(x, adjustCol = FALSE, colPerPage = 5,main=NULL) { nrows = nlevels(x$comp) # This is not very general ncols = nlevels(x$subj) # if (adjustCol) # try to get an alternative layout that fits the pages better { colPerPage = adjustedColPerPage(colPerPage,ncols) main = paste(main," usedCol= ",colPerPage) } p = xyplot(val~time|subj+comp, data=x,type="l", layout = c(colPerPage,nrows),main=main) # http://r-project.markmail.org/thread/rcztoawll5kduw4x page = 1 for (fromCol in seq(1,ncols,by=colPerPage)){ toCol = min(fromCol+colPerPage-1,ncols) showCol = toCol %% colPerPage skip = rep(FALSE,colPerPage) if (showCol != 0) skip[(showCol+1):colPerPage] = TRUE print(update(p[fromCol:toCol],skip=skip,sub=page)) page = page +1 } } # Test testFrame = expand.grid(adjustCol=c(FALSE,TRUE), nsubj=c(5,11,13),colPerPage=c(5,9,14) ) pdf(file="multpage.pdf") for (i in 1:nrow(testFrame)) { test = testFrame[i,] dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:test$nsubj]) dt$val = rnorm(nrow(dt)) with (test, xyPaged(dt,adjustCol, colPerPage, main=paste("nsubj=",test$nsubj, " requestedCol= ",colPerPage))) } dev.off() |
Powered by Nabble | Edit this page |