Last active
July 21, 2021 15:58
-
-
Save ercas/f969bf8854ec8721c3283d64f228f040 to your computer and use it in GitHub Desktop.
Revisions
-
ercas revised this gist
Jul 21, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -109,7 +109,7 @@ data %>% y = "Hour", title = "Most active listening times (note log scale)" ) + scale_x_datetime(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) + theme( # viridis: https://github.com/BIDS/colormap/blob/master/colormaps.py#L788 -
ercas revised this gist
Jul 21, 2021 . 1 changed file with 21 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ library(stringr) # load data --------------------------------------------------------------- data <- fromJSON("ercas_lb-2021-07-21.json") %>% flatten() %>% mutate( listened_at = with_tz( @@ -86,21 +86,33 @@ data %>% # top listening times ----------------------------------------------------- weeks_since_epoch <- function(dt) { return(floor(as.numeric(dt) / (60*60*24*7))) } data %>% mutate( week = weeks_since_epoch(listened_at), hour = hour(listened_at) ) %>% group_by(week, hour) %>% summarize( week_of = floor_date(min(listened_at) - 1, "weeks") + 1, listens = n() ) %>% ggplot() + aes(week_of, hour, fill = log(listens)) + geom_tile() + scale_fill_viridis_c() + labs( x = "Week", y = "Hour", title = "Most active listening times (note log scale)" ) + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) + theme( # viridis: https://github.com/BIDS/colormap/blob/master/colormaps.py#L788 panel.background = element_rect(fill = rgb(0.267004, 0.004874, 0.329415)), panel.grid = element_blank() ) -
ercas revised this gist
Dec 23, 2020 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -73,16 +73,16 @@ data %>% ) ) %>% ggplot() + aes(x = listened_at, fill = Album) + geom_density(position = "stack", color = NA) + labs( x = "Date", y = "Density", title = sprintf("Top %d most listened-to albums over time", n_albums) ) + scale_fill_brewer(palette = "Spectral") + theme_minimal() + scale_y_continuous(breaks = NULL) # top listening times ----------------------------------------------------- -
ercas revised this gist
Dec 23, 2020 . 1 changed file with 0 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -84,38 +84,6 @@ data %>% theme_minimal() + scale_y_continuous(breaks = NULL) # top listening times ----------------------------------------------------- data %>% -
ercas created this gist
Dec 23, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,138 @@ library(dplyr) library(ggplot2) library(jsonlite) library(lubridate) library(stringr) # load data --------------------------------------------------------------- data <- fromJSON("ercas_lb-2020-12-23.json") %>% flatten() %>% mutate( listened_at = with_tz( as_datetime(listened_at), system("readlink -f /etc/localtime | grep -o '[^/]*/[^/]*$'", intern = TRUE) ) ) %>% filter(listened_at >= as_datetime("2020-01-01")) # top artists ------------------------------------------------------------- n_artists <- 10 top_artists <- data$track_metadata.artist_name %>% table() %>% as.data.frame() %>% arrange(desc(Freq)) %>% .$. %>% head(n_artists) data %>% mutate( Artist = ifelse( #track_metadata.artist_name %in% top_artists, #track_metadata.artist_name, str_detect(track_metadata.artist_name, paste(top_artists, collapse = "|")), str_extract(track_metadata.artist_name, paste(top_artists, collapse = "|")), "All others" ) ) %>% ggplot() + aes(x = listened_at, fill = Artist) + geom_density(position = "stack", color = NA) + labs( x = "Date", y = "Density", title = sprintf("Top %d most listened-to artists over time", n_artists) ) + scale_fill_brewer(palette = "Spectral") + theme_minimal() + scale_y_continuous(breaks = NULL) # top albums -------------------------------------------------------------- n_albums <- 10 top_albums <- data$track_metadata.release_name %>% table() %>% as.data.frame() %>% arrange(desc(Freq)) %>% .$. %>% head(n_albums) data %>% mutate( Album = ifelse( track_metadata.release_name %in% top_albums, ifelse( str_starts(track_metadata.release_name, "The Idler Wheel"), "The Idler Wheel (...)", track_metadata.release_name ), "All others" ) ) %>% ggplot() + aes(x = listened_at, fill = Album) + geom_density(position = "stack", color = NA) + labs( x = "Date", y = "Density", title = sprintf("Top %d most listened-to albums over time", n_albums) ) + scale_fill_brewer(palette = "Spectral") + theme_minimal() + scale_y_continuous(breaks = NULL) # top songs --------------------------------------------------------------- n_songs <- 10 top_songs <- data$track_metadata.track_name %>% table() %>% as.data.frame() %>% arrange(desc(Freq)) %>% .$. %>% head(n_songs) data %>% mutate( Track = ifelse( track_metadata.track_name %in% top_songs, track_metadata.track_name, "All others" ) ) %>% ggplot() + aes(x = listened_at, fill = Track) + geom_density(position = "stack", color = NA) + labs( x = "Date", y = "Density", title = sprintf("Top %d most listened-to tracks over time", n_songs) ) + scale_fill_brewer(palette = "Spectral") + theme_minimal() + scale_y_continuous(breaks = NULL) # top listening times ----------------------------------------------------- data %>% transmute( week = week(listened_at), hour = hour(listened_at) ) %>% table() %>% as.data.frame() %>% ggplot() + aes(week, hour, fill = log(Freq)) + geom_tile() + scale_fill_viridis_c(na.value = "black") + labs( x = "Week", y = "Hour", title = "Most active listening times (note log scale)" ) + theme_minimal() + theme(axis.text.x = element_text(angle=90))