#### Data Cleaning/Preparation ####



# Renaming my Column Names
music.2 <- music%>% rename(
  Artist = artist, 
  Song = song, 
  Duration_s = duration_ms, 
  Explicit = explicit, 
  Year = year, 
  Popularity = popularity, 
  Danceability= danceability, 
  Energy = energy, 
  Key = key, 
  Loudness = loudness, 
  Mode = mode, 
  Speechiness = speechiness, 
  Acousticness = acousticness, 
  Instrumentalness = instrumentalness, 
  Liveness = liveness, 
  Valence = valence, 
  Tempo = tempo, 
  Genre = genre
)
# Fix the seconds to the right scale 
music.2$Duration_s = (music.2$Duration_s / 1000)

# Extracting the first genre as the main genre (assuming genres are separated by commas) 
music.2 <- music.2 %>% mutate(Main_Genre = strsplit(as.character(Genre), ",")
                              [sapply(strsplit(as.character(Genre), ","), length) > 0]) 

# Extracting the first element from the split genres as the main genre 
music.2$Main_Genre <- sapply(music.2$Main_Genre, function(x) trimws(x[1]))

1 Numerical Summaries - Exploration of Spotify Data

Danceability

summary(music.2$Danceability)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.1290  0.5810  0.6760  0.6674  0.7640  0.9750

Energy

summary(music.2$Energy)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0549  0.6220  0.7360  0.7204  0.8390  0.9990

Popularity

summary(music.2$Popularity)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00   56.00   65.50   59.87   73.00   89.00

Explicit Music in Different Genre’s

table(music.2$Main_Genre, music.2$Explicit)
##                    
##                     False True
##   country              11    0
##   Dance/Electronic     34    7
##   easy listening        1    0
##   Folk/Acoustic         3    1
##   hip hop             350  426
##   latin                12    3
##   metal                 7    2
##   pop                 842   94
##   R&B                  11    2
##   rock                149   13
##   set()                20    2
##   World/Traditional     9    1

2 Visualizations

# Tempo Distribution of Hit Songs 
ggplot(music.2, aes(x=Tempo))+geom_histogram(bins=45, fill="pink", color="magenta")+
  labs(x="Tempo", y="Number of Songs", title="Distribution of Tempo of Hit Songs")+
  theme(plot.title=element_text(hjust=0.5))

# Energy vs. Danceability of the Songs
ggplot(music.2, aes(x=Danceability, y=Energy))+geom_point(color="skyblue")+
  labs(title="Energy by Danceability of Hit Songs")+
  theme(plot.title=element_text(hjust=0.5))

# Distribution of Energy by Valence
ggplot(music.2, aes(x=Valence, y=Danceability, size=Popularity, fill=Explicit))+
  geom_point(shape=21, alpha=.5, color="tan")+
  scale_size(range=c(.1,16))+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(title="Dist of Energy by Valence")+
  theme_classic()

Popularity Trends Over the Years

ggplot(music.2,aes(x=Year,y=Main_Genre,fill=Tempo)) + 
  geom_tile(color='white') + 
  scale_fill_gradient(low='Pink',high='Blue')

Popularity and Genre

ggplot(music.2, aes(x = Main_Genre, y = Popularity)) + 
  geom_boxplot(fill = "Lavender", color = "brown", outlier.color = "skyblue4") + 
  labs(title = "Popularity by Genre") + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1.0))

Comparing Music Popularity Trends

# Line chart comparing popularity trends over years for Spotify top hits
ggplot(music.2, aes(x = Year, y = Popularity)) +
  geom_line() +
  labs(title = "Popularity Trends Over Years")

Animated Plot

# Create the initial plot
p <- ggplot(music.2, aes(x = Year, y = Popularity, color = Main_Genre)) +
  geom_point() +
  labs(title = "Popularity Over Years") +
  theme_minimal()

# Animate the plot by 'Year'
animated_plot <- p +
  transition_time(Year) +
  ease_aes('linear')

# Render the animation
animate(animated_plot, nframes = 200, fps = 10)

Interactive Plot

# Create a scatter plot with plotly
plot_ly(data = music.2, x = ~Danceability, y = ~Energy, color = ~Main_Genre,
        type = 'scatter', mode = 'markers', marker = list(size = 10)) %>%
  layout(title = 'Danceability vs Energy by Genre',
         xaxis = list(title = 'Danceability'),
         yaxis = list(title = 'Energy'))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors