일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- ajoubamboo
- flask
- Linear Regression
- Server
- web
- rcv
- mongoDB
- Data Analysis
- 크롤링
- TensorFlow
- rstudio
- Python
- 웹크롤링
- Rfacebook
- python3
- Ajou University
- 파이썬
- 선형회귀
- Web Programming
- 오버워치
- Crawling
- chaining
- 머신러닝
- Barplot
- 파이썬3
- R
- 오버로그
- Cssselector
- webcrawling
- Programming
- Today
- Total
R is well
[R 패키지 소개] R package introduction : Rfacebook 본문
Rfacebook.R아보자
안상호
2016년 12월 5일
1. Rfacebook 크롤링
Rfacebook
은 facebook API를 사용하여
token을 생성해야만 함수가 이용가능합니다.
1.1. 패키지 install & library
먼저 Rfacebook
패키지를 설치해보도록 하겠습니다.
rcv("Rfacebook")
## Loading required package: httr
## Loading required package: rjson
## Loading required package: httpuv
##
## Attaching package: 'Rfacebook'
## The following object is masked from 'package:methods':
##
## getGroup
1.2. token 생성
token을 생성하기 위해서는 fbOAuth
함수를 사용하여야 하는데,
facebook for developers
에서 app
을 생성하여 id
와 secret code
를
발급 받아야만 합니다.
fb_oauth <- fbOAuth(app_id, app_secret, extended_permissions = TRUE)
save(fb_oauth, file="fb_oauth")
load("fb_oauth")
token 생성 과정을 완료하시고 이를 저장하셨다면,
필요한때면 언제든 다시 load하여 이용가능합니다.
1.3. crawling
이제 이를 바탕으로 데이터를 수집해 보도록 하겠습니다.
Rfacebook 을 통한 데이터 수집은 주로 getPage()
함수를 이용합니다.
이를 활용하여 아주대학교 대나무숲의 포스트 5000개를 크롤링 해보겠습니다.
AjouBamboo <- getPage(page="ajoubamboo", token=fb_oauth, n = 20000, since='2015/01/01', until='2016/12/05')
## 80 posts 160 posts 240 posts 320 posts 399 posts 479 posts 559 posts 639 posts 718 posts 798 posts 878 posts 957 posts 1036 posts 1112 posts 1192 posts 1272 posts 1352 posts 1432 posts 1512 posts 1592 posts 1672 posts 1751 posts 1830 posts 1910 posts 1990 posts 2070 posts 2150 posts 2230 posts 2310 posts 2390 posts 2470 posts 2549 posts 2629 posts 2708 posts 2788 posts 2867 posts 2947 posts 3027 posts 3107 posts 3187 posts 3267 posts 3346 posts 3426 posts 3504 posts 3584 posts 3664 posts 3744 posts 3824 posts 3903 posts 3983 posts 4063 posts 4142 posts 4222 posts 4301 posts 4381 posts 4461 posts 4540 posts 4620 posts 4700 posts 4780 posts 4860 posts 4940 posts 5019 posts 5098 posts 5178 posts 5257 posts 5337 posts 5417 posts 5497 posts 5576 posts 5656 posts 5736 posts 5816 posts 5896 posts 5974 posts 6054 posts 6134 posts 6214 posts 6294 posts 6373 posts 6452 posts 6532 posts 6612 posts 6692 posts 6763 posts
str(AjouBamboo) # 데이터 구조확인
## 'data.frame': 6763 obs. of 10 variables:
## $ from_id : chr "383934228440070" "383934228440070" "383934228440070" "383934228440070" ...
## $ from_name : chr "아주대학교 대나무숲" "아주대학교 대나무숲" "아주대학교 대나무숲" "아주대학교 대나무숲" ...
## $ message : chr "아대숲 #21108번째 선구자의 외침 :\n\n조별과제하는데 저만 빼는 듯한 느낌이 들어서 슬픕니다. 물론 저를 빼는 이유가 있겠지만 좀 슬"| __truncated__ "아대숲 #21107번째 선구자의 외침 :\n\n그냥 그런날있잖아요 무얼해도 무기력하고 내자신은 얼마나 초라해보이던지 자존감 급하락에 나"| __truncated__ "[21102-21106]#학교_상황에_대하여\n\n아대숲 #21106번째 선구자의 외침 :\n학교 상황이 참으로 쪽팔리네요. 그간 나온 내용이 사실이라"| __truncated__ "[21100-21101]#혼잣말모음\n\n아대숲 #21101번째 선구자의 외침 :\n아직도 나는 너한테 하고싶은 말들이 너무 많다. 내가 너한테 고백한"| __truncated__ ...
## $ created_time : chr "2016-12-04T16:07:39+0000" "2016-12-04T16:06:13+0000" "2016-12-04T16:00:28+0000" "2016-12-04T15:49:49+0000" ...
## $ type : chr "status" "status" "status" "status" ...
## $ link : chr NA NA NA NA ...
## $ id : chr "383934228440070_712853815548108" "383934228440070_712853135548176" "383934228440070_712850102215146" "383934228440070_712842015549288" ...
## $ likes_count : num 7 43 10 4 8 8 4 26 15 3 ...
## $ comments_count: num 2 5 1 0 2 3 5 10 2 2 ...
## $ shares_count : num 0 0 0 0 0 0 0 0 0 0 ...
2. 좋아요와 댓글 간 상관관계
이제 구한 데이터를 바탕으로 간단한 시각화를 해보도록 하겠습니다.
제가 구한 data에서 첫번째로 주목한 것은 바로
포스트에 있는 ‘좋아요’ 와 ‘댓글’ 의 정보입니다.
2.1. 상관계수
첫 번째로 알아볼 것은 상관계수
입니다.
‘1’ ~ ‘-1’ 까지의 수로 이루어져 있으며,
이 두 수에 근접할 수록 상관 관계가 높음을 의미하며,
또한 +
는 양의 상관관계, -
는 음의 상관관계를 의미합니다.
- 먼저
cor
함수를 이용해 상관 계수를 구해보도록 하겠습니다.
AJcor <- cor(AjouBamboo[,c(8:10)])
AJcor
## likes_count comments_count shares_count
## likes_count 1.0000000 0.4475504 0.4208302
## comments_count 0.4475504 1.0000000 0.3814045
## shares_count 0.4208302 0.3814045 1.0000000
- 우선 패키지 load
rcv(c("corrplot", # 상관관계 시각화
"magrittr")) # %>% (체이닝) 사용 가능
- 이제 이것을 시각화해서 나타내보도록 하겠습니다.
AJcor %>%
corrplot(method='shade',
shade.col=NA,
tl.col='black',
tl.srt=45)
Tip.
- method 를 shade 대신 number 로 주면 숫자로
나타낼 수 있습니다.
AJcor %>%
corrplot(method='number',
shade.col=NA,
tl.col='black',
tl.srt=45)
여기서 알 수 있는 것은 ‘좋아요’ 와 ‘댓글’ 간 에는
긴밀한 양의 상관 관계가 있다는 것입니다.
2.2. ggplot으로 시각화
양의 상관 관계를 그래프로 나타내 보도록 하겠습니다.
rcv(c("ggplot2", "plotly", "hexbin", "quantreg"))
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:httr':
##
## config
## The following object is masked from 'package:graphics':
##
## layout
## Loading required package: SparseM
##
## Attaching package: 'SparseM'
## The following object is masked from 'package:base':
##
## backsolve
plotly
: interactive 한 그래프를 그리기 위함hexbin
:geom_point
보다 시각적인 이해에 더 좋음quantreg
:geom_quantile
을 사용하기 위함
lc <- ggplot(data = AjouBamboo, aes(likes_count, comments_count))
lc <- lc + geom_hex()
lc <- lc + geom_smooth(method = lm)
lc <- lc + geom_quantile(colour = "red", size = 2, alpha = 0.5)
lc
## Smoothing formula not specified. Using: y ~ x
ggplotly(lc)
## Smoothing formula not specified. Using: y ~ x
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomQuantile() has yet to be implemented in plotly.
## If you'd like to see this geom implemented,
## Please open an issue with your example code at
## https://github.com/ropensci/plotly/issues
'프로그래밍 > R' 카테고리의 다른 글
[Data Analysis] 데이터 분석 기초 (0) | 2017.10.08 |
---|---|
[R Tip] R & Rstudio 관련 Tip_1 (5) | 2016.12.08 |