library("tinyplot")
# Basic examples of frequency tables (without y variable)
tinyplot(~ cyl, data = mtcars, type = "barplot")
tinyplot(~ cyl | vs, data = mtcars, type = "barplot")
tinyplot(~ cyl | vs, data = mtcars, type = "barplot", beside = TRUE)
tinyplot(~ cyl | vs, data = mtcars, type = "barplot", beside = TRUE, fill = 0.2)
# Reorder x variable categories either by their character levels or numeric indexes
tinyplot(~ cyl, data = mtcars, type = "barplot", xlevels = c("8", "6", "4"))
tinyplot(~ cyl, data = mtcars, type = "barplot", xlevels = 3:1)
# Note: Above we used automatic argument passing for `beside`. But this
# wouldn't work for `width`, since it would conflict with the top-level
# `tinyplot(..., width = <width>)` argument. It's safer to pass these args
# through the `type_barplot()` functional equivalent.
tinyplot(
~ cyl | vs, data = mtcars, fill = 0.2,
type = type_barplot(beside = TRUE, drop.zeros = TRUE, width = 0.65)
)
# Example for numeric y aggregated by x (default: FUN = mean) + facets
tinyplot(
extra ~ ID | group, facet = "by", data = sleep,
type = "barplot", fill = 0.6,
theme = "clean2"
)
# Fancy frequency table:
tinyplot(
Freq ~ Sex | Survived, data = as.data.frame(Titanic),
facet = ~ Class, facet.args = list(nrow = 1),
type = "barplot", flip = TRUE, fill = 0.6,
theme = "clean2"
)
# Centered barplot for conditional proportions of hair color (black/brown vs.
# red/blond) given eye color and sex
hec = as.data.frame(proportions(HairEyeColor, 2:3))
hcols = c("black", "sienna", "indianred", "goldenrod")
tinyplot(
Freq ~ Eye | Hair, data = hec,
facet = ~ Sex, facet.args = list(ncol = 1),
type = "barplot", center = TRUE,
flip = TRUE, yaxl = "percent",
theme = list("clean2", palette.qualitative = hcols)
)
# Use cases for the `offset` argument
# 1. Waterfall plot
d = data.frame(item = c("Sales", "Services", "Costs", "Returns", "TOTAL"),
value = c(100, 40, -80, -10, 50))
d$item = factor(d$item, levels = d$item)
d$offset = c(0, cumsum(d$value[1:3]), 0)
tinyplot(
value ~ item | I(value < 0), data = d,
type = type_barplot(offset = d$offset), legend = FALSE
)
tinyplot_add(type = type_vline(4.5), lty = 2)
# 2. Diverging/Likert layout: a character (or named numeric) offset "sets
# aside" the named category, pulling it out of the centered stack and drawing
# it as a standalone bar. Here a neutral "Unsure" response is shown apart from
# the diverging agree/disagree scale.
lik = expand.grid(
question = c("Pay", "Workload", "Manager", "Culture"),
response = c("Strong disagree", "Disagree", "Agree", "Strong agree", "Unsure")
)
lik$response = factor(lik$response, levels = unique(lik$response))
lik$share = c( # proportions summing to 1 within each question
.10, .25, .05, .15,
.20, .30, .15, .20,
.35, .20, .40, .30,
.25, .15, .35, .20,
.10, .10, .05, .15
)
# diverging palette: reds (disagree) -> blues (agree), grey for "Unsure"
pal = c("#b2182b", "#ef8a62", "#67a9cf", "#2166ac", "grey")
tinyplot(
share ~ question | response, data = lik,
type = "barplot", center = TRUE, offset = "Unsure",
flip = TRUE, xlab = NA, ylab = NA, yaxl = "percent",
legend = list("top!", title = NULL),
theme = list("clean2", palette.qualitative = pal),
main = "Hypothetical Likert example with category offset"
)
tinyplot_add(type = "vline")