Alternative to officer::body_add_gg() which uses ggplot syntax
an rdocx object
ggplot object
width and height. Can be abbreviated to w and h.
units for width and height
paragraph style
resolution of the png image in ppi (passed to the argument dpi of ggplot2::ggsave())
other arguments to be passed to ggplot2::ggsave()
The docx object doc
library(officer)
library(ggplot2)
p = ggplot(data=iris, aes(Sepal.Length, Petal.Length)) + geom_point()
crosstable_options(
units="cm",
style_image="centered"
)
doc = read_docx() %>%
body_add_normal("Text before") %>%
body_add_gg2(p, w=14, h=10, scale=1.5) %>% #or units="cm" instead of using options
body_add_normal("Text after")
write_and_open(doc)