Data Visualization and Analysis of Taylor Swift’s Song Lyrics
Taylor Swift
Taylor Swift
Taylor Swift 6 张专辑(album)96首歌的歌词 6列数据
探索性数据分析
文本挖掘
接触到一个新的函数:stringr包中的str_count()
帮助文档中的例子
library(stringr)
fruit <- c("apple", "banana", "pear", "pineapple")
str_count(fruit, "a")
#输出结果是
[1] 1 3 1 1
作用是统计每个字符串中符合特定规则的字符的数量 比如
str_count("A B C","\\S+")
输出的是“A B C”字符串中非空字符的数量(\S+是正则表达式的一种写法,自己还没有掌握) 读入数据
lyrics<-read.csv("taylor_swift_lyrics_1.csv",header=T)
head(lyrics)
计算每句歌词的长度
library(stringr)
lyrics$length<-str_count(lyrics$lyric,"\\S+")
head(lyrics)
计算每首歌的歌词长度
library(dplyr)
length_df<-lyrics%>%
group_by(track_title)%>%
summarise(length=sum(length))
head(length_df)
dim(length_df)
Top10wordCount<-arrange(length_df,desc(length))%>%
slice(c(1:10))
library(ggplot2)
ggplot(Top10wordCount,aes(x=reorder(track_title,length),y=length))+
geom_col(aes(fill=track_title))+coord_flip()+
ylab("Word count") + xlab ("") +
ggtitle("Top 10 songs in terms of word count") +
theme_minimal()+
theme(legend.position = "none")
image.png 从上图可以看到,单词数量最多的歌是 End Game 排名第二的是 Out of the Woods
Top10wordCount<-arrange(length_df,length)%>%
slice(c(1:10))
library(RColorBrewer)
color<-rainbow(10)
ggplot(Top10wordCount,aes(x=reorder(track_title,-length),y=length))+
geom_col(aes(fill=track_title))+coord_flip()+
ylab("Word count") + xlab ("") +
ggtitle("Top 10 songs in terms of word count") +
theme_minimal()+scale_fill_manual(values = color)+
theme(legend.position = "none")+
theme(legend.position = "none")
image.png 单词数量最少的歌是 Sad Beautiful Tragic,发布于2012年,是 Red 这张专辑中的歌
ggplot(length_df, aes(x=length)) +
geom_histogram(bins=30,aes(fill = ..count..)) +
geom_vline(aes(xintercept=mean(length)),
color="#FFFFFF", linetype="dashed", size=1) +
geom_density(aes(y=25 * ..count..),alpha=.2, fill="#1CCCC6") +
ylab("Count") + xlab ("Legth") +
ggtitle("Distribution of word count") +
theme_minimal()
image.png
lyrics %>%
group_by(album,year) %>%
summarise(length = sum(length))%>%
na.omit()-> length_df_album
length_df_album
ggplot(length_df_album, aes(x= reorder(album,-length), y=length)) +
geom_bar(stat='identity', fill="#1CCCC6") +
ylab("Word count") + xlab ("Album") +
ggtitle("Word count based on albums") +
theme_minimal()
image.png
length_df_album %>%
arrange(desc(year)) %>%
ggplot(., aes(x= factor(year), y=length, group = 1)) +
geom_line(colour="#1CCCC6", size=1) +
ylab("Word count") + xlab ("Year") +
ggtitle("Word count change over the years") +
theme_minimal()+
geom_point(aes(x=factor(year),y=length,
size=length,color=factor(year)),
alpha=0.5)+
scale_size_continuous(range=c(5,15))+
theme(legend.position = "none")
image.png
library("tm")
library("wordcloud")
lyrics_text <- lyrics$lyric
lyrics_text<- gsub('[[:punct:]]+', '', lyrics_text)
lyrics_text<- gsub("([[:alpha:]])\1+", "", lyrics_text)
docs <- Corpus(VectorSource(lyrics_text))
docs <- tm_map(docs, content_transformer(tolower))
docs <- tm_map(docs, removeWords, stopwords("english"))
tdm <- TermDocumentMatrix(docs)
m <- as.matrix(tdm)
word_freqs = sort(rowSums(m), decreasing=TRUE)
lyrics_wc_df <- data.frame(word=names(word_freqs), freq=word_freqs)
lyrics_wc_df <- lyrics_wc_df[1:300,]
set.seed(1234)
wordcloud(words = lyrics_wc_df$word, freq = lyrics_wc_df$freq,
min.freq = 1,scale=c(1.8,.5),
max.words=200, random.order=FALSE, rot.per=0.15,
colors=brewer.pal(8, "Dark2"))
剩下的部分有时间回来补上