In this document, I will demonstrate how to access the audio features from any song on Spotify. This is useful if you want to analyze trends within certain artists, such as how their music evolves/evolved over time. I will mainly be focused on the valence of songs, which is a value that spotify assigns to each song, from 0-1. Values closer to 1 indicate a positive tone within that track, and valence values closer to 0 indicate a negative tone in that track.
Setting up the spotify API is very simple. Go to the Spotify for developers website and following the instructions. It’s important to note that in order to set this up, you need to have a Spotify account. It doesn’t need to be a premium account, though, so you can do this with a free Spotify account. Once you have the app set up, go to your dashboard and go to settings. In the settings, you will find your client ID and your client secret. These are necessary to retrieve your access token.
The lines of code below are needed to input your client ID and secret into R so you can retrieve the access token (spotifyr package is needed):
Sys.setenv(SPOTIFY_CLIENT_ID = 'd884bb08560e497987e517293ba755b0')
Sys.setenv(SPOTIFY_CLIENT_SECRET = '99e8e3cab75f48fca2f90ab25cae1e9b')
# Secret was changed after publishing
access_token <- get_spotify_access_token()
Now that we have everything set up, we can access audio features from
any artist’s full discography by using the
get_artist_audio_features() function. The line of code
below assigns to the name weezer a data frame that includes
information for every track within Weezer’s full discography. The
information includes, but is not limited to, valence, tempo, key,
acousticness, liveness.
weezer <- get_artist_audio_features('weezer')
Now that we have a data frame with all this audio information, we can
perform data manipulation to analyze whatever we want. In this example,
I wanted to see how the overall valence of Weezer’s songs has changed
over time. To do this, I made a line graph by using
group_by() and summarize().
weezer %>%
group_by(album_release_year) %>%
summarize(avg.valence = mean(valence)) %>%
ggplot(aes(x = album_release_year, y = avg.valence)) +
geom_line(col = "blue") +
labs(title = "Weezer - Valence over time",
x = "Year",
y = "Average Valence")
The spotifyr package has many more functions that you can use to do all kinds of data analysis on different artists or even your own profile.