The function creates a TeX expression for a sparkline from the data supplied.
sparkline(x = NULL, y = NULL, xspikes = NULL, yspikes = NULL,
xdots = NULL, ydots = NULL, dotcolor = NULL,
width = getOption("ltxsparklines.width"),
rectangle = c(NA, NA),
xlim = c(NA, NA), ylim = c(NA, NA),
clip = getOption("ltxsparklines.clip"),
na.rm = getOption("ltxsparklines.na.rm"),
bottomline = getOption("ltxsparklines.bottomline"),
bottomlinelength = NA,
bottomlinex = getOption("ltxsparklines.bottomlinex"),
startdotcolor = getOption("ltxsparklines.startdotcolor"),
enddotcolor = getOption("ltxsparklines.enddotcolor"),
output = getOption('ltxsparklines.output'))
The data for sparkline. If both x
and y
are present,
they must be vectors of the same length, and interpreted as x and y
coordinates for a sparkline. If only x
or y
is
present, then the result depends on its class:
If it is a matrix or data frame, then its first column is assumed to be the x coordinate, and its second column is assumed to be the y coordinate.
If it is a time series (either ts
or zoo
),
then it is assumed to be the y coordinate, and time(x)
is
assumed to be the x coordinate.
Otherwise it is assumed to be the y coordinate, and the
sequence 1:length(x)
is assumed to be x coordinate.
See x
.
The data for spikes: small bar chart similar to sparklines. If both
xspikes
and yspikes
are present, they are x and y
coordinates for spike. If only one is present, it is interpreted as
x
above.
See xspikes
above.
The data for dots: small dot chart similar to sparklines. If both
xdots
and ydots
are present, they are x and y
coordinates for dots. If only one is present, it is interpreted as
x
above with one addition: if xdots
is a data frame,
and has three or more columns, and dotcolor
is not set,
the third column is interpreted as
the dot color.
See ydots
above
The color of dots as a LaTeX color. A vector like
c('red', 'blue', 'green')
. If it is larger than the number
of dots, it will be truncated. If it is shorted than the number of
dots, it will be recycled.
If it is not set, and data do not provide it,
option('ltxsparklines.defaultdotcolor')
is used.
The width of the sparline in ex
of the current font. By
default 10. To change the default, use
option(ltxsparklines.width=...)
.
A vector c(min,max)
for the bottom and top of the background
rectangle (for example, to show IQR of the data). If NA
, no
rectangle is drawn.
A vector c(min,max)
of the x range of the data mapped to the
c(0,1) interval. If any of
its components is NA
, the default, the actual range is used.
A vector c(min,max)
of the y range of the data mapped to the
c(0,1) interval. If any of
its components is NA
, the default, the actual range is used.
Whether to clip the sparkline to the drawing region. The default is
not clipping. To change the default, use
option(ltxsparklines.clip=TRUE)
. You need LaTeX package at
least v1.7 for clipping.
Whether to delete NA
lines from the spark data. If
TRUE
(the default), the sparkline is drawn over the gaps in
the data. Otherwise gaps in the data lead to gaps in the line, so
it is presented as a number of chunks.
Note that continuous chunks of data must have at least two data
points to be drawn.
This parameter does not affect spikes or dots.
Whether to draw a bottom line. By default, FALSE
. To
change the default, use
option(ltxsparklines.bottomline=...)
.
You need LaTeX package at
least v1.6 for bottom lines.
The length of bottom line. If NA
(the default), the bottom
line extends to the end ot the spark rectangle.
The dimensions of the extended bottom line.
If c(NA, NA)
(the default), the extended bottom
line is not drawn. You need LaTeX package at
least v1.7 for extended bottom lines.
The color of the dot at the beginning of the spark line. If
NA
(the default), no dot is drawn. To
change the default, use
option(ltxsparklines.startdotcolor=...)
.
The color of the dot at the end of the spark line. If
NA
(the default), no dot is drawn. To
change the default, use
option(ltxsparklines.enddotcolor=...)
.
The format of the output. Sweave
uses special
processing for inline (Sexpr
) code. Thus you need to set
output
to inlineSweave
if you use Sweave
and Sexpr
. If you use knitr
or a Sweave
chunk, set output
to knitr
. This is the default, to
change it, use option(ltxsparklines.output='inlineSweave')
.
See Details below.
The function returns a string executable by TeX, to be used in
\Sexpr
, for example
Nile level changed over the years: \Sexpr{sparkline(Nile)}
.
Do not forget to add \usepackage{sparklines}
to the preamble
of your TeX document.
Most of the parameters of the sparkline are defined by the TeX code. Thus all color values must be understandable to LaTeX. For example, you can put in the TeX file
\definecolor{startdot}{named}{red}
and then use it like sparkline(Nile, startdotcolor="startdot")
.
You should use LaTeX command to change sparkline parameters, for example,
\setlength\sparklinethickness{0.3pt}
The output of sparkline
is also a TeX command, so you want to
put it inside Sexpr
or a code chunk. Note that Sweave
use a special processing for Sexpr
, so you need to set
output="inlineSweave"
for inline Sweave
output. Use
output="knitr"
for knitr
(both inline and chunks) and
Sweave
chunks.
The parameters xlim
and ylim
define the mapping between
the data and the sparkline rectangle. If there are data points
outside the rectangle, they may overplot the text. You may set
clip
to TRUE
to prevent this.
ltxsparklines-package
,
vignette{ltxsparklines}
# NOT RUN {
sparkline(x=c(1,3,6),
y=c(5,8,9),
startdotcolor="blue",
enddotcolor="red")
# }
# NOT RUN {
sparkline(Nile)
# }
Run the code above in your browser using DataLab