Code for project-specific methods can be found at my OSF page. Below are some code snippets I’ve used that can be adapted for more general tasks that may be of interest for students or researchers. More soon!
Connect to Twitter’s API using Mike Kearney’s ‘rtweet’ package.
Collect tweets via keywords, from a specific geographic location, or from a specific user’s timeline. Also do some basic plotting to explore data like geo-maps and a word cloud.
Important: this script assumes you have a Twitter developer account, which you can easily set up following this tutorial.
Sentiment analysis with lexicon-based methods
Use Julia Silge’s tidytext’ package to tokenize tweets and then determine positive or negative sentiment based on existing lexicons.
Plot most frequently occurring negative or positive words comparing two Twitter users.
Create a custom dot plot
This is one of my favorite plot types for presenting means and distributions when comparing group means. It has all the advantages of ‘violin plots’ but looks a little cleaner in my opinion. This script includes ggplot functions to customize most features in the plot.
Train a random forest model