本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

Rでウェブ解析:ツイート内容に含まれる単語をネットワーク図で表示

ツイート内容に含まれる単語の関係(バイグラム)をネットワーク図で示すことができれば面白いかもと思い、RMecab、twitteR、qgraphパッケージを組み合わせコマンドを作成しました。

使用パッケージのインストール

過去記事をご覧ください。

スポンサーリンク

実行コマンドの紹介

コマンドは作成途中にTwitterのアカウントロック騒動がありましたので、キーワード「アカウントロック」で検索し、バイグラムの出現数50以上を図をプロットしてみました。

#ライブラリの読み込み
library("twitteR")
library("RMeCab")
library("wordcloud")
library("RColorBrewer")
library("qgraph")

#情報の入力
consumerKey <- "Consumer Key (API Key)を入力"
consumerSecret <- "Consumer Secret (API Secret)を入力"

#処理の準備
twitCred <- OAuthFactory$new(consumerKey = consumerKey,
                             consumerSecret = consumerSecret,
                             requestURL = "https://api.twitter.com/oauth/request_token",
                             accessURL = "https://api.twitter.com/oauth/access_token",
                             authURL = "http://api.twitter.com/oauth/authorize")

#認証の実行
twitCred$handshake()
registerTwitterOAuth(twitCred)

#キーワードで検索
SearchWords <- c("アカウントロック")
TwGetDF <- twListToDF(searchTwitter(searchString = SearchWords, #検索キーワード
                                    n = 1000, #取得するツイート数
                                    #since = YYYY-MM-DD #取得する期間
))

###データの作成######
res <- docDF(TwGetDF, column = 1, type = 1, N = 2, nDF = TRUE) #バイグラムデータの作成
res <- cbind(res[, 1:2], rowSums(res[, 5:ncol(res)])) #単語の出現率を集計
res <- res[order(res[,3], decreasing = TRUE),] #出現数を降順で並び替え
########

#グラフのプロット
#出現数の設定
PlotData <- subset(res, res[, 3] > 50)

#MACでの文字化け防止
par(family = "HiraKakuProN-W3")

#グラフのプロット
qgraph(PlotData, layout = "spring")

プロット結果

矢印の色が濃いほど出現数が大きい組み合わせとなります。現在はアカウントロックの問題は解決したようです。

Rplot01

少しでも、あなたのウェブや実験の解析が楽になりますように!!

スポンサーリンク
Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.