library(spotifyr)
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0     ✔ purrr   0.2.5
## ✔ tibble  2.0.0     ✔ dplyr   0.7.8
## ✔ tidyr   0.8.2     ✔ stringr 1.3.1
## ✔ readr   1.3.1     ✔ forcats 0.3.0
## ── Conflicts ───────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(wordcloud2)

Spotify access token- obtain your own through this process laid out by RCharlie Analytics: https://www.rcharlie.com/.

Choose an artist- why not the King of Rock and Roll?

Elvis <- get_artist_audio_features("Elvis Presley")
## Request failed [429]. Retrying in 2 seconds...
## Request failed [429]. Retrying in 1 seconds...
glimpse(Elvis)
## Observations: 1,704
## Variables: 39
## $ artist_name                  <chr> "Elvis Presley", "Elvis Presley", "…
## $ artist_id                    <chr> "43ZHCT0cAZBISjO8DG9PnE", "43ZHCT0c…
## $ album_id                     <chr> "1vaQwUom5fWnLNJDcabU01", "1vaQwUom…
## $ album_type                   <chr> "album", "album", "album", "album",…
## $ album_images                 <list> [<data.frame[3 x 3]>, <data.frame[…
## $ album_release_date           <chr> "2019-02-15", "2019-02-15", "2019-0…
## $ album_release_year           <dbl> 2019, 2019, 2019, 2019, 2019, 2019,…
## $ album_release_date_precision <chr> "day", "day", "day", "day", "day", …
## $ danceability                 <dbl> 0.435, 0.405, 0.438, 0.325, 0.128, …
## $ energy                       <dbl> 0.8280, 0.6380, 0.8730, 0.9600, 0.7…
## $ key                          <int> 0, 9, 5, 2, 5, 9, 2, 9, 4, 0, 4, 10…
## $ loudness                     <dbl> -8.764, -9.353, -8.722, -10.345, -1…
## $ mode                         <int> 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,…
## $ speechiness                  <dbl> 0.1010, 0.0706, 0.0324, 0.4530, 0.0…
## $ acousticness                 <dbl> 0.31000, 0.37900, 0.35500, 0.35800,…
## $ instrumentalness             <dbl> 4.07e-05, 4.83e-03, 9.80e-01, 2.39e…
## $ liveness                     <dbl> 0.6670, 0.8310, 0.8840, 0.9520, 0.9…
## $ valence                      <dbl> 0.743, 0.693, 0.772, 0.603, 0.394, …
## $ tempo                        <dbl> 116.141, 139.492, 95.772, 94.018, 8…
## $ track_id                     <chr> "5blcqgpsuKhm0WQwMsmywj", "7Gbq3qTE…
## $ analysis_url                 <chr> "https://api.spotify.com/v1/audio-a…
## $ time_signature               <int> 4, 3, 4, 4, 4, 4, 4, 4, 4, 3, 4, 3,…
## $ artists                      <list> [<data.frame[1 x 6]>, <data.frame[…
## $ available_markets            <list> [<"AD", "AE", "AR", "AT", "AU", "B…
## $ disc_number                  <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ duration_ms                  <int> 206373, 63200, 46600, 97000, 145293…
## $ explicit                     <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ track_href                   <chr> "https://api.spotify.com/v1/tracks/…
## $ is_local                     <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ track_name                   <chr> "Trouble / Guitar Man - Live", "Hea…
## $ track_preview_url            <chr> "https://p.scdn.co/mp3-preview/be7e…
## $ track_number                 <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …
## $ type                         <chr> "track", "track", "track", "track",…
## $ track_uri                    <chr> "spotify:track:5blcqgpsuKhm0WQwMsmy…
## $ external_urls.spotify        <chr> "https://open.spotify.com/track/5bl…
## $ album_name                   <chr> "The Best of The '68 Comeback Speci…
## $ key_name                     <chr> "C", "A", "F", "D", "F", "A", "D", …
## $ mode_name                    <chr> "major", "major", "major", "major",…
## $ key_mode                     <chr> "C major", "A major", "F major", "D…
Elvis %>% 
  count(album_release_year, sort = T) 

Still the King. They say he’s not dead the record shows that he is indeed alive!

Elvis %>% 
  ggplot(aes(x= album_release_year, y= danceability, color = album_release_year)) +
  geom_point()

Retrieving data from spotify- to obtain artist ID, left click on artist name and use the last alpha numeric following the final/

https://open.spotify.com/artist/43ZHCT0cAZBISjO8DG9PnE

theking <- get_artist_top_tracks("43ZHCT0cAZBISjO8DG9PnE", market = "US",
  authorization = get_spotify_access_token(),
  include_meta_info = FALSE)
glimpse(theking)
## Observations: 10
## Variables: 28
## $ artists                      <list> [<data.frame[1 x 6]>, <data.frame[…
## $ disc_number                  <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
## $ duration_ms                  <int> 182360, 146480, 261279, 136026, 170…
## $ explicit                     <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ href                         <chr> "https://api.spotify.com/v1/tracks/…
## $ id                           <chr> "44AyOl4qVkzS48vBsbNXaC", "4gphxUgq…
## $ is_local                     <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, …
## $ is_playable                  <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,…
## $ name                         <chr> "Can't Help Falling in Love", "Jail…
## $ popularity                   <int> 77, 72, 71, 69, 68, 64, 63, 62, 61,…
## $ preview_url                  <chr> "https://p.scdn.co/mp3-preview/994e…
## $ track_number                 <int> 5, 5, 13, 1, 13, 1, 4, 1, 19, 8
## $ type                         <chr> "track", "track", "track", "track",…
## $ uri                          <chr> "spotify:track:44AyOl4qVkzS48vBsbNX…
## $ album.album_type             <chr> "album", "album", "album", "album",…
## $ album.artists                <list> [<data.frame[1 x 6]>, <data.frame[…
## $ album.href                   <chr> "https://api.spotify.com/v1/albums/…
## $ album.id                     <chr> "7xe8VI48TxUpU1IIo0RfGi", "0C3t1htE…
## $ album.images                 <list> [<data.frame[3 x 3]>, <data.frame[…
## $ album.name                   <chr> "Blue Hawaii", "Elvis' Golden Recor…
## $ album.release_date           <chr> "1961-10-20", "1958-03-21", "1969-0…
## $ album.release_date_precision <chr> "day", "day", "day", "day", "day", …
## $ album.total_tracks           <int> 14, 14, 16, 14, 16, 3, 14, 12, 19, …
## $ album.type                   <chr> "album", "album", "album", "album",…
## $ album.uri                    <chr> "spotify:album:7xe8VI48TxUpU1IIo0Rf…
## $ album.external_urls.spotify  <chr> "https://open.spotify.com/album/7xe…
## $ external_ids.isrc            <chr> "USRC16101350", "USRC15705223", "US…
## $ external_urls.spotify        <chr> "https://open.spotify.com/track/44A…

Sounds like a whole lot of vibrato to me…

Elvis %>%
  ggplot(aes(x = album_name, y = speechiness)) +
  geom_point()

Elvis %>% 
  plot_ly(x = ~danceability, 
          y = ~energy) %>% 
  add_markers(showlegend = F) %>%
  layout(title = "Energy and Danceability",
         xaxis = list(title = "Danceability"),
         yaxis = list(title = "Energy")) 

“A little less conversation” seems to be the case.

Elvis %>% 
  plot_ly(x = ~track_name, 
          y = ~speechiness) %>% 
  add_markers(showlegend = F) %>%
  layout(title = "Speechiness in each Track",
         xaxis = list(title = "Track Name"),
         yaxis = list(title = "Speechiness"))

Point well taken: more music= greater volume

 Elvis %>% 
  plot_ly(x = ~speechiness, y = ~loudness) %>% 
  add_histogram2dcontour()

Danceability versus tempo: slow songs are for the brave apparently.

Elvis %>% 
  plot_ly(x = ~tempo, 
          y = ~danceability) %>% 
  add_markers(showlegend = F) %>%
  layout(title = "Danceability and Tempo Comparison",
         xaxis = list(title = "Tempo"),
         yaxis = list(title = "Danceability"))

If it’s a Hit, it has got to come first. ??? OR maybe just anywhere will do.

theking %>% 
  count(track_number, sort = T)
theking%>% 
ggplot(aes(x= track_number, y = popularity)) +
  geom_jitter(color = "dark blue") +
  ggtitle("Popularity based on track number")

theking %>% 
count(duration_ms, popularity, sort = T)

Songs that are in the middle range for time seem to be the most popular

theking %>% 
ggplot(aes(x= duration_ms, y = popularity)) +
  geom_jitter(color = "dark blue") +
  ggtitle("Popularity based on time")

theking %>%
  group_by(track_number) %>%
  mutate(duration_ms) %>%
  summarize(mean_duration_ms = mean(duration_ms))