| tinytheme | R Documentation |
tinyplotThe tinytheme function sets or resets the theme for plots created with
tinyplot. Themes control the appearance of plots, such as text alignment,
font styles, axis labels, and even dynamic margin adjustment to reduce
whitespace.
tinytheme(
theme = c("default", "basic", "dynamic", "clean", "clean2", "bw", "linedraw",
"classic", "minimal", "ipsum", "ipsum2", "dark", "socviz", "broadsheet", "nber",
"web", "heatmap", "ridge", "ridge2", "tufte", "float", "void"),
...,
register = NULL
)
theme |
A character string specifying the name of the theme to apply. Themes are arranged in an approximate hierarchy, adding or subtracting elements in the order presented below. Note that several themes are dynamic, in the sense that they attempt to reduce whitespace in a way that is responsive to the length of axes labels, tick marks, etc. These dynamic plots are marked with an asterisk (*) below.
|
... |
Named arguments to override specific theme settings. These
arguments are passed to |
register |
Optional character string. If provided, the theme (with any
|
Sets a list of graphical parameters using tpar()
Persistent vs. ephemeral themes. Calling tinytheme("<theme>") triggers
a persistent theme, which will be applied to all subsequent graphics (incl.
base plot). To reset the theme to your default settings, call tinytheme()
without arguments. Alternatively, invoke the tinyplot(..., theme = <theme>)
argument for an ephemeral theme that is automatically reset at the end of the
plot call.
# Set a persistent theme
tinytheme("clean")
tinyplot(1:10) # uses "clean"
tinyplot(10:1) # still uses "clean"
tinytheme() # reset to default
# Use an ephemeral theme for a single plot (incl. added components)
tinyplot(1:10, theme = "dark") # uses "dark" ephemerally
tinyplot_add(type = "S") # same plot, so retains theme
tinyplot(10:1) # new plot, so back to default
Custom overrides. To customize a theme's parameters (e.g., mar, las,
etc.), either pass them directly as additional args to
tinytheme(<theme>, ...) or as a list arguments to
tinyplot(..., theme = list(<theme>, ...)).
Please note that passing overrides through tpar() or par() won't work,
because themes work by installing a persistent hook, which means that an
active theme will override any subsequent calls for parameters that the theme
controls.
# Do this
tinytheme("clean", mar = c(5, 5, 2, 2))
<some plot>
# Or this (ephemeral version)
tinyplot(..., theme = list("clean", mar = c(5, 5, 2, 2)))
# Don't do this (the theme hook will overwrite your changes)
tinytheme("clean")
tpar(mar = c(5, 5, 2, 2))
<some plot>
Spacing primitives. Dynamic themes compute mgp (margin line positions)
automatically from two spacing primitives, rather than requiring users to
reason about how mar, mgp, and tcl combine:
gap.axis: the gap in margin lines between the tick tip and the near edge
of the tick label. Default 0.2.
gap.lab: the gap in margin lines between the far edge of the tick label
and the near edge of the axis title. Default 1.0.
These primitives scale automatically with cex.axis and cex.lab, so the
visible spacing between elements remains constant regardless of text size.
To adjust spacing, pass them as overrides:
# Tighter spacing between tick labels and axis titles
tinytheme("clean", gap.lab = 0.5)
# More room between ticks and tick labels
tinytheme("clean", gap.axis = 0.5)
If you supply an explicit mgp value, it is used as-is and the primitives
are ignored.
The function returns nothing. It is called for its side effects.
tpar which does the heavy lifting under the hood;
tinytheme_get() for retrieving the name of the active theme;
tinytheme_list() for listing the names of all available themes;
tinytheme_register() for registering custom named themes.
# Reusable plot function
p = function() tinyplot(
lat ~ long | depth, data = quakes,
main = "Earthquakes off Fiji",
sub = "Data courtesy of the Harvard PRIM-H project"
)
p()
# Set a theme
tinytheme("dark")
p()
# A set theme is persistent and will apply to subsequent plots
tinyplot(0:10)
# Try a different theme
tinytheme("clean")
p()
# Customize the theme by overriding default settings
tinytheme("clean",
adj.xlab = 1, adj.ylab = 1,
cex.lab = 0.75, cex.axis = 0.9,
font.sub = 3,
gap.axis = 0, gap.lab = 0.5,
tcl = -0.1)
p()
# Another custom theme example, including a different font
tinytheme("bw", font.main = 2, col.axis = "darkcyan", family = "HersheyScript")
p()
# Aside: One or two specialized themes are only meant for certain plot types
tinytheme("ridge2")
tinyplot(I(cut(lat, 10)) ~ depth, data = quakes, type = "ridge")
# Reset the theme
tinytheme()
p()
# For an ephemeral theme, use `tinyplot(..., theme = <theme>)` directly
tinyplot(0:10, theme = "clean", main = "This theme is ephemeral")
tinyplot(10:0, main = "See, no more theme")
# Customize an ephemeral theme by passing arguments as a list
tinyplot(0:10, main = "Custom", theme = list("clean", grid.col = "hotpink"))
# Themes showcase
## We'll use a slightly more intricate plot (long y-axis labs and facets)
## to demonstrate dynamic margin adjustment etc.
thms = eval(formals(tinytheme)$theme)
for (thm in thms) {
tinytheme(thm)
tinyplot(
I(Sepal.Length*1e4) ~ Petal.Length | Species, facet = "by", data = iris,
yaxl = ",",
main = paste0('tinytheme("', thm, '")'),
sub = "A subtitle",
cap = "A caption"
)
box("outer", lty = 2)
}
# Reset
tinytheme()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.