Search code examples
rlatticelevelplot

Saving levelplot() to file in R's lattice package


I noticed something really weird happening in my script:

pdf("name.pdf")
levelplot(my_data)
dev.off()

does not work for lattice's levelplot if I want to save the plot to a file. I read the lattice package manual and the levelplot help function but couldn't find anything about this particular problem.


Solution

  • By "script" I am presuming that you are not running this interactively? Anyway, simply calling a grid-based graphics function (such as those in the lattice or ggplot2 packages) does not do any plotting - it just creates the an R object that describes the plot. You need to print this object to get R to draw the plot.

    In interactive use, the object gets auto-printed, but not in a script (and not in a loop for example).

    Try:

    pdf("name.pdf")
    print(levelplot(my_data))
    dev.off()
    

    If that doesn't work you'll need to explain more as I can't see a reason why that would not work if you were running the code exactly as given and there was no problem with my_data.