addPlot-methods: Insert a plot into the docx document

Description Usage Arguments Details Examples

Description

Insert a plot into the Docx object.

Usage

1
2
3
  ## S4 method for signature 
## 'Docx,function,numeric,numeric,character,character,character,ANY'
addPlot(doc, fun, width, height, legend, stylename, bookmark, ...)

Arguments

doc

the Docx to use

fun

plot function

width

plot width in inches (default value is 7).

height

plot height in inches (default value is 6).

legend

optional character string, legend to add below the plot

stylename

name of the style to apply to the legend text (default value is "PlotReference" - a style available in the default base document.).

bookmark

a character value ; id of the Word bookmark to replace by the plot. optional

...

arguments for fun.

Details

This function work with base R graphics. Plot parameters are specified with the ... argument. However, the most convenient usage is to wrap the plot code into a function whose parameters will be specified as '...'.

If you want to add ggplot2 or lattice plot, use print function. See examples for more details.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
docx.file = "Hello_Word.docx"
require(R2DOCX)
# Create a new document 
doc <- new("Docx", title = "title" )
# Add a base plot
doc = addPlot( doc
		, fun = plot
		, x = rnorm( 100 )
		, y = rnorm (100 )
		, main = "base plot main title"
	)

## Add a ggplot2
## Not run: 
require(ggplot2)
# method 1 :
doc = addPlot( doc
		, function(){
			print( qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7)) )
		}
		, width = 8, height = 7, legend="graph example" 
		, stylename = "PlotReference"
	)

# method 2 :
myplot = qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))
doc = addPlot( doc
		, print
		, width = 8, height = 7, legend="graph example" 
		, stylename = "PlotReference"
		, x = myplot #this argument MUST be named, print is expecting argument 'x'
	)

writeDoc( doc, docx.file )

## End(Not run)

davidgohel/R2DOCX documentation built on May 14, 2019, 11:13 p.m.