How to export the graphs produced in R? - 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.

Related

How to save ggplots in PDF with less size R?

I am having trouble saving numerous ggplots inside pdf because I am creating ggplots (scatter plots and boxplots) with 12 million rows (lots of observations).
The problem is when I save the plot as PDF using:
ggsave("my_plots.pdf", myArrangedPlots)
The pdf size is very large = 90 MB for only 120 pages of PDF
When I save one plot as PNG using:
ggsave("plot1.png" plot1)
The size is much less in comparison to saving same single plot in PDF (1MB vs 0.1 MBs)
I think the reason is that ggplot internal mechanism tries to save the plots in Vectorized format format inside the PDF file to get maximum resolution but I don't need that much of resolution. Also note that when there are million of points represented in Vectorized format the size is going to be greater than the same plot in PNG, because PNG doesn't save layers.
I want to save the plots in PDF format but embedding the plots as PNGs instead of Vectorized format to make the PDF file size smaller.
I there any parameter in ggplot2 to achieve this or is there any workaround?
Observing the documentation of pdf(), it's parameters seem to be compatible with ggsave().
I found a parameter which is useDingbats, by default it is set to FALSE but If you set it to TRUE, the PDF size reduces drastically from 94 MB to 10 MB in my case.
So I use it like this:
ggsave("myplots.pdf", arrangedPlots, useDingbats = TRUE)
NOTE: setting useDingbats to true what does is using Dinbats font for small circles, which in case of the scatter plots and boxplots with lots of outlier points reduces the size of final PDF a lot.

Export 300dpi tiff of plotly plot in R

Is there a way to export a 300dpi tiff of a plotly plot?
I have a sunburst chart I'm trying to export but I can't find a good way to get the resolution I need.
You will need to install orca. Please find the installation instructions here.
Once it is installed you can use plotly's orca() function. The plotly book gives the following information regarding exports specified in inches (DPI):
It’s worth noting that the height and width of a static image must be
specified in pixels, which is intuitive for most file formats (png,
jpeg, svg, etc) but when exporting to pdf, you might want to specify
the size in inches. If you multiply the DPI of your machine’s display
by the number of inches you want, you’ll get the desired result.
Accordingly for 300 DPI you will need something like:
orca(plot_ly(), width = 8 * 300, height = 11 * 300)
Also, you might want to see this related article.

How to download clear plot through rstudio?

I plot in rstudio, cannot see the number in plot, then I download as bigger size.
Whatever the png or jpeg format, even size is 2048 pixels, still cannot read the number in plot.
How to solve this problem?
RStudio has known sizing issue of saving pictures plotted in the "plots" window. What I would do is to save the picture directly in code.
Refer to this SO question
In short, what you can do is:
png(filename = pathToYourOutputPNG) # create a png device
plot() # write your plotting functions here
dev.off() # close the device

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.

Saving plot to tiff, with high resolution for publication (in 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.

Resources