I'm currently trying to animate a plot using gganimate but am struggling to figure out how I would rotate through multiple y variables. The following data was collected from twitter scraping which allowed me to calculate a "sentiment score" based on the tweets following the recent Democratic debate. The goal here is to create an animated plot that eases through all 10 sentiment scores and adjusts the ggplot for each candidate. Is this possible with gganimate?
structure(
list(
candidate = c("warren", "booker", "yang", "harris", "biden", "sanders", "buttigieg"),
anger = c(162, 216, 193, 74, 451, 290, 114),
anticipation = c(570, 492, 401, 205, 360, 419, 499),
disgust = c(94, 75, 52, 61, 202, 81, 69),
fear = c(245, 241, 119, 117, 271, 251, 102),
joy = c(574, 525, 279, 181, 214, 319, 183),
sadness = c(237, 161, 138, 106, 406, 157, 251),
surprise = c(104, 191, 176, 106, 255, 343, 123),
trust = c(741, 749, 460, 325, 593, 574, 410),
negative = c(540, 317, 253, 205, 715, 360, 469),
positive = c(989, 1202, 857, 510, 751, 790, 701)
),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L),
spec = structure(
list(
cols = list(
candidate = structure(list(), class = c("collector_character", "collector")),
anger = structure(list(), class = c("collector_double", "collector")),
anticipation = structure(list(), class = c("collector_double", "collector")),
disgust = structure(list(), class = c("collector_double", "collector")),
fear = structure(list(), class = c("collector_double", "collector")),
joy = structure(list(), class = c("collector_double", "collector")),
sadness = structure(list(), class = c("collector_double", "collector")),
surprise = structure(list(), class = c("collector_double", "collector")),
trust = structure(list(), class = c("collector_double", "collector")),
negative = structure(list(), class = c("collector_double", "collector")),
positive = structure(list(), class = c("collector_double", "collector"))),
default = structure(list(), class = c("collector_guess", "collector")), skip = 1
),
class = "col_spec")
)
Here is the script I currently have written:
library ("ggplot2")
library("dplyr")
library("tidyverse")
library("plotly")
library("viridis")
library("gganimate")
#Read in CSV Files
sentiment_score <- read_csv('C:\\Users\\tdago\\Documents\\R\\Sentiment_Scores.csv')
sentiment_score_hashtag <- read_csv('C:\\Users\\tdago\\Documents\\R\\Sentiment_Scores_hashtag.csv')
#Tidy Data
sentiment_score <- sentiment_score %>%
rename(candidate = X1)
sentiment_score_hashtag <-sentiment_score_hashtag %>%
rename(candidate = X1)
#Create Charts for Comparison
ggplot(data=sentiment_score,aes(x = candidate, y=anger))+
geom_bar(aes(fill=candidate),stat = "identity")+
theme(legend.position="none")+
xlab("Presidential Candidates")+ylab("Scores")+ggtitle("Anger") +
labs(x = "", y = "{sentiment"}) +
ease_aes('linear')
Note: the sentiment_score object is the only one that is being used in this specific chart. sentiment_score_hashtag is a similar data frame that contains sentiment scores based on a different search.