Search code examples
rgraphplotlattice

Autosaving multiple pages of lattice plots


I was wondering if anyone could assist with code below. I have a huge dataset (> 1000 subjects) which I'm trying to visualise individually.

I was fortunate to find a code written by Tony Cookson from R-bloggers which I've modified for my use. The code works ok but the pdfs produced are damaged-essentially they refuse to open. I have a feeling there's bug somewhere but I haven't yet figured out where. Any assistance would be highly appreciated.

library(lattice)

names = LETTERS[1:3] 

for(i in 1:3){

mypath <- file.path("myFilepath", "folder containing 'Plots' subfolder ",
                    "Plots",paste("myplot_", names[i], ".pdf", sep = ""))

pdf(file=mypath)
mytitle = paste("Theoph Plots", names[i])
xyplot(conc ~ Time | Subject, group = Subject, data = Theoph, type = "l",
     layout = c(2, 2), main = mytitle)
dev.off()
}

For the code to be reproducible, you need to replace myFilepath, folder containing 'Plots' subfolder and "Plots" with names of actual folders that can be found on your computer. Please see the original on R-bloggers for more details. I would be very happy to clarify anything that seems ambiguous.

Thanks

Edit:

library(lattice)

names = LETTERS[1:3] 

for(i in 1:3){

mypath <- file.path("myFilepath", "folder containing 'Plots' subfolder ",
                "Plots",paste("myplot_", names[i], ".pdf", sep = ""))

pdf(file=mypath)
mytitle = paste("Theoph Plots", names[i])
print(xyplot(conc ~ Time | Subject, group = Subject, data = Theoph, type =    "l",
 layout = c(2, 2), main = mytitle))
dev.off()
}

I've managed to find a temporary solution (above) using the print function. However, I'm currently getting all 12 Subjects in the same pdf. What I really want is 4 subjects (2 by 2 matrix) on separate pdfs so making 3 pdfs in total. Anyone know how to do this?


Solution

  • If you're looking to plot a subset of Subjects on each page, then you have to subset your data for each iteration and then plot.

    To get 4 Subjects on each page, you can use the following index builder as a basis for subsetting:

    (i - 1) * 4 + 1:4
    

    The trick with the Theoph dataset is that the subject "numbers" are actually ordered factors. So you have to convert the above to a factor, or, as a shortcut, to a character vector.

    for(i in 1:3){
        ## Changed mypath to make it reproducible
        mypath <- file.path(tempdir(), paste("myplot_", names[i], ".pdf", sep = ""))
        pdf(file=mypath)
    
        mytitle = paste("Theoph Plots", names[i])
    
        myIndex <- as.character((i - 1) * 4 + 1:4) # index builder from above
    
        print(xyplot(conc ~ Time | Subject, 
            data = Theoph[Theoph$Subject %in% myIndex, ],
            type = "l", layout = c(2, 2), main = mytitle))
        dev.off()
    }
    

    The order of the subjects is a bit screwy, since that variable is an ordered factor, as mentioned. To keep the ordering, you could subset on the levels of that factor:

    myIndex <- levels(Theoph$Subject)[(i - 1) * 4 + 1:4]
    

    The best way to build your index will depend on your actual data.