Force ggsave to vectorize point geoms in .wmf-files

感情迁移 提交于 2020-02-25 03:46:34

问题


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).

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")

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):

In MS Word, the plot looks like this:

with these "points":

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

Libre Office Draw:

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.


回答1:


I didn't find a way to make the WMF device behave, but you can save to SVG and then use R to automate conversion from SVG to EMF via Inkscape (a free software you need to download separately) as:

inkscape_path <-'C:/Program Files/Inkscape/inkscape.exe'
if(!file.exists(inkscape_path)) {
  warning("Could not find inkscape, will not convert to .emf")
} else {
   input_file <- "plot.svg"
   output_file <- "plot.emf"
   system(paste0('"', inkscape_path,'"', ' --file "', input_file, '" --export-emf "', output_file, '"'))
}

(Inkscape doesn't support export to WMF from command line but for the purpose of importing int Office, WMF and EMF should be interchangeable)

EDIT: Unfortunately, you cannot access some export settings from the command line (notably the "convert text to paths" option). However, the command line will use the settings you last used to export WMF, so you can save once manually with the desired settings and do further conversions automatically. (this is a known limitation: https://bugs.launchpad.net/inkscape/+bug/1747696)



来源:https://stackoverflow.com/questions/58607860/force-ggsave-to-vectorize-point-geoms-in-wmf-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!