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 DataLab