6

In R, trying to export choropleth maps with density and frequency histograms as legends; have had success exporting as an image but not as a pdf.

Exporting as an image (.png) works well; the map, including the placement of the histogram within the frame, looks the same in the exported image as it does within the Plot window in R. However, when exporting to a PDF, the histogram changes size and placement and often overlaps with the map (which renders the histogram unreadable). Any ideas as to why the two exported maps look different? And how to export to a PDF so that the exported map looks like the map in R?

Thanks!

2 Answers 2

4

Use the pdf as a device driectly and skip the plot window device. In this way you can also write multiple page pdf's by passing multiple plots to the device. The device is turned off (closed) with dev.off().

# Will write a 10"x10" pdf to disk
pdf("MyPlot.pdf", height=10, width=10)
  plot(runif(100), runif(100))
dev.off()
2
  • When I ran this function, the exported pdf contained what looked like a scatterplot, rather than the choropleth map and histogram. Any idea why this would happen? I've tried several times with no success.
    – user26607
    Commented Feb 15, 2014 at 3:03
  • Are you using the exact code provided above? This was meant to be an example. You would provide your own plot function and not the scatterplot example I provided. Remember, you are sending your plot to a device. Commented Feb 18, 2014 at 2:33
2

You can use a single command to export the current plot to a single (not multi-page) pdf:

dev.copy2pdf(file="MY.pdf", width = 7, height = 5)

The dimensions are in inches; experiment with different sizes. This won't necessarily give you exactly what appears on screen - in particular, smaller export sizes will give strange layouts of legends and other components.

Note there is no need for dev.off()

5
  • Is there a problem with dev.off()? Why would you call a graphic device if you want a pdf? For complex plot (e.g., spatial objects) you would have to wait until the plot renders before sending it to a file using dev.copy. This seems like unnecessary overhead. Whereas pdf sends the plot directly to the file. Using pdf does necessitate a multiple-page pdf but rather facilitates it. Commented Feb 9, 2014 at 1:02
  • I personally find this method more convenient - nothing personal against dev.off()! When producing single diagrams for inserting into a report this is a little easier than going back and wrapping everything in pdf/dev.
    – Simbamangu
    Commented Feb 9, 2014 at 6:52
  • This method worked accurately and quickly for me, as I exported a single-page pdf and it wasn't a small export size. Thanks for the suggestion.
    – user26607
    Commented Feb 10, 2014 at 20:59
  • 1
    What I am hung up on is unnecessarily calling a device. If you have a large amount of data it takes time to render in the window plot device. You then send it to a pdf. You are rendering the plot twice! If you use "pdf" you are sending the plot to the pdf and not rendering to a window device. Some polygon plots take minutes to render in the window whereas it only takes a few seconds to send to the pdf. Commented Feb 11, 2014 at 21:17
  • 1
    Understood - I guess I rarely work with very large, complex plots, so this hasn't been an issue for me. dev.copy2pdf() is a convenience function, certainly, and it does make ad-hoc creation and export of diagrams easy - but definitely not best practice.
    – Simbamangu
    Commented Feb 12, 2014 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.