Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating high-resolution figures in R

Tags:

r

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!

like image 266
FBC Avatar asked May 17 '13 22:05

FBC


1 Answers

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.

like image 180
plannapus Avatar answered Sep 18 '22 15:09

plannapus