Learn R Programming

openxlsx2 (version 0.6.1)

wb_add_plot: Insert the current plot into a worksheet

Description

The current plot is saved to a temporary image file using grDevices::dev.copy() This file is then written to the workbook using wb_add_image().

Usage

wb_add_plot(
  wb,
  sheet = current_sheet(),
  width = 6,
  height = 4,
  xy = NULL,
  startRow = 1,
  startCol = 1,
  rowOffset = 0,
  colOffset = 0,
  fileType = "png",
  units = "in",
  dpi = 300,
  dims = rowcol_to_dim(startRow, startCol)
)

Arguments

wb

A workbook object

sheet

A name or index of a worksheet

width

Width of figure. Defaults to 6in.

height

Height of figure . Defaults to 4in.

xy

Alternate way to specify startRow and startCol. A vector of length 2 of form (startcol, startRow)

startRow

Row coordinate of upper left corner of figure. xy[[2]] when xy is given.

startCol

Column coordinate of upper left corner of figure. xy[[1]] when xy is given.

rowOffset

offset within cell (row)

colOffset

offset within cell (column)

fileType

File type of image

units

Units of width and height. Can be "in", "cm" or "px"

dpi

Image resolution

dims

Worksheet dimension, single cell ("A1") or cell range ("A1:D4")

See Also

wb_add_image()

Examples

Run this code
if (requireNamespace("ggplot2") && interactive()) {
## Create a new workbook
wb <- wb_workbook()

## Add a worksheet
wb$add_worksheet("Sheet 1", gridLines = FALSE)

## create plot objects
require(ggplot2)
p1 <- ggplot(mtcars, aes(x = mpg, fill = as.factor(gear))) +
  ggtitle("Distribution of Gas Mileage") +
  geom_density(alpha = I(.5))
p2 <- ggplot(Orange, aes(x = age, y = circumference, color = Tree)) +
  geom_point() + geom_line()

## Insert currently displayed plot to sheet 1, row 1, column 1
print(p1) # plot needs to be showing
wb$add_plot(1, width = 5, height = 3.5, fileType = "png", units = "in")

## Insert plot 2
print(p2)
wb$add_plot(1, dims = "J2", width = 16, height = 10, fileType = "png", units = "cm")

}

Run the code above in your browser using DataLab