library(tidyverse)
library(ggraph)
library(tidygraph)
library(tidytags)
library(leaflet)
d1 <- read_csv("googlesheets-tweets.csv")
d %>%
count(screen_name) %>%
arrange(desc(n))
## # A tibble: 187 x 2
## screen_name n
## <chr> <int>
## 1 MLCmath 43
## 2 JUMP_Math 13
## 3 DrMNikfar 9
## 4 staaSTALLIONS 9
## 5 ForestGlenGator 6
## 6 abmath7 5
## 7 Mathcutups 5
## 8 caob2018 4
## 9 Inv3_Math 4
## 10 iteachmathAll 4
## # … with 177 more rows
d %>%
count(screen_name) %>%
arrange(desc(n)) %>%
ggplot(aes(x = n)) +
geom_histogram()
n_tweets <- d %>%
count(screen_name)
n_tweets %>%
filter(n >= 2)
## # A tibble: 40 x 2
## screen_name n
## <chr> <int>
## 1 abmath7 5
## 2 anujgsj 2
## 3 bmeyring 2
## 4 caob2018 4
## 5 ChiefBearspaw 2
## 6 DeniseGB55 2
## 7 DrMNikfar 9
## 8 EarlyMathEDC 3
## 9 eriksonmath 2
## 10 eschoolnews 2
## # … with 30 more rows
codes <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vQPer3xRgn56MOIznyAOPTFvFEci47EVkIOoSYN5GuGcYiDw5t24jExKHBdoGJlRryAGxi2yFlShd42/pub?output=csv")
codes %>%
count(code) %>%
arrange(desc(n))
## # A tibble: 8 x 2
## code n
## <chr> <int>
## 1 teacher 10
## 2 school 9
## 3 organization 7
## 4 unclear 5
## 5 coach 4
## 6 professor 3
## 7 hashtag 1
## 8 media 1
codes %>%
count(code, `also-parent`) %>%
filter(!is.na(`also-parent`))
## # A tibble: 3 x 3
## code `also-parent` n
## <chr> <dbl> <int>
## 1 professor 1 1
## 2 teacher 1 2
## 3 unclear 1 1
edgelist <- d %>%
tidytags::create_edgelist()
edgelist %>%
count(edge_type) %>%
arrange(desc(n))
## # A tibble: 4 x 2
## edge_type n
## <chr> <int>
## 1 mention 229
## 2 retweet 105
## 3 quote-tweet 15
## 4 reply 6
edgelist %>%
count(sender, receiver) %>%
arrange(desc(n))
## # A tibble: 210 x 3
## sender receiver n
## <chr> <chr> <int>
## 1 ForestGlenGator JUMP_Math 12
## 2 caob2018 JUMP_Math 8
## 3 DrMNikfar HobbsTimes4 8
## 4 TaothaSchool JUMP_Math 8
## 5 TTPowerUp1 JUMP_Math 8
## 6 anujgsj teacheranand 4
## 7 bmeyring teachersam_dmti 4
## 8 ChiefBearspaw JUMP_Math 4
## 9 DrMNikfar Krit_Popke 4
## 10 indranil2kin teacheranand 4
## # … with 200 more rows
codes_ss <- select(codes, screen_name, code, ntweets, parent = `also-parent`)
# edgelist <- tidytags::add_users_data(edgelist, codes_ss)
users <- n_tweets %>%
left_join(codes_ss) %>%
mutate(code = if_else(is.na(code), "uncoded", code))
users
## # A tibble: 187 x 5
## screen_name n code ntweets parent
## <chr> <int> <chr> <dbl> <dbl>
## 1 _LarissaChan 1 uncoded NA NA
## 2 070180 1 uncoded NA NA
## 3 0rtizclassroom 1 uncoded NA NA
## 4 24game 1 uncoded NA NA
## 5 4ahealthyhabit 1 uncoded NA NA
## 6 AbecedaireApps 1 uncoded NA NA
## 7 abmath7 5 professor 5 NA
## 8 adrianmendozaed 1 uncoded NA NA
## 9 akhlaquequmar35 1 uncoded NA NA
## 10 anshtripathi029 1 uncoded NA NA
## # … with 177 more rows
# edgelist %>%
# left_join(rename(n_tweets, sender = screen_name, ntweets_sender_1 = n)) %>%
# left_join(select(n_tweets, receiver = screen_name, ntweets_receiver_1 = n)) %>%
# mutate(ntweets_sender = if_else(is.na(ntweets_sender), ntweets_sender_1, as.integer(ntweets_sender))) %>%
# mutate(ntweets_receiver = if_else(is.na(ntweets_receiver), ntweets_sender_1, as.integer(ntweets_receiver)))
edgelist
## # A tibble: 355 x 3
## sender receiver edge_type
## <chr> <chr> <chr>
## 1 JJDLagrange YehCathery reply
## 2 MTL_CS_BozziK MTL_CS_BozziK reply
## 3 FirstInMath KAndrew000 reply
## 4 pelletier_t3 pelletier_t3 reply
## 5 aprilschoenberg missmillerin5th reply
## 6 UTEddieBrown UTEddieBrown reply
## 7 iteachmathAll Mathcutups retweet
## 8 iteachmathAll HobbsTimes4 retweet
## 9 iteachmathAll Krit_Popke retweet
## 10 iteachmathAll Mathcutups retweet
## # … with 345 more rows
edgelist_ss <- edgelist %>%
filter(sender %in% users$screen_name & receiver %in% users$screen_name)
users <- mutate(users, parent = as.factor(ifelse(is.na(parent), "No", "Yes")))
users$screen_name_addorned <- ifelse(users$parent == 1, str_c(users$screen_name, "*"), users$screen_name)
graph <- igraph::graph_from_data_frame(edgelist_ss, vertices = users) %>%
as_tbl_graph() %>%
mutate(Popularity = centrality_degree(mode = 'in'))
ggraph(graph, layout = 'kk') +
geom_edge_fan(aes(alpha = stat(index)), show.legend = FALSE) +
geom_node_point(aes(size = Popularity, color = code, shape = parent)) +
geom_node_text(aes(label = ifelse(Popularity > 3.5, name, NA))) +
theme_graph() +
scale_color_brewer("Role", type = "qual", palette = 3) +
labs(caption = "Only those who posted one or more original tweets are included") +
guides(size = FALSE)
Using the Google Maps API based on the self-reported Twitter location
# d <- d %>%
# mutate(location = ifelse(str_detect(location, "#"), NA, location))
#
# geocoded_locs <- d %>%
# tidytags::geocode_tags()
#
# write_rds(geocoded_locs, "geocoded-locs.rds")
geocoded_locs <- read_rds("geocoded-locs.rds")
users_all <- rtweet::users_data(d) %>%
left_join(users)
# geocoded_locs$code <- users_all$code
# geocoded_locs$parent <- users_all$parent
# example_unique_places <- dplyr::distinct(geocoded_locs, location, .keep_all = TRUE)
# example_geo_coords <- geocode_tags(example_unique_places)
pal <- colorFactor(RColorBrewer::brewer.pal(9, "Set1"), unique(users_all$code))
leaflet() %>%
addProviderTiles("OpenStreetMap") %>%
addCircleMarkers(data = geocoded_locs,
radius = users_all$n,
label = str_c(d$screen_name, "; Parent: ", users_all$parent, "; N tweets: ", users_all$n),
color = ~pal(users_all$code)) %>%
addLegend("bottomright", pal = pal, values = users_all$code,
title = "Role")
library(tidyverse)
older_data <- read_lines("older-mathathome-tweets.txt")
older_data_words <- older_data %>%
str_split(" ") %>%
unlist()
is_a_link <- older_data_words %>%
str_detect("t.co")
links <- older_data_words[is_a_link]
expanded_links <- longurl::expand_urls(links)
expanded_links %>% count(orig_url) %>%
arrange(desc(n))
expanded_links <- expanded_links %>%
filter(str_detect(expanded_url, "twitter.com"))
statuses <- str_split(expanded_links$expanded_url, "/")
statuses <- statuses %>% map_chr(~.[6])
gss <- pull_tweet_data(statuses)
write_csv(flatten(gss), "googlesheets-tweets.csv")