Load packages and set knitr options for NVD3 Visualizations

stopifnot(require(pacman))
pacman::p_load(dplyr, sp, maps, rgdal, rgeos, GISTools, maptools, ggplot2, tmap, tm, ggmap, streamR, RColorBrewer, classInt, wordcloud, rCharts, knitr)
#options(rcharts.mode = 'iframe', rcharts.cdn = TRUE)
#knitr::opts_chunk$set(results = "asis", comment = NA)
setwd("~/Google Drive/QMSS/QMSS-Spring2016/Data Visualization/Assignments/A4")

Parse Twitter data From 16 Mar 2016 to 25 Mar 2016

#parse twitter data from 3.16.2016-3.25.2016
twts_316 <- parseTweets("tweets.03.16.2016.summary.json")
twts_317 <- parseTweets("tweets.03.17.2016.summary.json")
twts_318 <- parseTweets("tweets.03.18.2016.summary.json")
twts_319 <- parseTweets("tweets.03.19.2016.summary.json")
twts_320 <- parseTweets("tweets.03.20.2016.summary.json")
twts_321 <- parseTweets("tweets.03.21.2016.summary.json")
twts_322 <- parseTweets("tweets.03.22.2016.summary.json")
twts_323 <- parseTweets("tweets.03.23.2016.summary.json")
twts_324 <- parseTweets("tweets.03.24.2016.summary.json")
twts_325 <- parseTweets("tweets.03.25.2016.summary.json")

save(twts_316, file = "twts_316.RData")
save(twts_317, file = "twts_317.RData")
save(twts_318, file = "twts_318.RData")
save(twts_319, file = "twts_319.RData")
save(twts_320, file = "twts_320.RData")
save(twts_321, file = "twts_321.RData")
save(twts_322, file = "twts_322.RData")
save(twts_323, file = "twts_323.RData")
save(twts_324, file = "twts_324.RData")
save(twts_325, file = "twts_325.RData")

Subset tweets by candidate name mention and topic issue indicator words

Repeat this code for each day’s tweets by applying the function I wrote, keyword_count, to each tweet corpus subset by candidate.

Collapse the tweet dataframes to get total counts of topic issues by candidate name mention

Apply the second function I created, combine_df, to achieve this.

#create combined dataframes by party
combine_df <- function(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10) {
  for (i in 1:10) {
    totals <- data.frame(Topic = "topic", Totals = 0, stringsAsFactors = FALSE)
    topics <- df1$Topic
    for (topic in topics) { #loop through topics
      for (row in 1:10) { # loop through rows
        total <- 0 #store total by topic
        for (dataframe in list(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10)) {
        total <- sum(total, dataframe[row, 2]) #running sum of totals
      }
      totals[row,] <- total
    }
    totals$Topic <- topics
  }
}
return(totals)
}

cl_all <- combine_df(cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, cl10)
cl_all$Candidate <- rep("Clinton")
sd_all <- combine_df(sd1, sd2, sd3, sd4, sd5, sd6, sd7, sd8, sd9, sd10)
sd_all$Candidate <- rep("Sanders")
dems <- rbind(cl_all, sd_all) #combine across days
save(dems, file = "dems.RData")
#repeat for republican candidates
cz_all <- combine_df(cz1, cz2, cz3, cz4, cz5, cz6, cz7, cz8, cz9, cz10)
cz_all$Candidate <- rep("Cruz")
tr_all <- combine_df(tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tr10)
tr_all$Candidate <- rep("Trump")
reps <- rbind(cz_all, tr_all) 
save(reps, file = "reps.RData")

Generate topic frequency plots between candidates by political party

I used the ggplot2 package to create each plot.

load("dems.RData")
load("reps.RData")
#generate general plot showing all topic frequencies for dems
alltopic_dem_plot <- ggplot(dems, aes(Candidate, Totals)) + 
  geom_bar(stat = "identity", aes(fill = Topic), position = "dodge") +
  ggtitle("Tweet Mention Frequency Comparison Comparing Democratic Candidates")


#generate topic frequency plot by topic for dems
bytopic_rep_plot <- ggplot(dems, aes(Candidate, Totals)) + 
  geom_bar(stat = "identity", aes(fill = Topic), position = "dodge") +
  scale_colour_hue("Topic") +
  facet_wrap(~Topic) +
  ggtitle("Tweet Mention Frequency Comparison by Topic Comparing Democratic Candidates")


#same as above but for reps
alltopic_rep_plot <- ggplot(reps, aes(Candidate, Totals)) + 
  geom_bar(stat = "identity", aes(fill = Topic), position = "dodge") +
  scale_fill_brewer(palette = "Spectral") +
  ggtitle("Tweet Mention Frequency Comparison Comparing Republican Candidates")


bytopic_rep_plot <- ggplot(reps, aes(Candidate, Totals)) + 
  geom_bar(stat = "identity", aes(fill = Topic), position = "dodge") +
  scale_fill_brewer(palette = "Spectral") +
  facet_wrap(~Topic) +
  ggtitle("Tweet Mention Frequency Comparison by Topic Comparing Republican Candidates")

Contrasting Tweet Frequencies Within and Between Parties

Add dates to the tweet dataframes by candidate to get daily topic issue mention frequencies

I created a third function, add_10dates, to add a date column to each dataframe.

#counts over time
add_10dates <- function(df, date){
  Date <- rep(date, 10)
  df <- cbind(df, Date)
  df$Date <- as.Date(df$Date, "%d %b %Y")
  return(df)
}

cl1 <- add_10dates(cl1, "16 mar 2016")
cl2 <- add_10dates(cl2, "17 mar 2016")
cl3 <- add_10dates(cl3, "18 mar 2016")
cl4 <- add_10dates(cl4, "19 mar 2016")
cl5 <- add_10dates(cl5, "20 mar 2016")
cl6 <- add_10dates(cl6, "21 mar 2016")
cl7 <- add_10dates(cl7, "22 mar 2016")
cl8 <- add_10dates(cl8, "23 mar 2016")
cl9 <- add_10dates(cl9, "24 mar 2016")
cl10 <- add_10dates(cl10, "25 mar 2016")

sd1 <- add_10dates(sd1, "16 mar 2016")
sd2 <- add_10dates(sd2, "17 mar 2016")
sd3 <- add_10dates(sd3, "18 mar 2016")
sd4 <- add_10dates(sd4, "19 mar 2016")
sd5 <- add_10dates(sd5, "20 mar 2016")
sd6 <- add_10dates(sd6, "21 mar 2016")
sd7 <- add_10dates(sd7, "22 mar 2016")
sd8 <- add_10dates(sd8, "23 mar 2016")
sd9 <- add_10dates(sd9, "24 mar 2016")
sd10 <- add_10dates(sd10, "25 mar 2016")

cz1 <- add_10dates(cz1, "16 mar 2016")
cz2 <- add_10dates(cz2, "17 mar 2016")
cz3 <- add_10dates(cz3, "18 mar 2016")
cz4 <- add_10dates(cz4, "19 mar 2016")
cz5 <- add_10dates(cz5, "20 mar 2016")
cz6 <- add_10dates(cz6, "21 mar 2016")
cz7 <- add_10dates(cz7, "22 mar 2016")
cz8 <- add_10dates(cz8, "23 mar 2016")
cz9 <- add_10dates(cz9, "24 mar 2016")
cz10 <- add_10dates(cz10, "25 mar 2016")

tr1 <- add_10dates(tr1, "16 mar 2016")
tr2 <- add_10dates(tr2, "17 mar 2016")
tr3 <- add_10dates(tr3, "18 mar 2016")
tr4 <- add_10dates(tr4, "19 mar 2016")
tr5 <- add_10dates(tr5, "20 mar 2016")
tr6 <- add_10dates(tr6, "21 mar 2016")
tr7 <- add_10dates(tr7, "22 mar 2016")
tr8 <- add_10dates(tr8, "23 mar 2016")
tr9 <- add_10dates(tr9, "24 mar 2016")
tr10 <- add_10dates(tr10, "25 mar 2016")

#combine by party
dem_time <- rbind(cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, cl10, 
                 sd1, sd2, sd3, sd4, sd5, sd6, sd7, sd8, sd9, sd10)
rep_time <- rbind(cz1, cz2, cz3, cz4, cz5, cz6, cz7, cz8, cz9, cz10, 
                  tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tr10)

Generate plots of daily frequencies

I used ggplot again for these plots.

dem_time_plot <- ggplot(dem_time, aes(x = Date, y = Count, color = Topic)) + 
  geom_line(aes(linetype = Candidate), size = 1) +
  ggtitle("Daily Changes in Tweet Frequency by Topic Comparing Democratic Candidates") +
  theme(panel.background = element_rect(fill = "black"),
        panel.grid = element_blank()) +
  guides(col = guide_legend(nrow = 5),
         size = 1) +
  theme(legend.position = c(.8, .8)) +
  ylim(0, max(dem_time$Count))


rep_time_plot <- ggplot(rep_time, aes(x = Date, y = Count, color = Topic)) + 
  geom_line(aes(linetype = Candidate), size = 1) +
  ggtitle("Daily Changes in Tweet Frequency by Topic Comparing Republican Candidates") +
  theme(panel.background = element_rect(fill = "black"),
        panel.grid = element_blank()) +
  guides(col = guide_legend(nrow = 5),
         size = 1) +
  theme(legend.position = c(.2, .8)) +
  ylim(0, max(rep_time$Count))

Daily tweet frequency changes between 16 Mar 2016 and 25 Mar 2016

Generate interactive plots to see daily tweet frequencies by candidate and topic issue

I used rCharts to create NVD3 visualizations for these graphs.

require(devtools)
install_github('rCharts', 'ramnathv')
## Warning: Username parameter is deprecated. Please use ramnathv/rCharts
library(rCharts)

cl_iv <- rbind(cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, cl10)
sd_iv <- rbind(sd1, sd2, sd3, sd4, sd5, sd6, sd7, sd8, sd9, sd10)
cz_iv <- rbind(cz1, cz2, cz3, cz4, cz5, cz6, cz7, cz8, cz9, cz10)
tr_iv <- rbind(tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tr10)

cl_plot <- nPlot(Count ~ Date, group = "Topic", data = cl_iv, type = 'lineWithFocusChart')
cl_plot$xAxis(axisLabel = 'Date')
cl_plot$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
cl_plot$set(title = "Daily Tweet Frequency by Topic for 'Clinton'")
cl_plot$xAxis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")
cl_plot$x2Axis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")

sd_plot <- nPlot(Count ~ Date, group = "Topic", data = sd_iv, type = 'lineWithFocusChart')
sd_plot$xAxis(axisLabel = 'Date')
sd_plot$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
sd_plot$set(title = "Daily Tweet Frequency by Topic for 'Sanders'")
sd_plot$xAxis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")
sd_plot$x2Axis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")

cz_plot <- nPlot(Count ~ Date, group = "Topic", data = cz_iv, type = 'lineWithFocusChart')
cz_plot$xAxis(axisLabel = 'Date')
cz_plot$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
cz_plot$set(title = "Daily Tweet Frequency by Topic for 'Cruz'")
cz_plot$xAxis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")
cz_plot$x2Axis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")

tr_plot <- nPlot(Count ~ Date, group = "Topic", data = tr_iv, type = 'lineWithFocusChart')
tr_plot$xAxis(axisLabel = 'Date')
tr_plot$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
tr_plot$set(title = "Daily Tweet Frequency by Topic for 'Trump'")
tr_plot$xAxis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")
tr_plot$x2Axis(tickFormat = "#!
      function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}
    !#")

You can interact with these plots below

We can change the date range with the bottom tool and add or remove topic issues from the graph. We can also hover over points in the graph to see the specific counts.

cl_plot$show('iframesrc', cdn = TRUE)
sd_plot$show('iframesrc', cdn = TRUE)
cz_plot$show('iframesrc', cdn = TRUE)
tr_plot$show('iframesrc', cdn = TRUE)