# NOT RUN {
latexCheckOptions()
## These are the LaTeX options I use
options(latexcmd="pdflatex") ## Macintosh, Windows, linux
options(dviExtension="pdf") ## Macintosh, Windows, linux
options(xdvicmd="open") ## Macintosh, Windows, SMP linux
## or
options(xdvicmd="xdg-open") ## ubuntu linux
## See ?Hmisc::latex for discussion of these options.
# }
# NOT RUN {
## This example writes a set of pdf files and then uses the Hmisc::latex
## function to display them in LaTeX.
## The graphs are constructed three times, once each with lattice,
## base graphics, and ggplot2.
# }
# NOT RUN {
tmp <- matrix(rnorm(20), 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
# }
# NOT RUN {
## All three examples use the pdf device
## lattice example
# }
# NOT RUN {
library(lattice)
tmp.lattice <- bwplot(group ~ y | group, data=tmp.df, layout=c(1,2))
tmp.lattice
pdf("tmpl%03d.pdf", onefile=FALSE, height=.4, width=4) ## inch
update(tmp.lattice, layout=c(1,1), xlab=NULL, ylab=NULL,
par.settings=list(layout.heights=layoutHeightsCollapse(),
layout.widths=layoutWidthsCollapse(),
axis.line=list(col="transparent")),
scales=list(y=list(relation="free", at=NULL)))
dev.off()
tmpl.graphnames <- paste0("tmpl", sprintf("%03i", 1:2), ".pdf")
tmpl.display <- data.frame(round(tmp, 2),
graphs=as.includegraphics(tmpl.graphnames, raise="-.55ex"))
tmpl.display
tmpl.latex <- Hmisc::latex(tmpl.display)
tmpl.latex$style <- "graphicx"
tmpl.latex ## this line requires latex in the PATH, and Hmisc to be loaded
## Hmisc::print.latex(tmpl.latex) ## if Hmisc is not loaded
# }
# NOT RUN {
## base graphics example
# }
# NOT RUN {
pdf("tmpb%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 is correct for horizontal plot
boxplot(tmp["B",], horizontal=TRUE, ylim=range(tmp)) ## ylim is correct for horizontal plot
dev.off()
tmpb.graphnames <- paste0("tmpb", sprintf("%03i", 1:2), ".pdf")
tmpb.display <-
data.frame(round(tmp, 2),
graphs=as.includegraphics(tmpb.graphnames, height="2em", raise="-1.4ex"))
tmpb.display
tmpb.latex <- Hmisc::latex(tmpb.display)
tmpb.latex$style <- "graphicx"
tmpb.latex ## this line requires latex in the PATH, and Hmisc to be loaded
## Hmisc::print.latex(tmpb.latex) ## if Hmisc is not loaded
# }
# NOT RUN {
## ggplot2 example, whole set constructed as a unit, then printed one panel at a time.
# }
# NOT RUN {
library(ggplot2)
tmpga <-
ggplot(tmp.df, aes(group, y)) +
geom_boxplot(outlier.size = 8) +
coord_flip() +
theme_collapse()
tmpga ## on interactive device
pdf("tmpga%03d.pdf", onefile=FALSE, height=1, width=4) ## inch
for (i in 1:length(levels(tmp.df$group))) {
tmpga$coordinates$limits$x <- c(i, i) ## I dislike this usage.
## I want to use "+" with some ggplot2 function,
## but have not figured out the right incantation.
print(tmpga)
}
dev.off()
graphnames <- paste0("tmpga", sprintf("%03i", 1:2), ".pdf")
tmpga.display <- data.frame(round(tmp, 2),
graphs=as.includegraphics(graphnames, raise="-.7ex"))
tmpga.display
tmpga.latex <- Hmisc::latex(tmpga.display)
tmpga.latex$style <- "graphicx"
tmpga.latex ## this line requires latex in the PATH, and Hmisc to be loaded
## Hmisc::print.latex(tmpga.latex) ## if Hmisc is not loaded
# }
# NOT RUN {
## ggplot2 example, constructed one panel at a time.
# }
# NOT RUN {
library(ggplot2)
pdf("tmpgb%03d.pdf", onefile=FALSE, height=1, width=4) ## inch
ggplot(tmp.df[1:5,], aes(group, y)) +
geom_boxplot(outlier.size = 8) + ylim(range(tmp.df[,1])) +
coord_flip() +
theme_collapse()
ggplot(tmp.df[6:10,], aes(group, y)) +
geom_boxplot() + ylim(range(tmp.df[,1])) +
coord_flip() +
theme_collapse()
dev.off()
graphnames <- paste0("tmpgb", sprintf("%03i", 1:2), ".pdf")
tmpgb.display <- data.frame(round(tmp, 2),
graphs=as.includegraphics(graphnames, raise="-.7ex"))
tmpgb.display
tmpgb.latex <- Hmisc::latex(tmpgb.display)
tmpgb.latex$style <- "graphicx"
tmpgb.latex ## this line requires latex in the PATH, and Hmisc to be loaded
## Hmisc::print.latex(tmpgb.latex) ## if Hmisc is not loaded
# }
# NOT RUN {
## xtable example
# }
# NOT RUN {
tmpl.display ## from lattice example above
tmplx.name <- "tmpldisplayxtable.tex"
print(xtable::xtable(tmpl.display),
caption.placement = "top",
sanitize.text.function = function(x) x, ## xtable converts "\abc" to "$\backslash$abc"
file=tmplx.name) ## sanitize restores it back to "\abc".
tmpx.latex <- list(file=tmplx.name, style="graphicx")
class(tmpx.latex) <- "latex"
tmpx.latex ## this line requires latex in the PATH, and Hmisc to be loaded
## Hmisc::print.latex(tmpx.latex) ## if Hmisc is not loaded
# }
# NOT RUN {
## Please see the demos for more interesting examples.
## demo(package="microplot")
# }
Run the code above in your browser using DataLab