ReporteRs (version 0.7.8)

addPlot.docx: Add a plot into a docx object

Description

Add a plot into the docx object.

Usage

## S3 method for class 'docx':
addPlot(doc, fun, pointsize = getOption("ReporteRs-fontsize"),
  vector.graphic = F, width = 6, height = 6,
  fontname = getOption("ReporteRs-default-font"), editable = TRUE, bookmark,
  par.properties = parProperties(text.align = "center", padding = 5), ...)

Arguments

doc
the docx to use
fun
plot function. The function will be executed to produce graphics. For grid or lattice or ggplot object, the function should just be print and an extra argument x should specify the object to plot. For traditionnal pl
pointsize
the default pointsize of plotted text in pixels, default to getOption("ReporteRs-fontsize").
vector.graphic
logical scalar, default to FALSE.

DrawingML instructions cannot be read by MS Word 2007.

width
plot width in inches (default value is 6).
height
plot height in inches (default value is 6).
fontname
the default font family to use, default to getOption("ReporteRs-default-font").
editable
logical value - if TRUE vector graphics elements (points, text, etc.) are editable.
bookmark
id of the Word bookmark to replace by the plot. optional.

bookmark is a character vector specifying bookmark id to replace by the plot(s). If provided, plot(s) will replace the paragraph that contains the bookmark. See

par.properties
paragraph formatting properties of the paragraph that contains plot(s). An object of class parProperties
...
arguments for fun.

Value

  • an object of class docx.

See Also

docx, addPlot, bookmark.

Examples

Run this code
doc.filename = "addPlot_example.docx"

# set default font size to 10
options( "ReporteRs-fontsize" = 10 )

doc = docx( )

doc = addTitle( doc, "Title example 1", level = 1 )
# Add a base plot
# set vector.graphic to FALSE if Word version
#   used to read the file is <= 2007
doc = addPlot( doc, fun = plot
  , x = rnorm( 100 ), y = rnorm (100 )
  , main = "base plot main title"
  , vector.graphic = TRUE
  , width = 5, height = 7
  , par.properties = parProperties(text.align = "left")
)


doc = addTitle( doc, "Title example 2", level = 1 )
# load ggplot2
if( requireNamespace("ggplot2", quietly = TRUE) ){

  # create a ggplot2 plot
  myplot = ggplot2::qplot(Sepal.Length, Petal.Length, data = iris
    , color = Species, size = Petal.Width, alpha = I(0.7) )

  # Add myplot into object doc
  #   myplot is assigned to argument 'x' because function 'print' on ggplot
  #   objects is expecting argument 'x'.
  doc = addPlot( doc = doc, fun = print, x = myplot )
}

# Write the object
writeDoc( doc, file = doc.filename )

Run the code above in your browser using DataCamp Workspace