Lecture12: Network representation: Part2

最終課題

コード内容の注意

  • 授業で配布したshinyアプリケーションのフォルダを使用し、使用テキストだけ変更した課題は、受理しません。(授業で学んだことを応用して、コードを書いてください)
  • サンプル等で公開されているコードをコピーするのは控えてください。(利用する場合は。自分でコードの意味を説明できるようにしてください)

作成上の注意点

  • shinyのファイル (ui.R, server.R)の前に、動作確認をR scriptファイル上で確認

  • ui.Rは、フォームやタブを追加したら、その都度動作を確認

  • shinyアプリケーション作成時に、エラーが起きた場合は、直前の動いていた状態に戻れるように、少しずつ作成すること

Network representation

任意の区切りによる共起ネットワーク

英文サンプル

test2<-"With the COVID-19 pandemic worsening in areas of Japan beyond the capital, the government is expected to extend the nation’s second state of emergency Wednesday to cover Osaka, Kyoto and Hyogo prefectures in the Kansai region, as well as Aichi and Gifu prefectures."

“,” or “.”区切り

lineLst <- unlist(strsplit(test2, ",|\\."))
lineLst[3:4]
## [1] " Kyoto and Hyogo prefectures in the Kansai region"
## [2] " as well as Aichi and Gifu prefectures"

netwkPairsBis.R(任意の区切り対応ペア単語取得用関数)の読み込み

source("netwkPairsBis.R")

共起ペア情報の取得関数(4番目の区切り)

getPairsL(lineLst[4])
##      [,1]          [,2]         
## pair "as"          "well"       
## pair "as"          "as"         
## pair "aichi"       "as"         
## pair "and"         "as"         
## pair "as"          "gifu"       
## pair "as"          "prefectures"
## pair "as"          "well"       
## pair "aichi"       "well"       
## pair "and"         "well"       
## pair "gifu"        "well"       
## pair "prefectures" "well"       
## pair "aichi"       "as"         
## pair "and"         "as"         
## pair "as"          "gifu"       
## pair "as"          "prefectures"
## pair "aichi"       "and"        
## pair "aichi"       "gifu"       
## pair "aichi"       "prefectures"
## pair "and"         "gifu"       
## pair "and"         "prefectures"
## pair "gifu"        "prefectures"

共起ペア頻度情報の取得関数(すべての区切り)

pFreq <- getPairsFreqL(lineLst)
head(pFreq)
##      Term1     Term2 Freq
## 1      the      with    2
## 2 covid-19      with    1
## 3 pandemic      with    1
## 4     with worsening    1
## 5       in      with    1
## 6    areas      with    1

igraphによるネットワーク描画

library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
pFreq_s<-pFreq[pFreq$Freq>1,]

ネットワーク描画

エッジ幅とノードの大きさを調整

wng<-as.undirected(graph.data.frame(pFreq_s))
E(wng)$weight<-pFreq_s$Freq
deg<-degree(wng)
plot(wng,edge.width=5*E(wng)$weight/max(E(wng)$weight),vertex.size=30*(deg/max(deg)))

任意の区切りによる共起ネットワーク

日本語サンプル

test3<-"菅 総理 大臣 は 、 大阪 、 愛知 、 福岡 など 合わせ て 7 つ の 府 県 を 対象 に 、 新型 コロナ ウイルス 対策 の 特別 措置 法 に 基づく 、 緊急 事態 宣言 を 出し まし た 。 
期間 は 来月 7 日 まで で 、 これ に よっ て 宣言 の 対象 地域 は 1 1 の 都 府 県 に 拡大 さ れ ます 。 "

“、” or “。” or ”\n(改行)”区切り

lineLstJa <- unlist(strsplit(test3, "、|。|\n"))
lineLstJa <- lineLstJa[lineLstJa != " "]
lineLstJa[3:4]
## [1] " 愛知 "                                        
## [2] " 福岡 など 合わせ て 7 つ の 府 県 を 対象 に "

共起ペア頻度情報の取得関数(すべての区切り)

pFreq <- getPairsFreqL(lineLstJa)
head(pFreq)
##   Term1 Term2 Freq
## 1  総理    菅    1
## 2  大臣    菅    1
## 3    は    菅    1
## 4  大臣  総理    1
## 5    は  総理    1
## 6    は  大臣    1

ネットワーク図の文字が文字化けする場合

IPAexfontのttfファイルのコピーの方法はLecture10を参照

par(family = "IPAexGothic")

ネットワーク描画

エッジ幅とノードの大きさを調整

wng_ja<-as.undirected(graph.data.frame(pFreq))
E(wng_ja)$weight<-pFreq$Freq
deg<-degree(wng_ja)
plot(wng_ja,edge.width=5*E(wng_ja)$weight/max(E(wng_ja)$weight),vertex.size=30*(deg/max(deg)))

Shinyでのインタラクティブなネットワーク描画 (日本語編)

library(shiny)
library(d3Network)
runApp("app_netwk_ja")

おまけ: showtextパッケージ (中国語, 韓国語にも対応)

準備

ライブラリのロード

library(showtext)
## Loading required package: sysfonts
## Loading required package: showtextdb

フォントのインストール

ライブラリの初回ロード時に実行(2回目以降は不要)

font_install(source_han_serif())
head(font_files())

テキストのプロット

注意:showtextのフォントを使用すると、上のigraphの日本語の文字が文字化けしますので、Rのセッションを再起動して、ネットワーク描画を実行してください。

以下のコードは、新規にRファイルを作成して、実行確認してください

showtext_auto()
plot(0,0,type="n")
text(0,0,"我们外语学院",cex=5)
showtext_auto(FALSE)
showtext_auto()
plot(0,0,type="n")
text(0,0,"緊急事態",cex=5)
showtext_auto(FALSE)
showtext_auto()
plot(0,0,type="n")
text(0,0,"고맙습니다",cex=5)
showtext_auto(FALSE)