Skip to content

Instantly share code, notes, and snippets.

@ajstewartlang
Last active January 23, 2020 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajstewartlang/9be637ac94b41da26bf92316db9d39e6 to your computer and use it in GitHub Desktop.
Save ajstewartlang/9be637ac94b41da26bf92316db9d39e6 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(lubridate)
library(patchwork)
library(ggrepel)
library(ggthemes)
spotify_songs <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-21/spotify_songs.csv')
arrow <- tibble(x1 = 25, x2 = 12, y1 = 320, y2 = 331.5)
spotify_plot <- spotify_songs %>%
arrange(-duration_ms) %>%
distinct(track_name, .keep_all = TRUE) %>%
distinct(duration_ms, .keep_all = TRUE) %>%
mutate(duration_s = round(duration_ms/1000)) %>%
separate(col = track_album_release_date, into = c("Year", "Month", "Day"), sep = "-") %>%
group_by(Year) %>%
summarise(av_duration_s = mean(duration_s)) %>%
ggplot(aes(x = Year, y = av_duration_s)) +
geom_jitter(alpha = .5, size = 3) +
coord_flip() +
theme_economist() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(panel.background = element_rect(fill = "white")) +
theme(plot.background = element_rect(fill = "white")) +
scale_x_discrete(labels = c(1960, 1970, 1980, 1990, 2000, 2010, 2020),
breaks = c(1960, 1970, 1980, 1990, 2000, 2010, 2020)) +
theme(text = element_text(size = 12)) +
labs(title = "",
x = NULL,
y = "Average track duration (seconds)") +
annotate("text", x = 30, y = 320, size = 5, color = "gray20",
label = "1971 and it all got a bit prog") +
geom_curve(data = arrow, aes(x = x1, y = y1, xend = x2, yend = y2),
arrow = arrow(length = unit(0.2, "cm")), size = 0.5,
color = "gray20", curvature = 0.3)
plot_1971 <- spotify_songs %>%
filter(playlist_genre == "rock") %>%
arrange(-duration_ms) %>%
distinct(track_name, .keep_all = TRUE) %>%
distinct(duration_ms, .keep_all = TRUE) %>%
mutate(duration_s = round(duration_ms/1000)) %>%
separate(col = track_album_release_date, into = c("Year", "Month", "Day"), sep = "-") %>%
filter(Year == 1971) %>%
group_by(track_artist) %>%
summarise(mean_duration_s = mean(duration_s)) %>%
mutate(rank = rank(mean_duration_s)) %>%
ggplot(aes(x = mean_duration_s, y = 0, label = track_artist, size = rank)) +
geom_text_repel(force = 5, min.segment.length = 5) +
labs(title = NULL,
x = NULL,
y = NULL) +
theme_minimal() +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
scale_y_discrete(labels = c(-.02, 0, .02), breaks = c(-.02, 0, .02)) +
scale_x_discrete(labels = c(200, 500), breaks = c(200, 500)) +
guides(colour = FALSE) +
theme(text = element_text(size = 12)) +
annotate("text", x = 365, y = .35, size = 12,
label = "Increasing average track duration in 1971 rock") +
guides(size = FALSE)
spotify_plot / plot_1971
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment