A simple wrapper to add a 'ggplot' object as a png in a document.
It produces an object of class 'block_gg' with a corresponding method to_wml()
that can be used to convert the object to a WordML string.
block_gg(
value,
fp_p = fp_par(),
width = 6,
height = 5,
res = 300,
scale = 1,
unit = "in"
)
'ggplot' object
paragraph formatting properties, see fp_par()
image size in units expressed by the unit argument. Defaults to "in"ches.
resolution of the png image in ppi
Multiplicative scaling factor, same as in ggsave
One of the following units in which the width and height arguments are expressed: "in", "cm" or "mm".
Other block functions for reporting:
block_caption()
,
block_list()
,
block_pour_docx()
,
block_section()
,
block_table()
,
block_toc()
,
fpar()
,
plot_instr()
,
unordered_list()
library(officer)
if (require("ggplot2")) {
set.seed(2)
doc <- read_docx()
z <- body_append_start_context(doc)
for (i in seq_len(3)) {
df <- data.frame(x = runif(10), y = runif(10))
gg <- ggplot(df, aes(x = x, y = y)) + geom_point()
write_elements_to_context(
context = z,
block_gg(
value = gg
)
)
}
doc <- body_append_stop_context(z)
print(doc, target = tempfile(fileext = ".docx"))
}
#> Loading required package: ggplot2