type_spineplot {tinyplot} | R Documentation |
Spineplot and spinogram types
Description
Type function(s) for producing spineplots and spinograms, which
are modified versions of histograms or mosaic plots, and particularly
useful for visualizing factor variables. Note that tinyplot
defaults
to type_spineplot()
if y
is a factor variable.
Usage
type_spineplot(
breaks = NULL,
tol.ylab = 0.05,
off = NULL,
xlevels = NULL,
ylevels = NULL,
col = NULL,
xaxlabels = NULL,
yaxlabels = NULL,
weights = NULL
)
Arguments
breaks |
if the explanatory variable is numeric, this controls how
it is discretized. |
tol.ylab |
convenience tolerance parameter for y-axis annotation. If the distance between two labels drops under this threshold, they are plotted equidistantly. |
off |
vertical offset between the bars (in per cent). It is fixed to
|
xlevels , ylevels |
a character or numeric vector specifying the ordering of the
levels of the |
col |
a vector of fill colors of the same length as |
xaxlabels , yaxlabels |
character vectors for annotation of x and y axis.
Default to |
weights |
numeric. A vector of frequency weights for each
observation in the data. If |
Examples
# "spineplot" type convenience string
tinyplot(Species ~ Sepal.Width, data = iris, type = "spineplot")
# Aside: specifying the type is redundant for this example, since tinyplot()
# defaults to "spineplot" if y is a factor (just like base plot).
tinyplot(Species ~ Sepal.Width, data = iris)
# Use `type_spineplot()` to pass extra arguments for customization
tinyplot(Species ~ Sepal.Width, data = iris, type = type_spineplot(breaks = 4))
p = palette.colors(3, "Pastel 1")
tinyplot(Species ~ Sepal.Width, data = iris, type = type_spineplot(breaks = 4, col = p))
rm(p)
# More idiomatic tinyplot way of drawing the previous plot: use y == by
tinyplot(
Species ~ Sepal.Width | Species, data = iris, type = type_spineplot(breaks = 4),
palette = "Pastel 1", legend = FALSE
)
# Grouped and faceted spineplots
ttnc = as.data.frame(Titanic)
tinyplot(
Survived ~ Sex, facet = ~ Class, data = ttnc,
type = type_spineplot(weights = ttnc$Freq)
)
# For grouped "by" spineplots, it's better visually to facet as well
tinyplot(
Survived ~ Sex | Class, facet = "by", data = ttnc,
type = type_spineplot(weights = ttnc$Freq)
)
# Fancier version. Note the smart inheritance of spacing etc.
tinyplot(
Survived ~ Sex | Class, facet = "by", data = ttnc,
type = type_spineplot(weights = ttnc$Freq),
palette = "Dark 2", facet.args = list(nrow = 1), axes = "t"
)
# Reorder x and y variable categories either by their character levels or numeric indexes
tinyplot(
Survived ~ Sex, facet = ~ Class, data = ttnc,
type = type_spineplot(weights = ttnc$Freq, xlevels = c("Female", "Male"), ylevels = 2:1)
)
# Note: It's possible to use "by" on its own (without faceting), but the
# overlaid result isn't great. We will likely overhaul this behaviour in a
# future version of tinyplot...
tinyplot(Survived ~ Sex | Class, data = ttnc,
type = type_spineplot(weights = ttnc$Freq), alpha = 0.3
)