drought_layer <- function(players) {
geom_rect(
data = players |> filter(!is.na(drought_good)) |> distinct(day),
inherit.aes = FALSE,
aes(xmin = day - 0.5, xmax = day + 0.5, ymin = -Inf, ymax = Inf),
fill = alpha(palette$ink, 0.15)
)
}
plot_status <- function(players, title, subtitle) {
players |>
count(day, status) |>
mutate(
share = n / sum(n),
status = factor(status, levels = c("trading", "alone", "short"),
labels = c("Trading", "Alone", "Short day")),
.by = day
) |>
ggplot(aes(x = day, y = share, fill = status)) +
geom_area() +
scale_fill_manual(values = c(Trading = palette$ink,
Alone = alpha(palette$ink, 0.45),
`Short day` = alpha(palette$ink, 0.15))) +
labs(title = title, subtitle = subtitle, x = "Day", y = "Share of players", fill = NULL) +
theme_bmh()
}
plot_choices <- function(players, title, subtitle) {
goods <- sort(setdiff(unique(players$choice), "none"))
players |>
count(day, choice) |>
complete(day, choice = c(goods, "none"), fill = list(n = 0)) |>
mutate(choice = factor(choice, levels = c(goods, "none"),
labels = c(paste("Good", goods), "No specialty"))) |>
ggplot(aes(x = day, y = n)) +
drought_layer(players) +
geom_area(fill = alpha(palette$ink, 0.3)) +
geom_line(linewidth = 0.5) +
facet_wrap(~ choice, ncol = 3) +
labs(title = title, subtitle = subtitle, x = "Day", y = "Players") +
theme_bmh()
}
plot_village <- function(players, summary, title, subtitle, y) {
players |>
summarise(value = {{ summary }}, .by = day) |>
ggplot(aes(x = day, y = value)) +
drought_layer(players) +
geom_line() +
labs(title = title, subtitle = subtitle, x = "Day", y = y) +
theme_bmh()
}
plot_members <- function(players, summary, title, subtitle, y) {
players |>
summarise(value = {{ summary }}, .by = c(day, member)) |>
ggplot(aes(x = day, y = value, linetype = member)) +
drought_layer(players) +
geom_line() +
scale_linetype_manual(values = c(`In a group` = "solid", `Outside a group` = "dashed")) +
labs(title = title, subtitle = subtitle, x = "Day", y = y, linetype = NULL) +
theme_bmh()
}
# how each player got each good: one tile per player and good
plot_swaps <- function(choice, inside = NULL, outside = NULL, title, subtitle) {
n_goods <- max(ncol(inside), ncol(outside))
none <- matrix(FALSE, length(choice), n_goods)
inside <- inside %||% none
outside <- outside %||% none
fills <- c(`Their own good` = palette$ink,
`Swapped in the group` = alpha(palette$ink, 0.7),
`Swapped on the market` = alpha(palette$ink, 0.4),
`Made themselves` = alpha(palette$ink, 0.1))
expand_grid(player = seq_along(choice), good = seq_len(n_goods)) |>
mutate(
how = case_when(
good == choice[player] ~ "Their own good",
inside[cbind(player, good)] ~ "Swapped in the group",
outside[cbind(player, good)] ~ "Swapped on the market",
.default = "Made themselves"
),
how = factor(how, levels = names(fills)),
good = LETTERS[good],
player = factor(player, levels = rev(seq_along(choice)))
) |>
ggplot(aes(x = good, y = player, fill = how)) +
geom_tile(colour = palette$paper, linewidth = 1.5) +
scale_fill_manual(values = fills, drop = FALSE) +
labs(title = title, subtitle = subtitle, x = "Good", y = "Player", fill = NULL) +
guides(fill = guide_legend(nrow = 2)) +
theme_bmh()
}