R markdown, ggplot export to word

Dear all,

My problem is that I use R markdown to write all of my studies in HTML file, but since I work in academic era, I also must report my work in word with strict rules. You may know that in APA format, the text size must be 11 on the figures.
Mostly, I import the figures to the word file, from the library what R creates with the plots of a html file, when I knit. But when I import them to word I lose the contor over the text size on the plot. (format = png, in ggplot2 I added size 11)
Do you know any trickt to solve this issue?

Did you do any manual resizing of the image after dropping it into Word? This will affect the apparent font size. I don't think you can embed PDF or SVG images into Word. They get "flattened" into PNGs which means you can't modify the figure text.

You need to set the font size in ggplot(...) and set the image height and width to whatever size you want the figure within the Word doc (figure this out before saving) when exporting, using ggsave(...).

For example, if you want the figure to be 4in x 3in in the Word doc:

p <-
ggplot(iris, aes(Sepal.Length)) +
  geom_histogram() +
  theme(text = element_text(size = 11))

ggsave('image.png', p, width = 4, height = 3, units = "in")

That's great. One more Q then connected to this, when i specify the plot size in the header of a chunk in markdown, is it in "in"? Or should force it like this anyway?

Yes, I believe fig.height and fig.width are in inches. However, I am not certain if these chunk parameters will be used when saving a plot in your code. They are definitely used when the code chunk produces a figure.

You can test it out, but to be safe you might want to specify the height and width in the ggsave() call.

Friends don't let friends use Word :grin:

The best option is to set text size in ggplot2 as part of the theme. If you were only allowed to present in pdf, there are R packages that will take care of the rest.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.