14

This is such a basic problem that's driving me crazy. When generating a figure in R it looks great on the screen. But when I try to generate it directly onto a file using png(), tiff(), etc. by setting the resolution to 300 and the width and height to reasonable values that would suit a journal paper well, there are 2 problems:

  1. All lines are made super thick
  2. All letters are in huge font.

This has been really annoying, I've tried playing with the pointsize option, it helps make the font size smaller, but the line widths are still thick and ugly. Can you please suggest what's going on wrong in R and how I can fix this? I've looked around and most solutions involve using other image processing software. I'd rather figure out why R does this when increasing the resolution and why it makes the figures so ugly. Here's an example:

png(file="test.png",width=5,height=5,units="cm",res=300)
plot(rnorm(1000),rnorm(1000),xlab="some text")
dev.off()

Thanks!

FBC
  • 993
  • 3
  • 9
  • 15
  • Are you not allowed to include PDFs in your journal submissions? – Jason Morgan May 17 '13 at 22:55
  • The figures have to be submitted as separate files not exceeding 10MB each. – FBC May 17 '13 at 23:06
  • 1
    You can use `pdf()` to create PDFs for each plot, as separate files. – Jason Morgan May 17 '13 at 23:11
  • 1
    When I used `pdf()` on your example, it came out less than half the size of the PNG and much better looking. Try `pdf(file="test.pdf")` rather than your `png()` line. – pjs May 17 '13 at 23:16
  • 1
    Related blog post on graphing resolution in R: http://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html – Tyler Rinker May 17 '13 at 23:59
  • 1
    I suggest adjusting height and width until the plot looks right. For example, 12cm works pretty well: `png(file="test.png",width=12,height=12,units="cm",res=300)`. You are making a bitmap image, so don't take 'centimeter' too literally--it's just a way to force R to rescale the font and line widths. Once you find a good height and width, then you can adjust res to make the pixel dimensions fit your needs. – bdemarest May 18 '13 at 01:45
  • - I agree, pdf() makes much better looking figures, but unfortunately no, the journal only accepts TIFF or EPS format. - so the units are not literal? I don't see the point of that then if we still have to fiddle about with those numbers if they don't mean anything. - the article is useful, but one thing I don't understand is why R considers the plotting area to be smaller when res is increased, that's counterintuitive to me – FBC May 18 '13 at 05:55
  • 3
    I think R's graphics output isn't device independent because the graphics system uses an annoying mix of sizes - absolute lengths (things in units of cm or points) together with an idea of how big in those lengths the graphics output window is, together with fractional lengths. I've heard people complain about this before (so much so I thought user19999 might be Ben...). I'm wondering if R should try and work towards something more device-independent... – Spacedman May 18 '13 at 08:25
  • If the journal accepts EPS then see http://stackoverflow.com/questions/5142842/export-a-graph-to-eps-file-with-r – pjs May 18 '13 at 16:49

1 Answers1

13

I think the issue is with the default point size (see parameter pointsize in ?png):

Here's what you had with the default of 12:

enter image description here

But if you lower it down to 6:

png(file="test.png",width=5,height=5,units="cm",res=300, pointsize=6)
plot(rnorm(1000),rnorm(1000),xlab="some text")
dev.off()

enter image description here

The way I understand it, a pointsize of 12 means that a text at cex=1 is 12/72th (i. e. 1/6th) of an inch. Your png being ca. 2 inches, your text is therefore 1/12th of the plot width with the default pointsize.

plannapus
  • 18,529
  • 4
  • 72
  • 94
  • 1
    I tried changing the pointsize, and I agree the image looks somewhat better, but look at the plotting characters, they have rough edges and look terrible, nowhere near the quality for print journals, and this at supposedly high resolution of 300dpi. – FBC May 22 '13 at 19:02
  • if you look at the plot at its normal size (i. e. 5cm per 5cm) the characters look ok. – plannapus May 23 '13 at 06:49
  • @FCB In his `ggplot2` book, Hadley Wickham recommends to use 600 dpi for raster graphics (Chapter 8.3). This advice might be applicable to base graphics as well. – Uwe Jan 27 '17 at 14:29