互動式 JavaScript 圖形
另一個廣泛使用的互動式繪圖框架是 D3.js。它對建立網路圖有一套特定規格,而我們可以在 R 中自動產生這類圖。好處是只要幾行程式碼,你就能建立完整可互動的 D3.js 圖形。缺點是,真正的自訂通常必須直接編輯 R 產出的 JavaScript 原始碼(這超出本課程的範圍)。儘管如此,你仍可透過 d3network 函式庫,在 R 中快速輕鬆地建立漂亮的 D3.js 網路圖。在本課中,我們會載入 #rstats 的 Twitter 資料集並加入社群歸屬。接著,我們會擷取少數幾個社群形成子圖,並繪製 D3.js 網路圖。
推文圖形物件 retweet_samp 已可使用。
本練習屬於課程
案例研究:R 的網路分析
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Run this to see the static version of the plot
ggplot(ggnetwork(retweet_samp, arrow.gap = 0.01),
aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "black") +
geom_nodes(aes(color = as.factor(comm))) +
theme_blank()
# Convert retweet_samp to a networkD3 object
nd3 <-igraph_to_networkD3(___, V(___)$___)
# View the data structure
str(nd3)