# NOT RUN {
## This example writes a set of pdf files and then uses the latex
## function to display them in LaTeX.
## The graphs are constructed three times, once each with lattice,
## base graphics, and ggplot2.
# }
# NOT RUN {
## 0. set options for pdflatex and pdf graphics files
latexSetOptions()
## 1. define dataset
tmp <- matrix(rnorm(10), 2, 5, byrow=TRUE,
dimnames=list(c("A", "B"), paste0("X", 1:5)))
tmp.df <- data.frame(y=as.vector(t(tmp)),
group=factor(rep(row.names(tmp), each=5)))
tmp.df
## 2. lattice example for latex and msWord
tmp.lattice <- lattice::bwplot(group ~ y | " " * group, data=tmp.df, layout=c(1,2),
as.table=TRUE, xlim=c(-2.1, 1.3),
scales=list(y=list(relation="free", at=NULL)))
tmp.lattice
## 3. using the latex.trellis method
latex(tmp.lattice,
height.panel=.3, width.panel=3, ## inches
x.axis=TRUE, y.axis=FALSE,
rowlabel="group", caption="latex.trellis of lattice graph column")
latex(tmp.lattice, dataobject=formatDF(tmp, dec=2),
height.panel=.3, width.panel=1.5, ## inches
x.axis=FALSE, y.axis=FALSE,
rowlabel="group", caption="latex.trellis of numeric data and lattice graph column")
## 4. MS Word example. Uses functions in the flextable and officer packages.
tmplw.docx <-
msWord(tmp.lattice, dataobject=format(tmp, digits=2),
height.panel=.3, width.panel=2, ## inches
height.x.axis=.35, width.y.axis=.3,
figPrefix="tmplw",
y.axis=FALSE,
rowlabel="group", width.rowname=.6,
data.header="data values", width.dataobject=.6,
graph.header="bwplot",
caption="Lattice bwplot using msWord function")
print.default(tmplw.docx)
tmplw.docx ## print method opens file
## cut and paste this graph into a larger .docx file.
## 5. ggplot2 example for latex and msWord
library(ggplot2)
tmpga.df <- cbind(tmp.df, fake="ff")
tmpga <-
ggplot(tmpga.df, aes(fake, y)) +
geom_boxplot(outlier.size = 2) +
facet_wrap(~ group, ncol=1) +
coord_flip() + ylim(-2, 1.1)
tmpga ## on interactive device
## 6. using the latex.ggplot method
latex(tmpga, height.x.axis=.2, width.y.axis=.2, y.axis=FALSE)
latex(tmpga, dataobject=formatDF(tmp, dec=2),
height.panel=.5, height.x.axis=.2, width.y.axis=.2, y.axis=FALSE)
## 7. msWord with ggplot
tmpga.docx <-
msWord(tmpga, dataobject=format(tmp, digits=2),
height.panel=.25, height.x.axis=.2, width.y.axis=.2, y.axis=FALSE,
rowlabel="group", width.rowname=.6,
data.header="data values", width.dataobject=.6,
graph.header="bwplot",
caption="ggplot2 boxplot using msWord function")
print.default(tmpga.docx)
tmpga.docx ## print method opens file
detach("package:ggplot2")
## 8. base graphics example
## This must be done with an explicit loop because
## base graphics doesn't produce a graphics object.
dir.verify("tmpb")
pdf("tmpb/fig%03d.pdf", onefile=FALSE, height=.5, width=3) ## inch
par( bty="n", xaxt="n", omd=c(0,1, 0,1), mai=c(0,0,0,0))
boxplot(tmp["A",], horizontal=TRUE, ylim=range(tmp)) ## ylim for horizontal plot
boxplot(tmp["B",], horizontal=TRUE, ylim=range(tmp)) ## ylim for horizontal plot
dev.off()
tmpb.graphnames <- paste0("tmpb/fig", sprintf("%03i", 1:2), ".pdf")
tmpb.display <-
data.frame(round(tmp, 2),
graphs=as.includegraphics(tmpb.graphnames, height="2em", raise="-1.4ex"))
tmpb.display
## we are now using the latex.data.frame method in the Hmisc package
tmpb.latex <- latex(tmpb.display, rowlabel="group",
caption="latex.default of base graphs")
tmpb.latex$style <- "graphicx"
tmpb.latex ## this line requires latex in the PATH
## 9. detail for latex of lattice. This is essentially what the
## latex.trellis method does all together.
dir.verify("tmpl") ## create a new subdirectory of the working directory
pdf("tmpl/fig%03d.pdf", onefile=FALSE, height=.5, width=2.5) ## inch
update(tmp.lattice, layout=c(1,1), xlab="",
par.settings=list(layout.heights=layoutHeightsCollapse(),
layout.widths=layoutWidthsCollapse(),
axis.line=list(col="transparent"),
strip.border=list(col="transparent")))
dev.off()
tmpl.graphnames <- paste0("tmpl/fig", sprintf("%03i", 1:2), ".pdf")
names(tmpl.graphnames) <- rownames(tmp)
tmpl <-
as.includegraphics(tmpl.graphnames)
## retains dimensions from pdf() statement
tmpl
tmpl.latex <- latex(tmpl, rowlabel="group",
caption="latex.default of lattice graph column")
tmpl.latex ## this line requires latex in the PATH
tmplw <-
data.frame(round(tmp, 2),
graphs=as.includegraphics(tmpl.graphnames, width="1in"))
## retains aspect ratio from pdf() statement
tmplw
tmplw.latex <- latex(tmplw, rowlabel="group",
caption="latex.default of numeric data and lattice graph column")
tmplw.latex$style <- "graphicx"
tmplw.latex ## this line requires latex in the PATH
## 10. detail for latex of ggplot.
## left as an exercise. It is very similar to the detail for latex with lattice.
# }
# NOT RUN {
## Please see the demos for more interesting examples.
## demo(package="microplot")
# }
Run the code above in your browser using DataLab