body_add_gg: Add a 'ggplot' in a 'Word' document

View source: R/docx_add.R

body_add_ggR Documentation

Add a 'ggplot' in a 'Word' document

Description

add a ggplot as a png image into an rdocx object.

Usage

body_add_gg(
  x,
  value,
  width = 6,
  height = 5,
  res = 300,
  style = "Normal",
  scale = 1,
  pos = "after",
  ...
)

Arguments

x

an rdocx object

value

ggplot object

width

height in inches

height

height in inches

res

resolution of the png image in ppi

style

paragraph style

scale

Multiplicative scaling factor, same as in ggsave

pos

where to add the new element relative to the cursor, one of "after", "before", "on".

...

Arguments to be passed to png function.

See Also

Other functions for adding content: body_add_blocks(), body_add_break(), body_add_caption(), body_add_docx(), body_add_fpar(), body_add_img(), body_add_par(), body_add_plot(), body_add_table(), body_add_toc()

Examples

if (require("ggplot2")) {
  doc <- read_docx()

  gg_plot <- ggplot(data = iris) +
    geom_point(mapping = aes(Sepal.Length, Petal.Length))

  if (capabilities(what = "png")) {
    doc <- body_add_gg(doc, value = gg_plot, style = "centered")
  }

  print(doc, target = tempfile(fileext = ".docx"))
}

officer documentation built on Oct. 10, 2024, 1:06 a.m.