library(readr)
clean_taylor_swift_spotify_data <- read_csv("~/Working with data frames/clean_taylor_swift_spotify_data.csv")
## Rows: 530 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (4): album, release_date, id, track_name
## dbl (11): acousticness, danceability, energy, instrumentalness, liveness, lo...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
colnames(clean_taylor_swift_spotify_data)
##  [1] "album"            "release_date"     "id"               "track_name"      
##  [5] "acousticness"     "danceability"     "energy"           "instrumentalness"
##  [9] "liveness"         "loudness"         "speechiness"      "tempo"           
## [13] "valence"          "popularity"       "duration_ms"
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(knitr)
top_3_popularity <- select(clean_taylor_swift_spotify_data, track_name,popularity)%>%
  arrange(desc(popularity))%>%
  head(n=3)
kable(head(top_3_popularity, 3))
track_name popularity
Cruel Summer 96
Lover 90
Anti-Hero 89
spotify screenshot
spotify screenshot
clean_distinct_spotify_data <- (clean_taylor_swift_spotify_data)%>%
  arrange(desc(popularity))
clean_distinct_spotify_data <- distinct(clean_distinct_spotify_data, track_name, .keep_all = TRUE)
popularity <- clean_distinct_spotify_data$popularity
correlation_with_popularity <- cor(clean_distinct_spotify_data[, sapply(clean_distinct_spotify_data, is.numeric)], popularity)
print(correlation_with_popularity)
##                          [,1]
## acousticness     -0.056909062
## danceability      0.140757318
## energy            0.001182703
## instrumentalness  0.041195021
## liveness         -0.293730395
## loudness         -0.079461432
## speechiness      -0.037737809
## tempo            -0.046831939
## valence          -0.015328652
## popularity        1.000000000
## duration_ms      -0.102930269
popularity_vs_liveness <- clean_distinct_spotify_data %>%
  mutate(popularity_group = cut(popularity, breaks = 10))

library(ggplot2)
ggplot(data = popularity_vs_liveness)+
  geom_point(mapping=aes(x=popularity_group, y=liveness))+
  labs(x= "Grouped Popularity", y= "Liveness")+
  ggtitle("Scatter Plot of Grouped Popularity vs. Liveness")

clean_distinct_spotify_data %>%
  group_by (album) %>%
  summarise(mean_popularity = mean(popularity)) %>%
  arrange(desc(mean_popularity))
## # A tibble: 27 × 2
##    album                            mean_popularity
##    <chr>                                      <dbl>
##  1 reputation                                  79.3
##  2 Lover                                       79.3
##  3 Midnights                                   78.1
##  4 1989 (Taylor's Version)                     77.4
##  5 folklore                                    76.3
##  6 Midnights (The Til Dawn Edition)            74.7
##  7 1989 (Taylor's Version) [Deluxe]            74  
##  8 evermore                                    73.7
##  9 Speak Now (Taylor's Version)                73.3
## 10 Red (Taylor's Version)                      70.3
## # ℹ 17 more rows
# Create a new dataset for continued analysis 
top_5_albums <- clean_distinct_spotify_data %>%
  filter(album %in% c("reputation","Lover","Midnights","1989 (Taylor's Version)", "folklore"))
library(dplyr)

# Define a function to calculate correlation for each group
calculate_correlation <- function(df) {
  correlation_with_popularity <- cor(df$popularity, df[, sapply(df, is.numeric)])
  return(data.frame(correlation_with_popularity))
}

# Group the data by album and calculate correlation for each group
correlation_by_album <- top_5_albums %>%
  group_by(album) %>%
  group_modify(~ calculate_correlation(.))

# Print the results and show all values
options(tibble.width = Inf)
print(correlation_by_album)
## # A tibble: 5 × 12
## # Groups:   album [5]
##   album                   acousticness danceability  energy instrumentalness
##   <chr>                          <dbl>        <dbl>   <dbl>            <dbl>
## 1 1989 (Taylor's Version)      -0.0945       0.195  -0.0791           0.288 
## 2 Lover                        -0.427       -0.272   0.570            0.0714
## 3 Midnights                    -0.498        0.253   0.690           -0.282 
## 4 folklore                     -0.370       -0.0377  0.343           -0.0394
## 5 reputation                   -0.216        0.239   0.0891           0.233 
##   liveness loudness speechiness   tempo valence popularity duration_ms
##      <dbl>    <dbl>       <dbl>   <dbl>   <dbl>      <dbl>       <dbl>
## 1  -0.0454   -0.225     -0.125  -0.485  -0.231           1     -0.217 
## 2  -0.283     0.563     -0.0740  0.0386  0.185           1      0.160 
## 3   0.237     0.676     -0.193  -0.207   0.490           1     -0.345 
## 4   0.379     0.626     -0.232  -0.144   0.0762          1     -0.0350
## 5  -0.311     0.210      0.0185  0.190  -0.0837          1      0.151
#Function for first graph
top_5_albums %>%
  filter(album== "Lover") %>%
  ggplot(aes(x=popularity, y=energy))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "Lover Tracks Energy and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#Function for second graph
top_5_albums %>%
  filter(album== "Lover") %>%
  ggplot(aes(x=popularity, y=loudness))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "Lover Tracks Loudness and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#Code for first Midnights graph
top_5_albums %>%
  filter(album== "Midnights") %>%
  ggplot(aes(x=popularity, y=energy))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "Midnights Tracks Energy and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#Code for second Midnights graph
top_5_albums %>%
  filter(album== "Midnights") %>%
  ggplot(aes(x=popularity, y=loudness))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "Midnights Tracks Loudness and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#Code for third Midnights graph
top_5_albums %>%
  filter(album== "Midnights") %>%
  ggplot(aes(x=popularity, y=valence))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "Midnights Tracks Valence and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#Code for first folklore track graph
top_5_albums %>%
  filter(album== "folklore") %>%
  ggplot(aes(x=popularity, y=loudness))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "folklore Tracks Loudness and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#code for second folklore graph
top_5_albums %>%
  filter(album== "folklore") %>%
  ggplot(aes(x=popularity, y=acousticness))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "folklore Tracks Acousticness and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#code for third folklore graph
top_5_albums %>%
  filter(album== "folklore") %>%
  ggplot(aes(x=popularity, y=energy))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "folklore Tracks Energy and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'

#code for fourth folklore graph
top_5_albums %>%
  filter(album== "folklore") %>%
  ggplot(aes(x=popularity, y=liveness))+
  geom_point()+
  stat_smooth(method="lm")+
  labs(title = "folklore Tracks Liveness and Popularity Correlation")
## `geom_smooth()` using formula = 'y ~ x'