

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 buildin 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~timesubj+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~timesubj+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 buildin 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~timesubj+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~timesubj+comp, data=dt,type="l",layout=c(10,3))
> dev.off()
>
> 
> View this message in context:
> http://r.789695.n4.nabble.com/Layoutofmulitpageconditionedlatticeplotstp3094581p3094581.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 buildin 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~timesubj+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~timesubj+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 timestamp for my lattice
output could have some sort of sideeffect. 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 (20101015)
Platform: x86_64appledarwin9.8.0/x86_64 (64bit)
locale:
[1] en_US.UTF8/en_US.UTF8/C/C/en_US.UTF8/en_US.UTF8
attached base packages:
[1] grid splines stats graphics grDevices utils
datasets methods base
other attached packages:
[1] survey_3.224 lubridate_0.2.3 circular_0.4 boot_1.243
ggplot2_0.8.8 proto_0.38
[7] reshape_0.8.3 plyr_1.2.1 gridExtra_0.7 gdata_2.8.1
Hmisc_3.83 survival_2.361
[13] sos_1.30 brew_1.04 lattice_0.1913
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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 buildin 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~timesubj+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~timesubj+comp, data=dt,type="l",layout=c(10,3))
>> dev.off()
>>
>> 
>> View this message in context:
>> http://r.789695.n4.nabble.com/Layoutofmulitpageconditionedlatticeplotstp3094581p3094581.html>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide
>> http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>

Thomas E Adams
National Weather Service
Ohio River Forecast Center
1901 South State Route 134
Wilmington, OH 45177
EMAIL: [hidden email]
VOICE: 9373830528
FAX: 9373830033
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


djmuseR wrote
If I read your intention correctly, you need a third element in layout = .
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.
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


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.
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+nPerPage1,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)
}


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~timesubj + 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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


David Winsemius wrote
Here's my latest guess at what you may want:
pdf(file="multpage.pdf")
xyplot(val~timesubj + comp, data=dt,type="l",
layout=c(3,5, 3),
skip=rep(c(rep(FALSE,13), TRUE, TRUE), 3) )
dev.off()
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 orphanavoiding 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 = (colPerPagesearchRange):(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(matchPagesearchRange))]]
} 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~timesubj+comp, data=x,type="l",
layout = c(colPerPage,nrows),main=main)
# http://rproject.markmail.org/thread/rcztoawll5kduw4x page = 1
for (fromCol in seq(1,ncols,by=colPerPage)){
toCol = min(fromCol+colPerPage1,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()

