tinytheme("basic") # filled points & background grid (but not dynamic yet)
#
## Basic coefficient plot(s)
mod = lm(mpg ~ wt * factor(am), mtcars)
coefs = data.frame(names(coef(mod)), coef(mod), confint(mod))
colnames(coefs) = c("term", "est", "lwr", "upr")
# "errorbar" and "pointrange" type convenience strings
tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "errorbar")
tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "pointrange")
# Use `type_errorbar()` to pass extra arguments for customization
tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs,
type = type_errorbar(length = 0.2))
#
## Flipped plots
# For flipped errobar / pointrange plots, it is recommended to use a
# *dynamic* theme for horizontal axis tick labels + appropriate spacing
tinytheme("classic") # or "clean(2)", "bw", "socviz", "float", ...
tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "errorbar",
flip = TRUE)
tinyplot_add(type = "hline", lty = 2) # "hline" b/c flip = TRUE (not vline!)
tinytheme("basic") # back to basic theme for the remaining examples
#
## Dodging groups
models = list(
"Model A" = lm(mpg ~ wt, data = mtcars),
"Model B" = lm(mpg ~ wt + cyl, data = mtcars),
"Model C" = lm(mpg ~ wt + cyl + hp, data = mtcars)
)
models = do.call(
rbind,
lapply(names(models), function(m) {
data.frame(
model = m,
term = names(coef(models[[m]])),
estimate = coef(models[[m]]),
setNames(data.frame(confint(models[[m]])), c("conf.low", "conf.high"))
)
})
)
tinyplot(estimate ~ term | model,
ymin = conf.low, ymax = conf.high,
data = models,
type = type_pointrange(dodge = 0.1))
# Aside 1: relative vs fixed dodge
# The default dodge position is based on the unique groups (here: models)
# available to each x value (here: coefficient term). To "fix" the dodge
# position across all x values, use `fixed.dodge = TRUE`.
tinyplot(estimate ~ term | model,
ymin = conf.low, ymax = conf.high,
data = models,
type = type_pointrange(dodge = 0.1, fixed.dodge = TRUE))
# Aside 2: layering
# For layering on top of dodged plots, rather pass the dodging arguments
# through the top-level call if you'd like the dodging behaviour to be
# inherited automatically by the added layers.
tinyplot(estimate ~ term | model,
ymin = conf.low, ymax = conf.high,
data = models,
type = "pointrange",
dodge = 0.1, fixed.dodge = TRUE)
tinyplot_add(type = "l", lty = 2)
#
## Handling (long/overlapping) tick labels
# You may face the annoyance of long and/or overlapping tick labels, e.g.
mod2 = lm(mpg ~ 0 + factor(cyl) * factor(am), mtcars)
coefs2 = data.frame(names(coef(mod2)), coef(mod2), confint(mod2))
colnames(coefs2) = c("term", "est", "lwr", "upr")
# (re-usable plot function)
demo_plot = function(...) {
tinyplot(
est ~ term, ymin = lwr, ymax = upr,
data = coefs2,
type = "errorbar",
xlab = NA,
ylab = "MPG (miles per gallon)",
main = "Impact on fuel efficiency",
...
)
}
demo_plot()
# Here are some useful arguments (strategies) to avoid this annoyance...
# 1) dynamic theme + rotated x-labels
demo_plot(theme = "clean", xaxr = 45)
# 2) dynamic theme + labeller function (here: dictionary w/ newline spacing)
dict = c(
"factor(cyl)4" = "Manual\n4 Cyclinders",
"factor(cyl)6" = "Manual\n6 Cyclinders",
"factor(cyl)8" = "Manual\n8 Cyclinders",
"factor(am)1" = "Automatic\n4 Cylinders",
"factor(cyl)6:factor(am)1" = "Automatic\n6 Cylinders",
"factor(cyl)8:factor(am)1" = "Automatic\n8 Cylinders"
)
demo_plot(theme = "clean", xaxl = dict)
# 3) dynamic theme + flipped axes
demo_plot(theme = "clean", flip = TRUE)
# 4) any combination of the above, e.g., labeller dictionary + flipped axes
demo_plot(theme = "clean", flip = TRUE, xaxl = dict)
tinytheme() # reset theme
Run the code above in your browser using DataLab