Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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()

share|improve this answer
    
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. –  Jeffrey Evans Feb 9 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 Feb 9 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 Feb 10 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. –  Jeffrey Evans Feb 11 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 Feb 12 at 13:00

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()
share|improve this answer
    
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 Feb 15 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. –  Jeffrey Evans Feb 18 at 2:33
    
Ah, got it; thanks. –  user26607 Feb 27 at 21:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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