-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_twitter_network.R
66 lines (51 loc) · 1.33 KB
/
plot_twitter_network.R
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# plot_twitter_network.R
#
# Contributors:
#
# What this file does:
# * Transforms returned Twitter data into a network of connections
# * Plots retweet and Mentions networks
install.packages("ggraph","tidygraph")
yes
# --- Libraries --- #
library(readr) # read/write files
library(dplyr) # data manip
library(tidyr) # data manip
library(tidygraph) # network data
library(ggraph) # network viz
# --- Load Data --- #
tweets <- read_rds("data/fox_cnn.Rds")
# --- Retweet Network --- #
rt <-
tweets %>%
filter(is_retweet == TRUE)
rt_edge <-
rt %>%
select(from = screen_name,
to = retweet_screen_name
) %>%
filter(from != to) %>% # filter out people who retweet themselves
distinct() #only unique retweets, so if A retweets B 2 times, only 1 time is counted
rt_network <-
as_tbl_graph(rt_edge)
ggraph(rt_network, layout = 'stress') +
geom_node_point() + geom_edge_link() +
theme_void()
ggraph(rt_network, layout = 'kk') +
geom_node_point() + geom_edge_link(alpha = 0.2) +
theme_void()
ggraph(rt_network, layout = 'fr') +
geom_node_point() + geom_edge_link(alpha = 0.2) +
theme_void()
# --- Mentions Network --- #
mnt <-
tweets %>%
select(
from = screen_name,
to = mentions_screen_name
) %>%
filter(to != "NA")
mnt2 <-
mnt %>%
unnest_longer(to) %>%
filter(from != to)