|
5 | 5 | #' @inheritParams dodge_positions |
6 | 6 | #' @inheritParams graphics::arrows |
7 | 7 | #' @examples |
| 8 | +#' tinytheme("basic") |
| 9 | +#' |
| 10 | +#' # |
| 11 | +#' ## Basic coefficient plot(s) |
| 12 | +#' |
8 | 13 | #' mod = lm(mpg ~ wt * factor(am), mtcars) |
9 | 14 | #' coefs = data.frame(names(coef(mod)), coef(mod), confint(mod)) |
10 | 15 | #' colnames(coefs) = c("term", "est", "lwr", "upr") |
11 | 16 | #' |
12 | | -#' op = tpar(pch = 19) |
13 | | -#' |
14 | 17 | #' # "errorbar" and "pointrange" type convenience strings |
15 | 18 | #' tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "errorbar") |
16 | 19 | #' tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "pointrange") |
|
19 | 22 | #' tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, |
20 | 23 | #' type = type_errorbar(length = 0.2)) |
21 | 24 | #' |
22 | | -#' # display three models side-by-side with dodging |
| 25 | +#' # |
| 26 | +#' ## Flipped plots |
| 27 | +#' |
| 28 | +#' # For flipped errobar / pointrange plots, it is recommended to use a dynamic |
| 29 | +#' # theme that applies horizontal axis tick labels |
| 30 | +#' tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "errorbar", |
| 31 | +#' flip = TRUE, theme = "classic") |
| 32 | +#' tinyplot_add(type = 'vline', lty = 2) |
| 33 | +#' |
| 34 | +#' |
| 35 | +#' # |
| 36 | +#' ## Dodging groups |
23 | 37 | #' |
24 | 38 | #' models = list( |
25 | | -#' "Model A" = lm(mpg ~ wt + cyl, data = mtcars), |
26 | | -#' "Model B" = lm(mpg ~ wt + hp + cyl, data = mtcars), |
27 | | -#' "Model C" = lm(mpg ~ wt, data = mtcars) |
| 39 | +#' "Model A" = lm(mpg ~ wt, data = mtcars), |
| 40 | +#' "Model B" = lm(mpg ~ wt + cyl, data = mtcars), |
| 41 | +#' "Model C" = lm(mpg ~ wt + cyl + hp, data = mtcars) |
28 | 42 | #' ) |
29 | 43 | #' |
30 | | -#' results = lapply(names(models), function(m) { |
| 44 | +#' models = do.call( |
| 45 | +#' rbind, |
| 46 | +#' lapply(names(models), function(m) { |
31 | 47 | #' data.frame( |
32 | | -#' model = m, |
33 | | -#' term = names(coef(models[[m]])), |
34 | | -#' estimate = coef(models[[m]]), |
35 | | -#' setNames(data.frame(confint(models[[m]])), c("conf.low", "conf.high")) |
| 48 | +#' model = m, |
| 49 | +#' term = names(coef(models[[m]])), |
| 50 | +#' estimate = coef(models[[m]]), |
| 51 | +#' setNames(data.frame(confint(models[[m]])), c("conf.low", "conf.high")) |
36 | 52 | #' ) |
37 | | -#' }) |
38 | | -#' results = do.call(rbind, results) |
| 53 | +#' }) |
| 54 | +#' ) |
39 | 55 | #' |
40 | 56 | #' tinyplot(estimate ~ term | model, |
41 | 57 | #' ymin = conf.low, ymax = conf.high, |
42 | | -#' data = results, |
43 | | -#' type = type_pointrange(dodge = 0.2)) |
| 58 | +#' data = models, |
| 59 | +#' type = type_pointrange(dodge = 0.1)) |
| 60 | +#' |
| 61 | +#' # Aside 1: relative vs fixed dodge |
| 62 | +#' # The default dodge position is based on the unique groups (here: models) |
| 63 | +#' # available to each x value (here: coefficient term). To "fix" the dodge |
| 64 | +#' # position across all x values, use `fixed.dodge = TRUE`. |
44 | 65 | #' |
45 | | -#' # Note that the default dodged position is based solely on the number of |
46 | | -#' # groups (here: models) available to each coefficient term. To fix the |
47 | | -#' # position consistently across all terms, use `fixed.pos = TRUE`. |
| 66 | +#' tinyplot(estimate ~ term | model, |
| 67 | +#' ymin = conf.low, ymax = conf.high, |
| 68 | +#' data = models, |
| 69 | +#' type = type_pointrange(dodge = 0.1, fixed.dodge = TRUE)) |
| 70 | +#' |
| 71 | +#' # Aside 2: layering |
| 72 | +#' # For layering on top of dodged plots, rather pass the dodging arguments |
| 73 | +#' # through the top-level call if you'd like the dodging behaviour to be |
| 74 | +#' # inherited automatically by the add layers. |
48 | 75 | #' |
49 | 76 | #' tinyplot(estimate ~ term | model, |
50 | 77 | #' ymin = conf.low, ymax = conf.high, |
51 | | -#' data = results, |
52 | | -#' type = type_pointrange(dodge = 0.2, fixed.pos = TRUE)) |
| 78 | +#' data = models, |
| 79 | +#' type = "pointrange", |
| 80 | +#' dodge = 0.1, fixed.dodge = TRUE) |
| 81 | +#' tinyplot_add(type = "l", lty = 2) |
53 | 82 | #' |
54 | | -#' tpar(op) |
| 83 | +#' tinytheme() # reset theme |
55 | 84 | #' |
56 | 85 | #' @export |
57 | | -type_errorbar = function(length = 0.05, dodge = 0, fixed.pos = FALSE) { |
| 86 | +type_errorbar = function(length = 0.05, dodge = 0, fixed.dodge = FALSE) { |
58 | 87 | out = list( |
59 | 88 | draw = draw_errorbar(length = length), |
60 | | - data = data_pointrange(dodge = dodge, fixed.pos = fixed.pos), |
| 89 | + data = data_pointrange(dodge = dodge, fixed.dodge = fixed.dodge), |
61 | 90 | name = "p" |
62 | 91 | ) |
63 | 92 | class(out) = "tinyplot_type" |
|
0 commit comments