knitr::opts_chunk$set( collapse = FALSE, comment = "##" )
Using quanteda's fcm()
and textplot_network()
, you can perform visual analysis of social media posts in terms of co-occurrences of hashtags or usernames in a few steps. The dataset for this example contains only 10,000 Twitter posts, but you can easily analyse more than one million posts on your laptop computer.
library(quanteda)
load("data/data_corpus_tweets.rda")
dfmat_tweets <- tokens(data_corpus_tweets, remove_punct = TRUE) |> dfm() head(dfmat_tweets)
dfmat_tag <- dfm_select(dfmat_tweets, pattern = "#*") toptag <- names(topfeatures(dfmat_tag, 50)) head(toptag)
library("quanteda.textplots") fcmat_tag <- fcm(dfmat_tag) head(fcmat_tag) fcmat_topgat <- fcm_select(fcmat_tag, pattern = toptag) textplot_network(fcmat_topgat, min_freq = 0.1, edge_alpha = 0.8, edge_size = 5)
dfmtat_users <- dfm_select(dfmat_tweets, pattern = "@*") topuser <- names(topfeatures(dfmtat_users, 50)) head(topuser)
fcmat_users <- fcm(dfmtat_users) head(fcmat_users) fcmat_users <- fcm_select(fcmat_users, pattern = topuser) textplot_network(fcmat_users, min_freq = 0.1, edge_color = "orange", edge_alpha = 0.8, edge_size = 5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.