library(ggdist)
library(ggpattern)
#> Warning: package 'ggpattern' was built under R version 4.5.1
rect_height <- .8
rect_pad <- 0.3
tms |>
group_by(max_shellback, metric) |>
median_qi(
.width = c(.67, .89, .97),
.exclude = c("album_name", "track_name")
) |>
mutate(.width = factor(.width)) |>
ggplot(aes(
x = value,
y = fct_rev(factor(metric)),
width = .upper - .lower
)) +
geom_tile(
data = tibble(
value = c(.63, .60, .69, .54, .45, .35),
metric = rep(c("danceability", "energy", "valence"), each = 2),
max_shellback = rep(c(TRUE, FALSE), times = 3),
.width = rep(c("0.67", "0.89", "0.97"), each = 2)
),
aes(group = max_shellback, alpha = .width),
height = rect_height,
width = 0.01,
fill = "grey20",
position = position_dodge2(padding = rect_pad)
) +
geom_tile_pattern(
data = ~filter(.x, .width == "0.97"),
aes(pattern_filename = max_shellback,),
pattern = "image",
pattern_type = "tile",
pattern_scale = .5,
height = rect_height,
position = position_dodge2(padding = rect_pad)
) +
geom_tile(
data = ~filter(.x, .width == "0.97"),
aes(group = max_shellback),
height = rect_height,
fill = "white",
alpha = 0.7,
position = position_dodge2(padding = rect_pad)
) +
geom_tile_pattern(
data = ~filter(.x, .width == "0.89"),
aes(pattern_filename = max_shellback,),
pattern = "image",
pattern_type = "tile",
pattern_scale = .5,
height = rect_height,
position = position_dodge2(padding = rect_pad)
) +
geom_tile(
data = ~filter(.x, .width == "0.89"),
aes(group = max_shellback),
height = rect_height,
fill = "white",
alpha = 0.4,
position = position_dodge2(padding = rect_pad)
) +
geom_tile_pattern(
data = ~filter(.x, .width == "0.67"),
aes(pattern_filename = max_shellback,),
pattern = "image",
pattern_type = "tile",
pattern_scale = .5,
height = rect_height,
position = position_dodge2(padding = rect_pad)
) +
expand_limits(x = c(0, 1)) +
scale_y_discrete(labels = str_to_title) +
scale_pattern_filename_discrete(
choices = c("glitter/glitter-orange.png", "glitter/glitter-green.png"),
breaks = c("TRUE", "FALSE"),
labels = c("Max Martin & Shellback", "Solo and Other Collaborators"),
name = "Collaborator"
) +
scale_alpha_manual(
values = c("0.67" = 1, "0.89" = 0.6, "0.97" = 0.3),
limits = c("0.97", "0.89", "0.67"),
labels = c("97%", "89%", "67%"),
drop = FALSE,
name = "Interval"
) +
labs(x = NULL, y = NULL, pattern_filename = NULL) +
theme(legend.box = "vertical") +
guides(
pattern_filename = guide_legend(
override.aes = list(pattern_type = "expand")
)
)