knitr::opts_chunk$set( # eval=TRUE, eval=FALSE, # echo=TRUE, collapse = TRUE, # cache = TRUE, comment = "#>" )
This vignette shows how {rtweettree} can be used to generate network graphs visualizing the reactions on a tweet (replies, quotes and favorites) as a tree. The nodes correspond to tweets and the users who interact with them with these reactions.
The following libraries are loaded:
library(dplyr) library(rtweettree) library(ggraph) library(tidygraph) library(ggiraph)
rtweettree tries to scrape as much as information as possible that might be related to the tweet at the root of the tree. Please beware that the twitter API doesn't allow to scrape all sub tweets. And that for large amounts of interactions to a tweet it can take a long time due to rate limits (please refer to the twitter developer website on rate limits, or the documentation of the rtweet functions).
Tweets on twitter are uniquely classified via the status id:
# Replace this number by any status_id (the last number in the twitter.com url of a tweet): main_status_id <- "1438481824922181635"
Now let's scrape:
rtweettree_data_scraped <- rtweettree_data(main_status_id)
rtweettree_data_example <- rtweettree_data_scraped usethis::use_data(rtweettree_data_example, overwrite = TRUE)
To allow to test the package in offline use (and without setting up an account for the twitter api), you can also load this dataset which is already included in the package:
rtweettree_data_scraped <- rtweettree_data_example
The relevant twitter information of these tweets is translated into a tidygraph network object:
g <- rtweettree_tbl_graph(rtweettree_data_scraped)
g
Now we can make use of the full power of tidygraph, e.g., add a column to the nodes tibble, showing how far the respective node is from the main tweet in the graph:
g <- g %>% # calculate the distance to the tree root with tidygraph: mutate(dist_to_root = node_distance_from(node_is_source())) g %>% as_tibble()
Please make sure not to publish information from Twitter you are not allowed to
and to comply in strict accordance with the twitter developer
terms!
The example rtweet data in this article only contains the tweets of three dummy
accounts I created. But probably you're not allowed to publish all this
information for the main_status_id
of any other tweet.
The generated graph object consists of nodes representing tweets and users. These are connected by edges that can be
We can generate a simple tree graph of the various tweets and users with:
g %>% ggraph() + geom_node_point(aes(color = dist_to_root), size = 3) + geom_edge_diagonal(aes(color = type))
This yields something similar to the autoplot() method of the package:
# (In order to include the profile pictures in the graph you need an internet # connection) ggplot2::autoplot(g, add_profile_pics = TRUE)
With ggiraph we can generate an interactive tree graph. Let's first add information to the nodes dataframe:
g <- g %>% # Add on click information to the graph nodes: # (twitter will correct the "fake_screen_name" if the tweet is still online): mutate(url = case_when( type == "user" ~ glue::glue("https://twitter.com/{screen_name}/"), type == "tweet" ~ glue::glue("https://twitter.com/fake_screen_name/status/{text}") )) %>% mutate(onclick = glue::glue('window.open("{url}")')) %>% # add tooltip information to the nodes: mutate(tooltip = case_when( type == "user" ~ screen_name, type == "tweet" ~ paste0(screen_name, ":\n", stringr::str_wrap(text, 30)) ))
Now we can use this information with geom_point_interactive()
:
g2 <- ggraph(g) + geom_edge_diagonal(aes(color= type)) + geom_point_interactive( aes( x = x, y = y, color = type, data_id = screen_name, # this highlights all tweets of one user tooltip = tooltip, # add the tooltip onclick = onclick # opens the url created above when clicking on the nodes ), size = 3 ) + ggtitle( "Hover over the nodes to see\nthe tweets/user names.", subtitle = "Click on the nodes to open\nthe tweets/users on twitter.com" ) p <- girafe(code = print(g2), width_svg = 4, height_svg = 4) p <- girafe_options( x = p, opts_zoom(min = 0.3, max = 5), opts_sizing(width = 0.7), opts_hover( girafe_css( css = "stroke:yellow;", point = "stroke-width:6px") ) ) p
autoplot()
We can also customize the ggraph object by passing arguments to the autoplot methods, e.g. by
add_profile_pics = FALSE
)ggplot2::autoplot(rtweettree_data_scraped, add_profile_pics = FALSE, layout = "stress")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.