Load libraries

setwd("/Users/Posh/Documents/R Projects/Therapy")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(spotifyr)
library(ggplot2)
library(ggthemes)
library(png)
library(grid)
library(ggimage)

Getting Spotify API connected to R

id <- "8cbfec51ec2c4327a133a709925be7aa"
secret <- "fd5c166fc3f24283991e5a9eb8bdfa91"
Sys.setenv(SPOTIFY_CLIENT_ID = id)
Sys.setenv(SPOTIFY_CLIENT_SECRET = secret)
access_token <- get_spotify_access_token()

my_id <- "vzrv22q5eovbfnvy74x1rgkpa"

get_user_profile(my_id)
## # A tibble: 1 x 10
##   display_name external_urls.s… followers.total href  id    images.height
##   <chr>        <chr>            <chr>           <chr> <chr> <chr>        
## 1 Irakli       https://open.sp… 8               http… vzrv… <NA>         
## # … with 4 more variables: images.url <chr>, images.width <chr>,
## #   type <chr>, uri <chr>

Cheking the functions in the package

tracks <- get_album_tracks("1RSG9GItLoWp1zQfGG7bCm", authorization = access_token)
tracks_id <- tracks %>%
  select(id, name)
track_features <- get_track_audio_features(tracks_id$id, access_token)

features <- left_join(tracks_id, track_features)
## Joining, by = "id"
eko_features <- features[, 1:13]

Visualisations

#import image for scatterplot background
img <- readPNG("/Users/Posh/Documents/R Projects/eko.png")
g <- rasterGrob(img, interpolate=TRUE) 

pdf("eko.pdf")
Valence <- features %>%
  ggplot(aes(valence, name, color = valence, size = valence))+
  annotation_custom(rasterGrob(img, 
                               width = unit(1,"npc"), 
                               height = unit(1,"npc")), 
                    -Inf, Inf, -Inf, Inf)+
  geom_point(show.legend = FALSE)+
  theme_minimal()+
  scale_color_gradient(low = "#000000", high = "#FB9121")+
  theme_hc()+
  xlab("Valence")+
  ylab("Song")
  
dev.off()
## quartz_off_screen 
##                 2
Valence