Let’s remove English stop words.
stop <- c(
"a", "about", "above", "across", "after", "again", "against", "all", "almost", "alone",
"along", "already", "also", "although", "always", "am", "among", "an", "and", "another",
"any", "anybody", "anyone", "anything", "anywhere", "are", "area", "areas", "aren't", "around",
"as", "ask", "asked", "asking", "asks", "at", "away", "b", "back", "backed", "backing", "backs",
"be", "became", "because", "become", "becomes", "been", "before", "began", "behind", "being",
"beings", "below", "best", "better", "between", "big", "both", "but", "by", "c", "came", "can",
"cannot", "can't", "case", "cases", "certain", "certainly", "clear", "clearly", "come", "could",
"couldn't", "d", "did", "didn't", "differ", "different", "differently", "do", "does", "doesn't",
"doing", "done", "don't", "down", "downed", "downing", "downs", "during", "e", "each", "early",
"either", "end", "ended", "ending", "ends", "enough", "even", "evenly", "ever", "every", "everybody",
"everyone", "everything", "everywhere", "f", "face", "faces", "fact", "facts", "far", "felt", "few",
"find", "finds", "first", "for", "four", "from", "full", "fully", "further", "furthered", "furthering",
"furthers", "g", "gave", "general", "generally", "get", "gets", "give", "given", "gives", "go", "going",
"good", "goods", "got", "great", "greater", "greatest", "group", "grouped", "grouping", "groups", "h",
"had", "hadn't", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "her", "here",
"here's", "hers", "herself", "he's", "high", "higher", "highest", "him", "himself", "his", "how",
"however", "how's", "i", "i'd", "if", "i'll", "i'm", "important", "in", "interest", "interested",
"interesting", "interests", "into", "is", "isn't", "it", "its", "it's", "itself", "i've", "j", "just",
"k", "keep", "keeps", "kind", "knew", "know", "known", "knows", "l", "large", "largely", "last", "later",
"latest", "least", "less", "let", "lets", "let's", "like", "likely", "long", "longer", "longest", "m",
"made", "make", "making", "man", "many", "may", "me", "member", "members", "men", "might", "more", "most",
"mostly", "mr", "mrs", "much", "must", "mustn't", "my", "myself", "n", "necessary", "need", "needed",
"needing", "needs", "never", "new", "newer", "newest", "next", "no", "nobody", "non", "noone", "nor",
"not", "nothing", "now", "nowhere", "number", "numbers", "o", "of", "off", "often", "old", "older",
"oldest", "on", "once", "one", "only", "open", "opened", "opening", "opens", "or", "order", "ordered",
"ordering", "orders", "other", "others", "ought", "our", "ours", "ourselves", "out", "over", "own", "p",
"part", "parted", "parting", "parts", "per", "perhaps", "place", "places", "point", "pointed", "pointing",
"points", "possible", "present", "presented", "presenting", "presents", "problem", "problems", "put", "puts",
"q", "quite", "r", "rather", "really", "right", "room", "rooms", "s", "said", "same", "saw", "say", "says",
"second", "seconds", "see", "seem", "seemed", "seeming", "seems", "sees", "several", "shall", "shan't", "she",
"she'd", "she'll", "she's", "should", "shouldn't", "show", "showed", "showing", "shows", "side", "sides",
"since", "small", "smaller", "smallest", "so", "some", "somebody", "someone", "something", "somewhere",
"state", "states", "still", "such", "sure", "t", "take", "taken", "than", "that", "that's", "the", "their",
"theirs", "them", "themselves", "then", "there", "therefore", "there's", "these", "they", "they'd", "they'll",
"they're", "they've", "thing", "things", "think", "thinks", "this", "those", "though", "thought", "thoughts",
"three", "through", "thus", "to", "today", "together", "too", "took", "toward", "turn", "turned", "turning",
"turns", "two", "u", "under", "until", "up", "upon", "us", "use", "used", "uses", "v", "very", "w", "want",
"wanted", "wanting", "wants", "was", "wasn't", "way", "ways", "we", "we'd", "well", "we'll", "wells", "went",
"were", "we're", "weren't", "we've", "what", "what's", "when", "when's", "where", "where's", "whether", "which", "while",
"who", "whole", "whom", "who's", "whose", "why", "why's", "will", "with", "within", "without", "won't",
"work", "worked", "working", "works", "would", "wouldn't", "x", "y", "year", "years", "yes"
, "yet", "you",
"you'd", "you'll", "young", "younger", "youngest", "your", "you're", "yours", "yourself", "yourselves", "you've", "z", "unto", "thou", "thy", "thee")
stop word 제거
# Remove stop words from words
words[!words %in% stop] -> words
# Create a frequency table
word_freq <- table(unlist(words))
# Sort the frequency table in descending order
sorted_word_freq <- sort(word_freq, decreasing = TRUE)
# 데이터 프레임으로 바꾸기
df_word <- as.data.frame(sorted_word_freq, stringsAsFactors = F)
# 변수명 수정
df_word <- rename(df_word,
word = Var1,
freq = Freq)
# 상위 20개 추출
top20 <- df_word %>%
arrange(desc(freq)) %>%
head(20)
top20
## word freq
## 1 god 230
## 2 lord 206
## 3 land 187
## 4 father 169
## 5 jacob 166
## 6 sons 158
## 7 son 148
## 8 joseph 138
## 9 abraham 121
## 10 earth 121
## 11 behold 118
## 12 name 101
## 13 wife 101
## 14 called 98
## 15 ye 96
## 16 hand 88
## 17 house 86
## 18 pass 82
## 19 brother 81
## 20 brethren 80
단어 빈도 막대 그래프 생성
library(ggplot2)
order <- arrange(top20, freq)$word
ggplot(data = top20, aes(x = word, y = freq)) +
ylim(0, 250) +
geom_col() +
coord_flip() +
scale_x_discrete(limit = order) +
geom_text(aes(label = freq), hjust = -0.3)

워드 클라우드 생성
library(wordcloud)
## Warning: 패키지 'wordcloud'는 R 버전 4.3.2에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: RColorBrewer
library(RColorBrewer)
pal <- brewer.pal(8,"Blues")
set.seed(1234)
wordcloud(words = df_word$word,
freq = df_word$freq,
min.freq = 10,
max.words = 300,
random.order = F,
rot.per = .1,
scale = c(4, 0.2),
colors = pal)
