Skip to contents

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.

Usage

block_gg(
  value,
  fp_p = fp_par(),
  width = 6,
  height = 5,
  res = 300,
  scale = 1,
  unit = "in"
)

Arguments

value

'ggplot' object

fp_p

paragraph formatting properties, see fp_par()

width, height

image size in units expressed by the unit argument. Defaults to "in"ches.

res

resolution of the png image in ppi

scale

Multiplicative scaling factor, same as in ggsave

unit

One of the following units in which the width and height arguments are expressed: "in", "cm" or "mm".

See also

Other block functions for reporting: block_caption(), block_list(), block_pour_docx(), block_section(), block_table(), block_toc(), fpar(), plot_instr(), unordered_list()

Examples

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