3

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?

John_dydx
  • 951
  • 1
  • 14
  • 27
  • 1
    You're spot on with the `print` function. If you want one PDF per subject, remove `group = Subject` from your `xyplot` and replace the data argument: `data = Theoph[Theoph$Subject == i, ]`. What do you want in each of the four plots? – BenBarnes Jan 05 '16 at 14:17
  • 1
    @BenBarnes, Thanks for that response. What I want is 4 subjects on each plot/pdf making a total of 3 pdfs. At the moment, its giving me all 12 Subjects on the same pdf but on different pages of the pdf. I've tried your suggestion but the result is not quite what I'm after-cheers! – John_dydx Jan 05 '16 at 14:38

1 Answers1

1

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.

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
  • Thanks so much for taking the time to post. It works like magic! The indexing is really clever, I just need to adapt it to over 100 subjects. Thanks again! – John_dydx Jan 05 '16 at 15:30
  • Would I need to change anything in this code if my subjects were just factors rather than ordered factors? I just keep getting an error report when I tried it on my data-`Error in limits.and.aspect(default.prepanel, prepanel = prepanel, have.xlim = have.xlim, : need at least one panel`. the code works well on the `Theoph` data but doesn't work on mine. – John_dydx Jan 05 '16 at 16:43
  • Are you using the second version of `myIndex` or the first? – BenBarnes Jan 05 '16 at 16:49
  • I've tried both-with and without `as.character`. Doesn't seem to make much of a difference-getting the error report with both. – John_dydx Jan 05 '16 at 16:53
  • Use the second version (`myIndex <- levels(Theoph$Subject)[(i - 1) * 4 + 1:4]`) - the first version assumes the `Subject`s are designated by a sequence of numbers starting from 1. If your subjects are designated by anything else, the first `myIndex` won't work. – BenBarnes Jan 05 '16 at 16:55
  • Thanks so much for your help, I still have no idea why its not working with my data. – John_dydx Jan 05 '16 at 22:28
  • Try to create a minimal, reproducible example of the problem/error you are getting, and then edit it into your original post. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for tips on creating the example. – BenBarnes Jan 05 '16 at 22:43