Saving plot to tiff, with high resolution for publication (in R) - r

A journal we are sending an article to is asking for the following:
To ensure the best reproduction quality of your figures we would appreciate high resolution files. All figures should preferably be in TIFF or EPS format... and should have the following resolution:
Graph: 800 - 1200 DPI
Photo: 400 - 800 DPI
Color (only CMYK): 300 - 400 DPI (DPI = dots per inch)
Since I am sending a graph, I am trying to save it using tiff.
Here is the code I am using:
tiff(filename = "c:\\aaa.tiff",
res = 800, pointsize = 2)
plot(1:100)
dev.off()
But sadly, it produces a very "bulky" image - and if I where to not use pointsize = 2, I would get the error message:
Error in plot.new() : figure margins too large
Any suggestions?

One thing you should do is use the compression parameter.
For instance:
tiff("outfile.tif", compression = "lzw")
LZW is a lossless compression format, so you will not lose any data, but you will definitely reduce filesize.

short answer:
It's a bulky file because the higher the resolution raster file (TIFF) then the larger the file.
The other option, EPS, is like PDF or PS. Just save in any of those. That's a vector image format and one they said was acceptable. It will be more compact and of higher quality (unless your figure contains an enormous number of points or lines.)
long answer:
One format they asked for is TIFF, a raster format. It saves the information as a value representing each pixel in the image. If the image is 100 DPI and 5" square then that's 500x500 and it saves 250000 pixels of information. As the resolution of the image gets higher and the image size stays constant then the points of information needed goes up. At 200 DPI it's 1000x1000 and 1e6 pixels of information. This explains why your file is so bulky.
In order to have a quality printed image at 5" square you would need about 300-400 DPI (dots per inch). That way you won't see each of those individual little pixels that make up the image. If it's at a lower resolution then the image is still 5" x 5" but each individual square block (pixel) that makes up the image is so large you can see them. That's why they asked for that resolution. Otherwise, text and lines in your graphs would appear blocky, jaggy, or blurry. It's why text on your computer screen (only around 100 DPI these days) isn't nearly as high quality as printed on a piece of paper (usually at least 300 dpi).
The other format they asked for was EPS. This is encapsulated postscript. Since it's a single page file PS, or postscript, would be equivalent. And, PDF is also similar. These are all vector formats, essentially the same vector format (they can contain raster format images too but let's ignore that).
A vector format file saves the information about how the image was drawn but not about each pixel. If you have an x-axis 3" long placed 0.5" from the bottom of the image then the vector file knows this description. Each feature of the graph is described in the file. Because of this, for simple drawings it is vastly more compact than any raster representation at a given image quality. Furthermore, it scales in size without losing quality. You simply ask it to draw the image at 6" x 6" instead of 5" x 5" and it scales each of the instructions for drawing the components of the image up as well. Therefore, even though the PDF or EPS file will be more compact, it will print at the highest quality.
Send a PDF or PS file and don't worry about the resolution.

When you increase resolution (res), the effective size of a plot decreases, and so the margin error jumps out. Try to make bigger picture with greater width and height (in theory, to make a plot of a same virtual size, it should be new_(width|height)=res/72*old_(width|height).
Still, as romunov suggested, it is a better idea to use ImageMagick; you won't loose quality.

I would suggest you to use devEval from the R.devices library.
I had this issue myself and solved it thanks to this :
options("devEval/args/path"=file.path(Folder_of_Output))
devEval("tiff", name="Name_output", width=120, height=800, {
barplot(data, main="imagetitle");
legend("top", legend =c("text of legend"))
})
For me it worked like a charm and the file was saved directly without issue.

Related

How to export the graphs produced in R?

As a beginner on R I manage to produce correct relational graphs thanks to R. They are about correct in the visualization space but of very bad quality when I export them in PDF or in JPEG / PNG. The image is not centered, a part of the legend is missing, the graph is very small or blurred etc.
How do you proceed for the export?
Thanks in advance!
I am looking for the right handling or settings to export the graph visualizations produced in R?
I understood that I had to set up the viewer space but via the code but have no idea how to do it...
Here an example how to write a plot to a PNG file. The plot "commands" are embedded in png(....)and dev.off(). Several options are available to configure size and resolution.
png("myfile.png", width=1600, height=1200, res=300) # good for LaTeX or Word
#png("myfile.png", width=800, height=600, res=150) # good for Powerpoint or Impress
plot(iris$Sepal.Length, iris$Petal.Length)
dev.off()
Some hints:
width and height are given in pixels
res influences nominal resolution and font size (play with it)
use at least 300 dpi (dots per inch). For centimeters, the number of pixels = 300/2.54 * width in cm
professionals use 600 or even 1200 pixels per inch, but then .docx and .pptx files will dramatically increase
1600 x 1200px is good for 13.3 x 10 cm size in the printed document
If you work with LaTeX, it is in most cases better to use PDF for the figures. Another very good idea is to use Markdown for the text. Then, figures are automatically embedded.

How to output always the same size (width/height) graph in R?

When I output the graph in R, the size (width and length) is always changing. So whenever I output graphs, I change the size by myself.
For example, I need a consistent graph size to insert into my document, but sometimes the default value of width and height is not the same when I try to export, particularly on different PC. How can I fix W:500. H:400 graph size all the time?
Many thanks!!
In order to get consistency, you should set the size of the graphics device yourself before plotting. For example, in Windows, you can use
windows(width=5.5, height=5, title="Controlled Size")
## NOW make your plot
to get a window that is always 5.5 inches wide by 5 inches high.
Other devices use minor variations for setting the size. You can check out other devices by looking at the help page help(Devices)
For example, bmp, jpeg, png and tiff allow you to set the number of pixels, not just the number of inches.

RStudio windows size

In RStudio, we can adjust the size of 4 windows (or panels): text editor, console, environment/history, files/plots/packages/help/viewer.
I frequently export plots from RStudio and paste it in MS Word or save it as an image file. My concern is that the size of the plot is different every time because I often adjust the size of panels as I need.
Is there any way that I adjust the panels' sizes to the same sizes that I did before after I randomly adjust the panel sizes?
If I can specify the panel size by numbers, then I can do it, but RStudio does not provide such feature.
This code writes your plot in PNG format (good for web), with a specified width, height, and resolution. (In this case, 400 pixels / 72 pixels per inch = 5.56 inches across--every time). See here for more detail and other formats.
png(file="YOUR_PLOT",width=400,height=350,res=72)

Reduce PDF file size of plots by filtering hidden objects

While producing scatter plots of many points in R (using ggplot() for example), there might be many points that are behind the others and not visible at all. For instance see the plot below:
This is a scatter plot of several hundreds of thousands points, but most of them are behind the other points. The problem is when casting the output to a vector file (a PDF file for example), the invisible points make the file size so big, and increase memory and cpu usage while viewing the file.
A simple solution is to cast the output to a bitmap picture (TIFF or PNG for example), but they lose the vector quality and can be even larger in size. I tried some online PDF compressors, but the result was the same size as my original file.
Is there any good solution? For example some way to filter the points that are not visible, possibly during generating plot or after it by editing PDF file?
As a start you can do something like this:
set.seed(42)
DF <- data.frame(x=x<-runif(1e6),y=x+rnorm(1e6,sd=0.1))
plot(y~x,data=DF,pch=".",cex=4)
PDF size: 6334 KB
DF2 <- data.frame(x=round(DF$x,3),y=round(DF$y,3))
DF2 <- DF[!duplicated(DF2),]
nrow(DF2)
#[1] 373429
plot(y~x,data=DF2,pch=".",cex=4)
PDF size: 2373 KB
With the rounding you can control how many values you want to remove. You only need to modify this to handle the different colours.
Simply saving the plot as a high-res png file will very drastically cut the size, while keeping the quality more than good enough. At least I've never had journals complain about any of the png's I sent them, just keep sure to use > 600 dpi.
I think it might be done with some post-processing of the pdf-file. In linux, if I have to reduce a pdf, I would do
pdf2ps input.pdf output.ps
ps2pdf output.ps output.pdf
which for some reason works quite efficiently.
You can see some discussion at https://askubuntu.com/questions/113544/how-to-reduce-pdf-filesize.

How to enlarge map/plot in R?

I plotted a map in R but when I export it the size is very small. How can I enlarge the map and still save it as a picture? (I know that I can save it as PDF and then it's a vector graphic - but I need to copy it to Power Point and also need a transparent background - I don't think that is possible with a PDF isn't it?)
As you can see here the map is way to small to use it in a Power Point slide:
If someone knows a good way to save it as vector graph that I can easily use in Power Point that would be perfect as well.
The png() function lets you specify width and height size in the default resolution of "px" and the defaults are 480 and 480. You can also supply a res argument in units of "ppi". If you have text you probably ought to specify point size >= 20 for legibility. I generally save my graphs as PDF and convert to PNG with an external program. However, the latest versions of PowerPoint will accept pdf formats. It is also possible to save as an .eps format.

Resources