export::graph2office moves axis labels around

2020-03-30 06:18发布

问题:

I have made plots in R (RStudio) with ggplot2. When I export them via export::graph2office, the labels are moved around. However, this only happens when I specify the font for the labels.

library (ggplot2)
library (export)    


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"),
                      text = element_text (size=18,  
                                family="ChantillyLH", 
                                color = "black")
               )

graph2office (file = "my_graph", type = "DOC")

Here, you can see the graph in R (to the right) and the exported graph in word (to the left):

The undesired behaviour is more obvious for the y-label in this example, but also the x-label is moved a bit. I wonder if there is a way to fix this.

The same happens when I specify another font family, for example family="Comic Sans MS":

EDIT: it even happens when no textcommand is given:

回答1:

The answer probably is: yes, export::graph2office moves axis labels around (so do export::graph2pptand export::graph2doc). There is no way to fix this. If you want to style your graphs in R and export them as-is into Office, the export::graph2office function, unfortunately, is not your way to go. However, the function can of course be used as a quick-and-dirty option to produce editable office-graphs.

If your goal is to export graphs in a more reliable manner, CairoSVG might be a much better option (see my answer here: Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007).