Created
November 10, 2023 18:05
-
-
Save tukachev/8bf53e535d830c31ed2b5561d8279f1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(jsonlite) | |
library(tidyverse) | |
library(RColorBrewer) | |
library(ggtext) | |
library(OneR) | |
#extrafont::loadfonts() # fix fonts | |
streamHistory <- | |
list.files( | |
path = "ext/", | |
full.names = TRUE, | |
pattern = ".json" | |
) %>% | |
map_df(~ fromJSON(.)) %>% | |
filter(ms_played / 1000 > 30 & !duplicated(.)) %>% | |
as_tibble() %>% | |
mutate_at("ts", ymd_hms, tz = "Asia/Yekaterinburg") %>% | |
mutate( | |
date = floor_date(ts, "day") %>% as_date(), | |
day = day(ts), | |
month = month(ts), | |
year = year(ts) | |
) %>% | |
filter(year %in% c(2019:2023)) | |
# str(streamHistory) | |
# names(streamHistory) | |
tracks_count <- streamHistory %>% | |
group_by(year, date) %>% | |
count() %>% | |
ungroup() %>% | |
mutate(year_day = yday(date)) | |
write_csv2(tracks_count, "tracks_count.csv") | |
# str(tracks_count) | |
quantile(tracks_count$n, probs = seq(0, 1, 0.1)) | |
quantile(tracks_count$n, probs = seq(0, 1, 0.25)) | |
table(bin(tracks_count$n, nbins = 5, method = "content")) | |
table( | |
bin(tracks_count$n, nbins = 5, method = "content"), | |
tracks_count$year | |
) | |
# col_strip <- brewer.pal(n = 5, "Oranges") | |
col_strip <- brewer.pal(n = 5, "YlGn") | |
# quantiles | |
q <- quantile(tracks_count$n, seq(0, 1, 0.2)) | |
qr <- quantile(rank(tracks_count$n), seq(0, 1, 0.2)) | |
# attributes(q)$names[2:5] | |
labelsq <- paste0( | |
as.numeric(q), "\n", | |
c("min", attributes(q)$names[2:5], "max") | |
) | |
all_tracks <- sum(tracks_count$n) | |
median <- median(tracks_count$n) | |
tracks_by_year <- tracks_count %>% | |
group_by(year) %>% | |
summarise(sum = sum(n), | |
median = median(n)) | |
tracks_by_year | |
annot <- glue::glue("Учитывались треки с продолжительностью прослушивания | |
более 30 секунд. Всего было прослушано {all_tracks} | |
треков за весь период с 2019 по 2023 год. Медианное | |
значение числа ежедневного прослушивания | |
составляет {median} треков") | |
annot <- stringr::str_wrap(annot, 68) | |
tracks_count %>% | |
ggplot(aes( | |
x = year_day, y = 1, | |
fill = rank(n) | |
)) + | |
geom_tile() + | |
scale_fill_stepsn( | |
colours = col_strip, | |
breaks = qr, | |
labels = labelsq | |
) + | |
facet_wrap(~year, | |
ncol = 1, | |
strip.position = "left" | |
) + | |
labs( | |
x = "", | |
y = "", | |
caption = "Данные: Аккаунт пользователя Spotify<br> | |
Юрий Тукачев, ноябрь 2023 @weekly_charts" | |
) + | |
ggtitle( | |
"Как часто я слушал музыку на Spotify?", | |
paste0("Ежедневное число прослушиваний треков в течение года\n", annot) | |
) + | |
guides( | |
fill = guide_coloursteps( | |
title = "Число треков", | |
title.theme = element_text( | |
size = 16, | |
margin = margin(t = 15) | |
), | |
label.theme = element_text( | |
color = "grey30", | |
size = 14, | |
lineheight = 1.1 | |
), | |
title.position = "top", | |
title.hjust = .5, | |
barwidth = unit(15, "lines"), | |
barheight = unit(.5, "lines") | |
) | |
) + | |
theme_void() + | |
theme( | |
plot.background = element_rect(color = "grey60"), | |
plot.title = element_markdown(face = "bold"), | |
plot.title.position = "plot", | |
text = element_text( | |
size = 22, | |
family = "PT Sans" | |
), | |
plot.caption.position = "plot", | |
plot.caption = element_markdown( | |
lineheight = 1.1, | |
size = 16, | |
color = "gray60", | |
margin = margin(t = 20) | |
), | |
plot.subtitle = element_text( | |
size = 16, | |
color = "gray60", | |
margin = margin(t = 15, b = 20) | |
), | |
legend.position = "bottom", | |
panel.background = element_blank(), | |
plot.margin = margin(25, 25, 10, 25), | |
axis.ticks = element_blank() | |
) | |
ggsave( | |
"spotify_all_years_YlGn5.png", | |
bg = "white", | |
dpi = 600, | |
width = 8, | |
height = 8 | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment