Analyzing Social Media Data in R
Sowmya Vivek
Data Science Coach
# View the retweet network
print.igraph(nw_rtweet)
IGRAPH e7e618c DN-- 21 39 --
+ attr: name (v/c), followers (v/c)
+ edges from e7e618c (vertex names):
[1] w3daily ->RealFirstBuzz w3daily ->RealFirstBuzz
[3] w3daily ->Giasaysthat w3daily ->RealFirstBuzz
[5] VanesEtim ->PotionVanity VanesEtim ->DAVIDxCGN
[7] VanesEtim ->PotionVanity VanesEtim ->Avinash_galaxy
[9] VanesEtim ->PotionVanity VanesEtim ->BklynLeague
[11] RedNileShop->Macaw_Blink RedNileShop->leuqimcouture
[13] RedNileShop->StoreDiv RedNileShop->PotionVanity
[15] RedNileShop->SCISSORSAPPAREL RedNileShop->Themoon12961958
+ ... omitted several edges
# Create the base network plot
set.seed(1234)
plot.igraph(nw_rtweet)
# Format the network plot with attributes
set.seed(1234)
plot(nw_rtweet, asp = 9/16,
vertex.size = 10,
vertex.color = "lightblue",
edge.arrow.size = 0.5,
edge.color = "black",
vertex.label.cex = 0.9,
vertex.label.color = "black")
# Create a variable for out-degree
deg_out <- degree(nw_rtweet, mode = c("out"))
deg_out
vert_size <- (deg_out * 2) + 10
# Assign vert_size to vertex size attribute and plot network
set.seed(1234)
plot(nw_rtweet, asp = 9/16,
vertex.size = vert_size,
vertex.color = "lightblue",
edge.arrow.size = 0.5,
edge.color = "black",
vertex.label.cex = 1.2,
vertex.label.color = "black")
# Import the followers count data frame
followers <- readRDS("follower_count.rds")
# View the follower count
head(followers)
screen_name followers_count
<fct> <dbl>
adyo312 58
AllesUndNix_ 18
Avinash_galaxy 1536
BklynLeague 40
DAVIDxCGN 267
Giasaysthat 9139
# Categorize high and low follower count
followers$follow <- ifelse(followers$followers_count > 500, "1", "0")
# View the data frame with the new column
head(followers)
screen_name followers_count follow
<fct> <dbl> <chr>
adyo312 58 0
AllesUndNix_ 18 0
Avinash_galaxy 1536 1
BklynLeague 40 0
DAVIDxCGN 267 0
Giasaysthat 9139 1
# Assign external network attributes to retweet network
V(nw_rtweet)$followers <- followers$follow
# View the vertex attributes
vertex_attr(nw_rtweet)
# Set the vertex colors for the plot
sub_color <- c("lightgreen", "tomato")
set.seed(1234)
plot(nw_rtweet, asp = 9/16,
vertex.size = vert_size,
edge.arrow.size = 0.5,
vertex.label.cex = 1.3,
vertex.color = sub_color[as.factor(vertex_attr(nw_rtweet, "followers"))],
vertex.label.color = "black",
vertex.frame.color = "grey")
Analyzing Social Media Data in R