Indian Forum for Water Adroit

Exporting High Resolution figures from 'R'

shailza

  • ***
  • Thanked: 2 times
  • +100/-0
    • View Profile
Exporting High Resolution figures from 'R'
« on: July 05, 2017, 11:56:35 PM »
How can we export high resolution tiff file from R?
 

Karthikeyan L

  • *****
  • Thanked: 52 times
  • +34/-0
  • Research Scholar
    • View Profile
  • Institute : Indian Institute of Science, Bangalore
  • Programming language : Matlab, R
Re: Exporting High Resolution figures from 'R'
« Reply #1 on: July 06, 2017, 12:21:18 AM »
We can create high resolution plots in two ways: 1) using the default functions; 2) using the 'cairo' package. Say you want to create a tiff file. Both methods have the following syntax,

Code: [Select]
tiff (filename='filename.tiff',  width=5, height=4,units="in",pointsize=12,res=72)
$$your plot command$$
dev.off ()
In case of cairo, load the library first, instead of tiff we use:
Code: [Select]
cairo(filename='filename.tiff', type="tiff", width=5, height=4,units="in",pointsize=12,res=72)Cairo is used because it produces much smoother plots.

Say you want to increase the resolution to 300 dpi from existing 72 dpi. Apart from changing 'res' in the function input, you should also increase the pointsize. The default pointsize correspond to 1/72 of inch. The pointsize should be increased according to this formula: 12×(new resolution)/72. In case of 300 dpi, it will be 12×300/72=50.

In case your output seems to be clumsy, increase width and height parameters.
 
The following users thanked this post: shailza

shailza

  • ***
  • Thanked: 2 times
  • +100/-0
    • View Profile
Re: Exporting High Resolution figures from 'R'
« Reply #2 on: July 06, 2017, 02:10:06 AM »
I am able to save .png file with high resolution, but there is some problem with the TIFF file.

> CairoTIFF(filename = "Rplot03.tiff", width = 480, height = 480,
+          pointsize = 12, bg = "white", res = 72)
Error in Cairo(width, height, type = "tiff", file = filename, pointsize = pointsize,  :
  Sorry, this Cairo was compiled without tiff support.


In the Cairo documentation, it's highlighted that the package is capable of generating .tiff file.
 

Pankaj Dey

  • *****
  • Thanked: 86 times
  • +109/-0
    • View Profile
  • Institute : Indian Institute of Science
  • Programming language : MATLAB, R
Pankaj
 
The following users thanked this post: shailza

shailza

  • ***
  • Thanked: 2 times
  • +100/-0
    • View Profile
Re: Exporting High Resolution figures from 'R'
« Reply #4 on: July 06, 2017, 02:16:34 PM »
I have tried all these yesterday, but nothing works. Error is coming for all the combinations of width and height when the resolution is high. But it can save the figures up to 100dpi.
> tiff('test.tiff', width=486, height=486, res=300)
> scatterplot3d(iris[,1:3])
Error in plot.new() : figure margins too large
> tiff('test.tiff', width=20, height=20, res=300)
> scatterplot3d(iris[,1:3])
Error in plot.new() : figure margins too large
 

Karthikeyan L

  • *****
  • Thanked: 52 times
  • +34/-0
  • Research Scholar
    • View Profile
  • Institute : Indian Institute of Science, Bangalore
  • Programming language : Matlab, R
Re: Exporting High Resolution figures from 'R'
« Reply #5 on: July 06, 2017, 04:59:58 PM »
Give the width and height units in inches. It will be easier for you to understand and manipulate the values. Plot error regarding margins could be due to insufficient space in the RStudio window. I know its silly, but you can increase the space for the plot by dragging with mouse. Also, I can see that you are not using dev.off() command, you should include that too. Forget the pointsize for now, increase the resolution, specify width and height in inches, and let us know what you have got!

Code: [Select]
tiff('test.tiff', width=20, height=20, res=300)----> The default units are pixels, so you are giving the size as 20 x 20 pixels, which is definitely too small for the figure to plot, isn't it? So, my advice is to change the units to inches and then keep playing with widths and heights

If you are still unable to plot, please provide data here, we can try to help you with that!
 

Karthikeyan L

  • *****
  • Thanked: 52 times
  • +34/-0
  • Research Scholar
    • View Profile
  • Institute : Indian Institute of Science, Bangalore
  • Programming language : Matlab, R
Re: Exporting High Resolution figures from 'R'
« Reply #6 on: July 06, 2017, 05:28:03 PM »
And I forgot about 'Cairo' error. With the current version of Cairo, you cannot create tiff file. Type Cairo.capabilities() to see what all formats are supported. To make this work, you need to edit the source code of Cairo library using the modified code put up by the Cairo developers on this github page https://github.com/s-u/Cairo/tree/master/R. If you wan to avoid all of it, then go with default function tiff. Apart from smoother plots, the only other advantage with Cairo is the option of enabling background transparency.
 

subash

  • *****
  • Thanked: 32 times
  • +16/-0
    • View Profile
  • Programming language : Python, R and Matlab
Re: Exporting High Resolution figures from 'R'
« Reply #7 on: July 07, 2017, 12:57:24 PM »
A small addition to what Karthik has touched upon, If you are not getting the plots in the Rstudio plot window, just check if the graphics handle is clear or not.. You can do that by checking dev.list().. If the result is NULL then you are good to plot or you have clear the graphics window before you plot to view the plot in the plot window and if you wish to save without viewing the plot then dev.off() is definite.

Code: [Select]
> dev.list()
png
  4

> dev.off(4)
null device
          1

> graphics.off()