本文介绍了强制ggsave对.wmf文件中的点几何矢量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用R生成的图如果无法正确地勒索,则不能用于发布.我在Windows机器上工作,并将MS Word 2016用于所有写作目的.因此,我希望将绘图导出为.wmf文件(我想,.emf也可以).

Plots produced with R are not usable for publication if they cannot be exorted properly. I work on a Windows Machine and use MS Word 2016 for all writing purposes. So, I wish to export my plots as .wmf files (.emf would also do, I suppose).

我用ggplot2生成所有图,所以我想ggsave (device = "wmf")似乎是一个不错的选择.但是,我对生成的文件存在一个主要问题:点几何似乎被打印为栅格而不是矢量格式.这是生成简单散点图的示例:

I produce all graphs with ggplot2, so ggsave (device = "wmf") seems a good choice, I suppose. However, I have a major problem with the resulting files: point geoms seem to be printed as raster instead of vector format. Here is an example for producing a simple scatterplot:

library (ggplot2)

plot_data <- data.frame (a = runif (1:20),
                         b = seq (1:20))

x11 (width =  3, height = 3)

ggplot (data = plot_data, mapping = aes (x = a, y = b)) +
    geom_point () +
    labs (x = "my x-label", y = "my y-label") +
    theme (panel.background = element_blank(),
           panel.border = element_rect (fill = NA, size = 0.7),
           axis.ticks = element_line (color = "black", lineend = "round"),
           axis.ticks.length = unit (2, "mm"),
           axis.text = element_text (color = "black"),
           plot.margin = unit(rep (0, 4), "cm")
           )

我使用以下代码保存该图:

I save the plot with the following code:

ggsave(filename = "my_file.wmf", device = "wmf")

当我在MS Word或Libre Office中打开绘图时,我发现这些点根本没有得到高质量的渲染.在Libre Office Draw中,一个点看起来像这样(放大了很多):

When I open the plot in MS Word or Libre Office, I see that the points are not rendered in good quality, at all. In Libre Office Draw, a point looks like this (zoomed in quite a lot):

在MS Word中,绘图如下所示:

In MS Word, the plot looks like this:

具有这些要点":

但是,标签和轴都可以. MS Word:

The labels and axes, however, are ok. MS Word:

Libre Office抽奖:

Libre Office Draw:

我假设标签,刻度线注释和轴(甚至点周围的圆)以矢量格式存储,而点几何似乎存储为栅格.我担心,由此产生的地块将无法使用.因此,我想找到一个选项来强制ggsave ()将点几何矢量化,而不是打印栅格.我希望很多人能为您提供帮助-我迫切需要一种简单的方法来从R导出图进行发布,以便说服我的实验室更多地依赖R.

I suppose that the labels, tick annotations and axes (and even circles around the points) are stored in vector format, whereas the point geoms seem to be stored as rasters. The resulting plots are not useable, I fear. So, I want to find an option to force ggsave () to vectorize point geoms instead of printing raster. I hope very much someone can help - I urgently need a simple way to export plots from R for publication in order to convince my lab to rely more on R.

推荐答案

您可以使用附加的CRAN包 devEMF .这避免了导出到SVG以及随后转换为EMF的任何需要.我不确定为什么ggsave(..., device="wmf")调用(使用窗口API生成文件)为什么在这里使用栅格绘图符号,但是devEMF中的函数肯定使用矢量(我说这既是devEMF的开发者,也得到了使用LibreOffice进行放大).

You can directly create a fully vectorized EMF (or EMF+) file in R using the add-on CRAN package devEMF. This avoids any need to export to SVG and later convert to EMF. I'm not sure why the ggsave(..., device="wmf") call (which uses the window API to generate the file) is using raster plotting symbols here, but the functions in devEMF definitely use vectors (I say this both as the developer of devEMF and confirmed by zooming in with LibreOffice).

在安装devEMF之后,您可以从原始问题中运行示例代码,然后:

After installing devEMF, you can run the example code from the original question and then:

library(devEMF)
ggsave(filename = "my_file.emf", device = emf)

注意:上面的代码中使用的emf(不带引号)是devEMF定义的函数的名称(类似于原始问题中的x11). ggsave将"emf"(带引号)解释为对使用Windows API的请求.您希望前者不带引号,而后者不带引号.

NOTE: emf (without quotes) used in the code above is the name of a function defined by devEMF (analogous to x11 used in the original question). "emf" (with quotes) would be interpreted by ggsave as a request to the use windows API. You want the former without quotes, not the latter with quotes.

或者,可以替换对x11的原始调用,而无需ggsave即可直接生成emf文件:

Alternatively, the original call to x11 could be replaced and the emf file generated directly without the need for ggsave:

library (ggplot2)
library (devEMF)

plot_data <- data.frame (a = runif (1:20),
                         b = seq (1:20))

emf (file = "my_file.emf", width =  3, height = 3)

ggplot (data = plot_data, mapping = aes (x = a, y = b)) +
    geom_point () +
    labs (x = "my x-label", y = "my y-label") +
    theme (panel.background = element_blank(),
           panel.border = element_rect (fill = NA, size = 0.7),
           axis.ticks = element_line (color = "black", lineend = "round"),
           axis.ticks.length = unit (2, "mm"),
           axis.text = element_text (color = "black"),
           plot.margin = unit(rep (0, 4), "cm")
           )

dev.off() #must close device to close file!

根据我的经验,MS Office对EMF& EMF +与WMF一样完整,因此生成EMF是此用例的合理解决方案.如果遇到兼容性问题,请查看emf函数的emfPlus *参数(请参见help("emf", package="devEMF")).

In my experience, MS Office support for EMF & EMF+ is as complete as for WMF, so generating EMF is a reasonable solution for this use case. If you run into compatibility problems, look at the emfPlus* arguments to the emf function (see help("emf", package="devEMF")).

这篇关于强制ggsave对.wmf文件中的点几何矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 18:09