Learn R Programming

openxlsx (version 1.0.3)

insertPlot: Insert the current plot into a worksheet

Description

The current plot is saved to a temporary image file using dev.copy. This file is then written to the workbook using insertImage.

Usage

insertPlot(wb, sheet, width = 6, height = 4, xy = NULL, startRow = 1,
  startCol = 1, fileType = "png", units = "in", dpi = 300)

Arguments

wb
A workbook object
sheet
A name or index of a worksheet
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.
xy
Alternate way to specify startRow and startCol. A vector of length 2 of form (startcol, startRow)
width
Width of figure. Defaults to 6in.
height
Height of figure . Defaults to 4in.
fileType
File type of image
units
Units of width and height. Can be "in", "cm" or "px"
dpi
Image resolution

See Also

insertImage

Examples

Run this code
## Create a new workbook
wb <- createWorkbook()

## Add a worksheet
addWorksheet(wb, "Sheet 1")

## create a plots
require(ggplot2)
p1 <- ggplot2::qplot(mpg, data=mtcars, geom="density",
  fill=as.factor(gear), alpha=I(.5), main="Distribution of Gas Milage")
p2 <- ggplot2::qplot(age, circumference,
  data = Orange, geom = c("point", "line"), colour = Tree)

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

##
p2
insertPlot(wb, 1, xy = c("J", 2), width = 16, height = 10,  fileType = "png", units = "cm")

## Save workbook
saveWorkbook(wb, "insertPlotExample.xlsx", overwrite = TRUE)

Run the code above in your browser using DataLab