R language high-definition picture output settings

R visualization based on packages such as ggplot2 is a very popular way for researchers, but I also encountered some problems with this. After exporting a high-resolution picture, insert it into word, it will appear blurry (very compressed). If it is not compressed, the word can easily exceed 100M. Recently, there is a solution, so let's introduce it.

Solution idea: R+Corel Draw

This is also the modification requirement of a journal image, and a .cdr file must be provided. Of course, after the test, it was found that the tif output by .cdr can be very clear even if it is compressed.

Here's how to do it. First of all, R currently cannot directly output files in .cdr format. According to online experience, it is possible to use emf format to output vector files that can be read by Corel Draw, and then make some fine-tuning after importing.

However, the R output emf does not have statements like tiff and jpeg, and the savePlot function is mainly used.

Here is the help document of R to explain this function

Usage

savePlot(filename = "Rplot", type = c("wmf", "emf", "png", "jpg", "jpeg", "bmp", "tif", "tiff", "ps", "eps", "pdf"), device = dev.cur(), restoreConsole = TRUE)

Arguments

filename The filename under which to save the plot. Tilde-expansion (see path.expand is supported.

type The type of plot, Windows metafile, PNG, JPEG, BMP (Windows bitmap format), TIFF, PostScript or PDF.

device A device number of a windows device, by default the current device.

restoreConsole See the ‘Details’ section of windows.

Let's explain it one parameter at a time. filename: It is easy to understand, that is, the path and name of the image you want to output, in a string type. Such as "C:/Rplot". No suffix is ​​required. type: output format. device: The device number of the Windows device, the default is the current device, this parameter. restoreConsole: This parameter is also related to Windows graphics devices.

In actual use, the last two parameters can be used by default. Mainly for the first two parameters.

Next, a case is used to demonstrate the operation. The default mtcars data of R language is used. Test with plot plot and ggplot2 plot each. But the author reported an error when running savePlot in Rstudio as mentioned on the Internet.

In fact this statement can only be run in native Rgui. Therefore, save the R code as a script, and call the written script directly with source in Rgui.

plot is just a direct plotting statement. And ggplot needs one more print statement to output the picture.

After running, open corel draw and operate as shown below.

This turns the emf into a fully editable vector.

Of course Rstudio also has a method. Just use the copy to clipboard function and paste it into Corel Draw.

This is the pre-work for importing R into Corel Draw. Compared with the very compressed images before, I feel that the image quality output by Corel Draw is very high. In addition, when I searched later, I also found a package for R: devEMF: EMF Graphics Output Device . You can output emf, so it can also be operated in Rstudio.

Reference blog: R language can be played like this | High-quality pictures are exported like this!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324502561&siteId=291194637