-
Notifications
You must be signed in to change notification settings - Fork 13
/
gg_interaction_plot.R
41 lines (28 loc) · 1.22 KB
/
gg_interaction_plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#' ---
#' title: gg_interaction()
#' description: inspired by 'Linear Models with R' by Julian Faraway (see page 243)
#' author: [email protected]
#' ---
library(tidyverse)
data(warpbreaks)
plot(breaks ~ wool, warpbreaks)
with(warpbreaks, interaction.plot(wool, tension, breaks))
gg_interaction_plot <- function(data, formula) {
formula <- as.formula(formula)
y_var <- as.character(formula[2])
x_vars <- as.character(formula[3]) %>%
str_split(" \\+ ") %>% unlist()
data <- mutate_at(data, x_vars, as.factor)
shp_vars <- rev(x_vars)
map2(x_vars, shp_vars,
~ ggplot(data, aes_(y = as.name(y_var), x = as.name(..1), shape = as.name(..2))) +
geom_point(position = position_jitter(width = .1)) +
stat_summary(fun.y = "mean", geom = "line",
aes_(group = as.name(..2), linetype = as.name(..2))) +
scale_shape_manual(values = 15:25) +
theme(legend.position = "top", legend.direction = "horizontal")) %>%
cowplot::plot_grid(plotlist = ., ncol = 2)
}
gg_interaction_plot(warpbreaks, breaks ~ wool + tension )
data(pvc, package("faraway"))
gg_interaction_plot(pvc, psize ~ operator + resin)