graphTweets not only allows to build graphs of interactions between users. It also lets you visualise what hashtag users use in their tweets and which hashtags are used together in the same tweets.
The same principles follow, we simply use
gt_edges_hash and pass the
hashtags column as returned by rtweet. This creates a
tibble of edges from
hashtags used in each tweet.
We’ll visualise the graph with sigmajs. Let’s prepare the data to meet the library’s requirements.
idto both nodes and edges
type(hashtag or user)
Apologies for not getting into details here but sigmajs is very well documented, check the website if you want to understand it all.
Let’s visualise it.
sg_layout to layout the graph and
sg_neightbours to highlight nodes on click.
We can also build networks of hashtags co-mentions; when two or more hashtags are used in the same tweet they are connected on the graph.
Let’s look at the hashtags associated with #rstats
For this network use
gt_edges_hashes and simply pass the
hashtags column as it’s all that is needed.
Then again, we’ll use
sigmajs for the visualisation. Since we got tweets on
#rstats we will remove this node, it will be connected to all other hashtags. Make sure you remove it from both nodes and edges
library(dplyr) #> #> Attaching package: 'dplyr' #> The following objects are masked from 'package:stats': #> #> filter, lag #> The following objects are masked from 'package:base': #> #> intersect, setdiff, setequal, union edges <- edges %>% mutate( id = 1:n() ) %>% filter(source != "rstats") %>% filter(target != "rstats") nodes <- nodes %>% mutate( id = nodes, label = paste0("#", id), size = n_edges ) %>% filter(id != "rstats") %>% select(id, label, size) sigmajs() %>% sg_nodes(nodes, id, label, size) %>% sg_edges(edges, id, source, target) %>% sg_cluster( colors = c( "#0084b4", "#00aced", "#1dcaff", "#c0deed" ) ) %>% sg_layout() %>% sg_neighbours() %>% sg_settings( defaultEdgeColor = "#a3a3a3", edgeColor = "default" )