12

I was making my plots using

dev.new(width=5.8, height=3) 
par(mfrow=c(1,3),mar=c(1,1,2,1),oma=c(4,1,2,0),mgp=c(3, 0.5, 0)) 
plot(...)

and coping and pasting them into Microsoft Word. They look really good in Word (I tried different widths until I found one that worked well) but when I printed them they looked terrible. After some web searching I found the resolution for printing should be at least 300ppi. So after fiddling with widths and heights for an eternity I came out with code that makes the plots look the same size but with a better resolution:

png(file="mag_feb.png",width=1800,height=950,res=300)

They now look good when printed, but they don't look sharp at all in Word (on screen). Could it be a problem with size? Isn't there a way to make graphs that look good printed and on screen? I already spent hours with this and can't think of anything else to try, so any help will be very much appreciated!

Thanks!

sbg
  • 1,772
  • 8
  • 27
  • 45
  • 4
    Not sure about R specifically, but my preferred format for exporting graphs would be as vector, not raster images. PDF and EPS are usually good formats for this, as would SVG be if it supports it – Flexo Nov 17 '11 at 12:17
  • 1
    You could export your Word file to PDF for onscreen reading. Does this stil lead to ugly images in the document? – Paul Hiemstra Nov 17 '11 at 13:30
  • yes, they look the same on word and on a PDF – sbg Nov 17 '11 at 15:43
  • @awoodland if I save the graphs in a PDF how do I manage to import them into Word without converting them into bitmaps? – sbg Nov 18 '11 at 12:55
  • @sbg - That I can't help with I'm afraid, I normally use LaTeX for all documents where I care about how they look, for that reason and many other reasons. (It's also somewhat off-topic for SO I suspect) – Flexo Nov 18 '11 at 12:57

4 Answers4

20

There is a small error in your original png command. Try this:

png(file="mag_feb.png", units="in", width=11, height=8.5, res=300)

Now, width and height are in inches, and res is in pixels/inch. Before, the res parameter was being ignored.

bdemarest
  • 14,397
  • 3
  • 53
  • 56
4

You should be using a vector format, like PDF, for plots you will print. If the images look good when printed but not in Word, that is a problem with Word's downscaling feature. You might want to try using the vector Windows Metafile format to get things into Word.

Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
  • thanks for answering! I was using the "copy as metafile" initially but I have lots of graphs in this word document and it just slowed word to the point that it would freeze for bits while scrolling. Would the same happen with win.metafile or PDF? Also how would I get the image from the PDF to word? If I use the "camera" bottom the plots still don't look good... – sbg Nov 17 '11 at 12:42
  • 2
    `win.metafile()` will give you the same problem. Your ultimate problem is that Word is not designed for the layout of high-quality images. If at all possible, it would be best to remove the images from your Word document and print them separately. Or use more suitable publishing software (LaTeX or Adobe InDesign). – Michael Hoffman Nov 17 '11 at 12:46
  • 2
    If your work does not require you to use Word, I would definitely switch to Latex. If you can use R, you can definitely use Latex. – Paul Hiemstra Nov 17 '11 at 13:31
3

As @awoodland notes, you want to export graphs in a vector format.

win.metafile(file="mag_feb.png") # or pdf(), svg(), etc.
par(mfrow=c(1,3),mar=c(1,1,2,1),oma=c(4,1,2,0),mgp=c(3, 0.5, 0)) 
plot(...)
dev.off()
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • thanks for answering! I was using the "copy as metafile" initially but I have lots of graphs in this word document and it just slowed word to the point that it would freeze for bits while scrolling. Would the same happen with win.metafile or PDF? Also how would I get the image from the PDF to word? If I use the "camera" bottom the plots still don't look good... – sbg Nov 17 '11 at 12:39
  • I think Word supports WMF the best? So you can just import it into word using the Insert menu or something like that? – Ari B. Friedman Nov 17 '11 at 16:39
1

I hope this is not a late reply, but my workflow to save optimal pictures from within R is the following:

1) copy the figure straight from the graphic device to a pdf file, using "dev.copy2pdf":

dev.copy2pdf("image.pdf", width=8.5, height=11)

2) use imagemagick to convert the pdf into a high-res png, using this command:

convert -density 600 image.pdf image.png

3) optional - also with imagemagick, remove all the white borders and leave only the contents that matter:

convert image.png -trim image_trimmed.png

Hope it helps.

thiagoveloso
  • 2,537
  • 3
  • 28
  • 57
  • 1
    Also my workflow, tested over years as the most reliable one both in terms in quality and repeatability. But need a linux box. – cmbarbu Nov 16 '18 at 16:05