microplot-package {microplot} | R Documentation |
Display microplots (sparklines) from R graphics panels in tables in 'LaTeX', 'Word', 'HTML', 'Excel'.
Description
The microplot function writes a set of R graphics files to be used as microplots (sparklines) in tables in either 'LaTeX', 'HTML', 'Word', or 'Excel' files. For 'LaTeX', we provide methods for the Hmisc::latex() generic function to construct 'latex' tabular environments which include the graphs. These can be used directly with the operating system 'pdflatex' or 'latex' command, or by using one of 'Sweave', 'knitr', 'rmarkdown', or 'Emacs org-mode' as an intermediary. For 'MS Word', the msWord() function uses the 'flextable' package to construct 'Word' tables which include the graphs. There are several distinct approaches for constructing HTML files. The simplest is to use the msWord() function with argument filetype="html". Alternatively, use either 'Emacs org-mode' or the htmlTable::htmlTable() function to construct an 'HTML' file containing tables which include the graphs. See the documentation for our as.htmlimg() function. For 'Excel' use on 'Windows', the file examples/irisExcel.xls includes 'VBA' code which brings the individual panels into individual cells in the spreadsheet. Examples in the examples and demo subdirectories are shown with 'lattice' graphics, 'ggplot2' graphics, and 'base' graphics. Examples for 'LaTeX' include 'Sweave' (both 'LaTeX'-style and 'Noweb'-style), 'knitr', 'emacs org-mode', and 'rmarkdown' input files and their 'pdf' output files. Examples for 'HTML' include 'org-mode' and 'Rmd' input files and their webarchive 'HTML' output files. In addition, the as.orgtable() function can display a data.frame in an 'org-mode' document. The examples for 'MS Word' (with either filetype="docx" or filetype="html") work with all operating systems. The package does not require the installation of 'LaTeX' or 'MS Word' to be able to write '.tex' or '.docx' files.
Details
The DESCRIPTION file:
Package: | microplot |
Type: | Package |
Title: | Microplots (Sparklines) in 'LaTeX', 'Word', 'HTML', 'Excel' |
Version: | 1.0-45 |
Date: | 2022-01-26 |
Author: | Richard M. Heiberger, with contributions from Karen Byron and Nooreen Dabbish. |
Maintainer: | Richard M. Heiberger <rmh@temple.edu> |
Description: | The microplot function writes a set of R graphics files to be used as microplots (sparklines) in tables in either 'LaTeX', 'HTML', 'Word', or 'Excel' files. For 'LaTeX', we provide methods for the Hmisc::latex() generic function to construct 'latex' tabular environments which include the graphs. These can be used directly with the operating system 'pdflatex' or 'latex' command, or by using one of 'Sweave', 'knitr', 'rmarkdown', or 'Emacs org-mode' as an intermediary. For 'MS Word', the msWord() function uses the 'flextable' package to construct 'Word' tables which include the graphs. There are several distinct approaches for constructing HTML files. The simplest is to use the msWord() function with argument filetype="html". Alternatively, use either 'Emacs org-mode' or the htmlTable::htmlTable() function to construct an 'HTML' file containing tables which include the graphs. See the documentation for our as.htmlimg() function. For 'Excel' use on 'Windows', the file examples/irisExcel.xls includes 'VBA' code which brings the individual panels into individual cells in the spreadsheet. Examples in the examples and demo subdirectories are shown with 'lattice' graphics, 'ggplot2' graphics, and 'base' graphics. Examples for 'LaTeX' include 'Sweave' (both 'LaTeX'-style and 'Noweb'-style), 'knitr', 'emacs org-mode', and 'rmarkdown' input files and their 'pdf' output files. Examples for 'HTML' include 'org-mode' and 'Rmd' input files and their webarchive 'HTML' output files. In addition, the as.orgtable() function can display a data.frame in an 'org-mode' document. The examples for 'MS Word' (with either filetype="docx" or filetype="html") work with all operating systems. The package does not require the installation of 'LaTeX' or 'MS Word' to be able to write '.tex' or '.docx' files. |
Imports: | Hmisc (>= 4.1-1), HH, lattice, grid, flextable (>= 0.5-11), officer, ggplot2, htmltools, cowplot |
Suggests: | reshape2, latticeExtra, xtable, markdown, rmarkdown, knitr, htmlTable |
License: | GPL (>= 2) |
Index of help topics:
LegendrePolyMatrices Legendre Orthogonal Polynomials for various values of alpha and beta. The dataset is used in the demo("LegendrePolynomials"). as.htmlimg Place a filename or filepath in the format used by HTML as.includegraphics Convert a filename into a complete 'LaTeX' '\includegraphics' expression for use with 'LaTeX' '\includegraphics' macro in the 'graphicx' package. This is used for 'pdf' and 'png' files with the system 'pdflatex' command. This is used for 'ps' files with the system 'latex' command. as.orgfile Place a filename or filepath in the format used by org-mode as.orgtable Prepare a matrix or data.frame to be used as an org-mode table cc176.y.adj Adjusted response values and their five number summaries by treatment level for one model using the cc176 dataset. This dataset is used in two demos: "bwplot" and "boxplot-ggplot". dir.verify Verifies existence of, or creates, a directory. formatDF Format a Data Frame or Matrix for LaTeX or HTML. graphicsList Convert a list of "trellis" objects or list of "ggplot" objects into a "graphicslist" object. latex.AEdotplot Display the AE (Adverse Events) dotplot of incidence and relative risk from the HH package in a 'LaTeX' tabular environment or in an 'MS Word' or an 'HTML' table. latex.trellis Display a table in 'latex' containing panels from R graphs in its cells. latexSetOptions Set the options for use of latex; check whether the options for latex functions have been specified. layoutCollapse Set the lattice 'par.settings' to remove all marginal space. microplot Take a trellis or ggplot object, or graphicsList object (list of trellis or ggplot objects), and generate a set of graphics files, one per panel of a multi-panel display. microplot-package Display microplots (sparklines) from R graphics panels in tables in 'LaTeX', 'Word', 'HTML', 'Excel'. microplotAttrDisplay Specify how to display the microplots for x.axis, y.axis, xlab, ylab, and key. msWord Display a table in 'MS Word' containing panels from R graphs in its cells. show.latexConsole Revisions of Hmisc latex and dvi functions that display the generated latex file on screen and divert the console log to a file. New print methods that display Operating System files (ps, docx, html) on screen. theme_collapse Set the 'ggplot2' theme to remove all marginal space. toxicity Simulated toxicity data. Dataset is used in demo("tablesPlusGraphicColumn").
Microplots are small plots that fit into the cells of a table that otherwise consists of text and numbers. A special case of a microplot is known as a sparkline.
The examples in this package show tables of simple or complex graphs placed into one or more columns of a table. The graphs can be produced by any graphical system in 'R'. We show lattice, ggplot2, and base graphics. The tables can be targeted for display in 'LaTeX', 'MS Word' on any operating system, 'MS Excel' on 'Windows', or 'HTML'. We show examples of each.
The functions produce valid 'LaTeX' .tex
files or 'Word'
.docx
files in the working directory. If 'LaTeX' or 'Word' are
installed, then the generated files can be displayed on screen as
illustrated in the help file examples and the demo
directory. The
.tex
files can be \include
d in a larger .tex
file.
Or the generated .pdf
file can be displayed in ‘LaTeX’ with
an \includegraphics
statement.
The images in the displayed .docx
file can be copied and pasted
into a larger 'Word' file.
The best way to learn this package is to read the examples and demo
files.
The primary function microplot
takes a trellis
or ggplot
object and generates a set of graphics files, one per panel of a
multi-panel display. The latex
and msWprd
functions place
the graphics files into a table.
The latex
examples (in the help file examples
) and demo
files use the operating system pdflatex
command with the 'R'
pdf()
or png()
graphics device. Or they could use the
operating system latex
command with the 'R'
ps()
graphics device. They therefore require that the
three options
options()[c("latexcmd","dviExtension","xdvicmd")]
all be set consistently.
The recommended settings for pdflatex
with pdf
graphics files
are included as the defaults in the function call
latexSetOptions()
The recommended settings for latex
with ps
graphics files
may be specified with the function call
latexSetOptions("latex")
Please see latexSetOptions
for details on the recommended
settings
for use with the microplot package. See the "System options"
section in the "Details" section of
latex
for discussion of the options themselves.
The examples in this help
file are inside dontrun
environments
because they depend on options
and they write files. You must set the
options
for your system before running the example manually.
Most of the 'LaTeX' examples are shown using the Hmisc::latex
function
latex
(I am coauthor of that function).
The microplot package also works with the xtable::xtable
function xtable
.
An example in the demo
directory shows a simple use of xtable
.
The demos in the demo
directory are not inside a dontrun
environment. You must set the options for your system before running
them. Each demo sets the options for pdfltex
. Should you prefer
latex
you will need to run the demos manually.
I recommend that you run the demos with ask=TRUE
because
will need to read them closely to see what they are doing.
To run the demos manually (with a stop at each graph), use
demo("HowToUseMicroplot" , package="microplot", ask=TRUE) demo("latex" , package="microplot", ask=TRUE) demo("latex-ggplot" , package="microplot", ask=TRUE) demo("msWord" , package="microplot", ask=TRUE) demo("LegendrePolynomials" , package="microplot", ask=TRUE) demo("timeseries" , package="microplot", ask=TRUE) demo("NTplot" , package="microplot", ask=TRUE) demo("bwplot-lattice" , package="microplot", ask=TRUE) demo("boxplot-ggplot" , package="microplot", ask=TRUE) demo("tablesPlusGraphicColumn" , package="microplot", ask=TRUE) demo("regrcoef" , package="microplot", ask=TRUE) demo("iris" , package="microplot", ask=TRUE) demo("AEdotplot" , package="microplot", ask=TRUE) demo("xtable" , package="microplot", ask=TRUE) demo("Examples" , package="microplot", ask=TRUE)
To run the demos automatically, with no stops, use ask=FALSE
.
The examples
directory
system.file(package="microplot", "examples")
includes complete working examples of Sweave (both LaTeX-style and
Noweb-style), knitr, emacs
orgmode, and
rmarkdown
input files and their pdf
output files. These files must be
copied into a directory in which you have write privilege, and that
directory must be made the current working directory with setwd
.
They will not work from the installed package directory.
The 'Excel' for Windows example is in file
examples/irisExcel.xls
. The 'VBA' code in that file shows how to
place the individual microplots into 'Excel' cells.
See also the vignette:
vignette("rmhPoster", package="microplot")
Author(s)
Richard M. Heiberger, with contributions from Karen Byron and Nooreen Dabbish.
Maintainer: Richard M. Heiberger <rmh@temple.edu>
See Also
latex
, latex.trellis
, microplot
, as.includegraphics
Examples
## 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.
## End(Not run)
## Please see the demos for more interesting examples.
## demo(package="microplot")