使用 R 分析 Facebook 社群網路教學

使用 Rfacebook 套件

Facebook 粉絲專頁

首先複製 facebook 粉絲專頁的 id,而常見的 id 有兩種,一種是純數字的,一種則是使用者自己命名的:

https://www.facebook.com/G-T-Wang-489529864441673/
https://www.facebook.com/humansofnewyork

接著使用 getPage 來取得粉絲專頁上面的文章資訊:

page.id <- "489529864441673" # G.T.Wang 粉絲專頁
page <- getPage(page.id, token, n = 300)
str(page)
'data.frame':	300 obs. of  10 variables:
 $ from_id       : chr  "489529864441673" "489529864441673" "489529864441673" "489529864441673" ...
 $ from_name     : chr  "G. T. Wang" "G. T. Wang" "G. T. Wang" "G. T. Wang" ...
 $ message       : chr  "這裡介紹如何設定標準網址(canonical link),讓自己網站可以在 Google 搜尋結果上呈現正確的網址,達到搜尋引擎最佳化的效果。" "本篇介紹如何改變 WordPress 文字編輯器的字型大小與顏色,讓原始的 HTML 程式碼更容易閱讀。" "一派胡塩酵素臭豆腐新市店是一家靠近南科的素食小吃,臭豆腐外酥內軟,用餐環境整潔衛生。" "Sci-Hub 是一個可以免費下載期刊論文全文電子檔的網站,推倒通往科學道路上的圍籬。" ...
 $ created_time  : chr  "2016-04-27T04:01:45+0000" "2016-04-27T00:25:27+0000" "2016-04-25T07:51:27+0000" "2016-04-24T04:59:53+0000" ...
 $ type          : chr  "link" "link" "link" "link" ...
 $ link          : chr  "http://blog.gtwang.org/web-development/google-canonical-link-setup-after-changing-url/" "http://blog.gtwang.org/wordpress/change-font-and-color-in-text-editor/" "http://blog.gtwang.org/life/yi-pai-hu-yan-stinky-tofu-xinshi-tainan/" "http://blog.gtwang.org/free/sci-hub-download-papers-for-free/" ...
 $ id            : chr  "489529864441673_1054145887980065" "489529864441673_1054059014655419" "489529864441673_1053005288094125" "489529864441673_1052385961489391" ...
 $ likes_count   : num  5 4 2 7 7 3 8 8 3 8 ...
 $ comments_count: num  0 0 1 0 0 0 10 7 4 14 ...
 $ shares_count  : num  0 0 1 0 2 1 0 3 1 1 ...

依據時間畫出網友的迴響數量:

## convert Facebook date format to R date format
format.facebook.date <- function(datestring) {
  date <- as.POSIXct(datestring, format = "%Y-%m-%dT%H:%M:%S+0000", tz = "GMT")
}
# aggregate metric counts over month
aggregate.metric <- function(metric) {
  m <- aggregate(page[[paste0(metric, "_count")]], list(month = page$month),
    mean)
  m$month <- as.Date(paste0(m$month, "-15"))
  m$metric <- metric
  return(m)
}
# create data frame with average metric counts per month
page$datetime <- format.facebook.date(page$created_time)
page$month <- format(page$datetime, "%Y-%m")
df.list <- lapply(c("likes", "comments", "shares"), aggregate.metric)
df <- do.call(rbind, df.list)
# visualize evolution in metric
library(ggplot2)
library(scales)
ggplot(df, aes(x = month, y = x, group = metric)) +
  geom_line(aes(color = metric)) +
  scale_y_log10("Average count per post",
  breaks = c(2, 10, 50, 100)) +
  theme_bw() +
  theme(axis.title.x = element_blank())

這是每篇文章平均的迴響狀況:

facebook-average-count-per-post-1

取出按「讚」數最高的文章:

top.post <- page[which.max(page$likes_count), ]
top.post
           from_id  from_name
62 489529864441673 G. T. Wang
                                                                    message
62 Zoolz 的終身 1TB 雲端備份空間現在大特價,只要 $39 美金,比買硬碟還便宜!
               created_time type
62 2016-01-15T23:54:46+0000 link
                                                                                 link
62 http://blog.gtwang.org/funny/get-1tb-zoolz-cold-storage-for-lifetime-subscription/
                                id likes_count comments_count shares_count
62 489529864441673_993084634086191         489              4            9

分析這一篇文章按讚的人:

post <- getPost(top.post$id, token, n = 1000, likes = TRUE, comments = FALSE)
users <- getUsers(post$likes$from_id, token)
head(sort(table(users$last_name), decreasing = TRUE))
  陳   林   黃   李 Chen   楊 
  42   31   18   17   14   14

畫出前 10 個姓氏:

top.last.name <- head(sort(table(users$last_name), decreasing = TRUE), n = 10)
top.last.name.df <- data.frame(last.name=rownames(top.last.name),
  count = top.last.name)
top.last.name.df$last.name <- reorder(top.last.name.df$last.name, top.last.name.df$count)
ggplot(data = top.last.name.df, aes(x = last.name, y = count)) +
  geom_bar(stat="identity")

facebook-last-name-barplot-1

Facebook 好友

由於 facebook 的 Graph API 在 2.0 版之後,將取得 facebook 好友相關資訊的權限停用了,所以現在我們無法直接使用 Rfacebook 來獲取 facebook 上好友的關係資料,這個問題在 stackoverflow 上也有許多人討論,不過由於這是 facebook 的政策改變所致,所以目前無解。

參考資料:ThinkToStart

R

6 留言

  1. CCY

    你好
    想請問設定完畢後,上面那些指令都是KEY在R語言上嗎?
    感恩

  2. jun

    你好,請問如果想抓某一個時間片段裡的粉專內容
    該怎麼寫比較好?
    謝謝

  3. 簡嘉宏

    你好,請問一下為何我會找不到 metric
    我跑aggregate.metric<-function(metric){m<-aggregate(page[[paste0(metric,"_count")]], list(month=page$month), mean)} 這部分沒有錯誤
    可是沒有出現 m ,R找不到她
    於是我單獨跑
    m<-aggregate(page[[paste0(metric,"_count")]], list(month=page$month), mean)
    出現metric
    非常謝謝您的教學,非常有用

  4. Ken

    不好意思我想請問一下 ,是自己的管理的社團才能分析嗎 ?別人未公開的社團是否也可以分析呢 ?

  5. CN

    請問:在 R STUDIO輸入完金鑰後,進行瀏覽器認證,但是出現Authentication complete.
    Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params, :
    Bad Request (HTTP 400). Failed to get an access token.
    我看了很多資料,都沒看懂如何解決,您能幫忙說明嗎?謝謝

Comments are Closed