Getting Technical

So here goes the first technical post.  I know, I know, I really should be using .rmd for this, but I really like Evernote.  
As I made clear in a very Shakespearean fashion, I was not too impressed with Spotify data, but kudos to them for making the data easy to get to.  
Although the Billboard website was easy to use, accessing the data was all manual.  The Billboard data was exactly what I wanted – performance information for songs – but to use it, I would need to manually gather the information.
I created a small dataset of chart hits from 1987, a stellar time for U2 and The Joshua Tree album.  Below is a sample of what I gathered.  I wanted to create the competition for U2 as a song climbed up the charts, so I always kept the top 5 hits in the database, and tracked U2’s songs as they went up and down the charts.

Date Song Artist Rank Previous Rank Weeks On Chart
3/21/87 Lean On Me Club Nouveau 1 5 6
3/21/87 Let’s Wait Awhile Janet Jackson 2 3 10
3/21/87 Nothing’s Gonna Stop Us Now Starship 3 10 8
3/21/87 Mandolin Rain Bruce Hornsby & The Range 4 6 10
3/21/87 Somewhere Out There Linda Ronstadt & James Ingram 5 2 14
3/21/87 With or Without You U2 64 na 1

I gathered about 8 months of data, and tracked 3 songs, “With Or Without You”, “I Still Haven’t Found What I’m Looking For”, and “Where The Streets Have No Name”.
I then proceeded to teach myself how to animate a times series graph using R’s gganimate package for ggplot2.

The first thing, load the packages.
library(dplyr)
library(gganimate)
library(ggplot2)
library(ggrepel)
library(ggridges)
library(ggthemes)
library(gridExtra)
library(knitr)
library(readxl)
library(tibble)
library(tidyr)
library(tidyselect)
library(tidyselect)
library(tidyverse)
library(wordcloud2)

Using my database, BBD3, I did some slight data munging to prep for the graph.  I used filter to reduce the data size so that I could check to see if the code was actually working.  I also created a color template.  The goal was to have the Song change colors as it climbed the charts, so if the song was not in the top 5, the color would be Gray.  And depending on the Rank, 1-5, the Song could be Blue, Red, White, Yellow or Green.

plotdata1 <- BBD3 %>%
#filter(Date >= as.Date(“1987-07-18”)) %>%
#filter(Date <= as.Date(“1987-08-08”)) %>%
filter(Artist == “U2”) %>%
mutate(Date = lubridate::ymd(Date)) %>%
mutate(newrank = ((101-Rank)/100)) %>%
mutate(Rank2 = as.character(Rank)) %>%
mutate(Color2 =
           ifelse(Artist == “U2”, “blue”,
                  “gray75”)) %>%
mutate(Color =
           ifelse(Rank2 == “1”,”blue”,
           ifelse(Rank2 == “2”,”red”,
           ifelse(Rank2 == “3”,”white”,
           ifelse(Rank2 == “4”,”yellow”,
           ifelse(Rank2 == “5”,”green”,
           “gray75”))))))
jColors <- plotdata1$Color
names(jColors) <- plotdata1$Rank2
aColors <- plotdata1$Color2
names(aColors) <- plotdata1$Artist
sColors <- plotdata1$Color2
names(sColors) <- plotdata1$Song
head(aColors)
head(sColors)

The colors never really worked for me.  I also tried to keep U2 one color so that it would be easier to see the Songs move on the chart, but it never seemed to work to my satisfaction.

ggplot(plotdata1, aes(x = WeeksOnChart,
                      y = reorder(Rank2, -Rank),
                      fill=factor(Rank2))) +
geom_point(size = 4, show.legend =FALSE, colour=”gray45″) +
aes(fill=factor(Rank2), color = jColors, show.legend=FALSE) +
xlab(“Weeks On Chart”)  +

scale_x_continuous(breaks  = seq(1,max(plotdata1$WeeksOnChart), by = 1)) +
scale_y_discrete(plotdata1$Rank2, na.translate = FALSE, name=”Billboard Rank”) +
geom_label_repel(aes(label = Artist, fill=Artist, colour=jColors),
                   fill = ‘white’,
                   hjust = 0,
                   vjust = 0,
                   size = 4,
                   nudge_y = 0.0,
                   nudge_x = 0.0,
                   direction = “x”,
                   fontface  = “bold”,
                   segment.color = ‘transparent’) +
geom_label_repel(aes(label = Song, fill=Song, colour=jColors),
                   fill = ‘white’,
                   hjust = 0,
                   vjust = 0,
                   size = 4,
                   nudge_y = 0.0,
                   nudge_x = -0.0,
                   direction = “y”,
                   fontface  = “bold”,
                   segment.color = ‘transparent’) +
theme(legend.position=”none”) +
labs(title = “Week: {closest_state}”) +
transition_states(Date, transition_length = 0, state_length = 2, wrap=FALSE) +
enter_fade() 
exit_fade()

The resulting code produced the following Gif:

A variation of the code also produced the following:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s