Skip to contents

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",
  unit = "in",
  ...
)

Arguments

x

an rdocx object

value

ggplot object

width, height

plot size in units expressed by the unit argument. Defaults to a width of 6 and a height of 5 "in"ches.

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".

unit

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

...

Arguments to be passed to png function.

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")

    # Set the unit in which the width and height arguments are expressed
    doc <- body_add_gg(doc, value = gg_plot, style = "centered", unit = "cm")
  }

  print(doc, target = tempfile(fileext = ".docx"))
}
#> Loading required package: ggplot2