人気の用語を可視化する

Rで学ぶソーシャルメディアデータ分析

Vivek Vijayaraghavan

Data Science Coach

レッスン概要

  • テキストコーパスから最頻用語を抽出
  • カスタムストップワードを除去し、コーパスを精錬
  • 棒グラフとワードクラウドで可視化
Rで学ぶソーシャルメディアデータ分析

用語頻度

  • 用語頻度(各単語の出現回数)を抽出
# Extract term frequency
library(qdap)
term_count  <-  freq_terms(twt_corpus_final, 60)
term_count
Rで学ぶソーシャルメディアデータ分析

用語頻度

用語頻度

Rで学ぶソーシャルメディアデータ分析

カスタムストップワードの除去

# Create a vector of custom stop words
custom_stop <- c("obesity", "can", "amp", "one", "like", "will", "just", 
                "many", "new", "know", "also", "need", "may", "now", 
                "get", "s", "t", "m", "re")
# Remove custom stop words
twt_corpus_refined <- tm_map(twt_corpus_final,removeWords, custom_stop)
Rで学ぶソーシャルメディアデータ分析

コーパス精錬後の用語数

# Term count after refining corpus
term_count_clean <- freq_terms(twt_corpus_refined, 20)
term_count_clean
Rで学ぶソーシャルメディアデータ分析

コーパス精錬後の用語頻度

コーパス精錬後の用語頻度

  • 肥満管理プログラムを宣伝するブランドは、これらの用語を分析できる
Rで学ぶソーシャルメディアデータ分析

人気用語の棒グラフ

  • 出現回数が50超の用語で棒グラフを作成
  • 棒グラフは人気の用語を直感的に要約
# Create a subset dataframe
term50 <- subset(term_count_clean, FREQ > 50)
Rで学ぶソーシャルメディアデータ分析

最頻用語の棒グラフ

library(ggplot2)
# Create a bar plot of frequent terms
ggplot(term50, aes(x = reorder(WORD,  -FREQ),  y = FREQ)) +
       geom_bar(stat = "identity", fill = "blue") + 
       theme(axis.text.x = element_text(angle = 45, hjust = 1))
Rで学ぶソーシャルメディアデータ分析

人気用語の棒グラフ

人気用語の棒グラフ

Rで学ぶソーシャルメディアデータ分析

ワードクラウド

  • ワードクラウドで頻出語を可視化
  • ワードクラウドは単語で構成された画像
  • 単語サイズが頻度を示す
  • キャンペーンの効果的な訴求画像に最適
  • ブランドのメッセージを伝え、人気用語を強調
Rで学ぶソーシャルメディアデータ分析

最小頻度に基づくワードクラウド

  • wordcloud() 関数でワードクラウドを作成
# Create a word cloud based on min frequency
library(wordcloud)
wordcloud(twt_corpus_refined, min.freq = 20, colors = "red", 
          scale = c(3,0.5), random.order = FALSE)
Rで学ぶソーシャルメディアデータ分析

最小頻度に基づくワードクラウド

最小頻度に基づくワードクラウド

Rで学ぶソーシャルメディアデータ分析

カラフルなワードクラウド

# Create a colorful word cloud
library(RColorBrewer)
wordcloud(twt_corpus_refined, max.words = 100, 
          colors = brewer.pal(6,"Dark2"), scale = c(2.5,.5),
          random.order = FALSE)
Rで学ぶソーシャルメディアデータ分析

カラフルなワードクラウド

色分けしたワードクラウド

Rで学ぶソーシャルメディアデータ分析

練習しましょう!

Rで学ぶソーシャルメディアデータ分析

Preparing Video For Download...