library("dslabs") # installs packages("dslabs") which we will use for the homework
## Warning: package 'dslabs' was built under R version 4.0.4
data(package="dslabs")
list.files(system.file("script", package = "dslabs"))
##  [1] "make-admissions.R"                   
##  [2] "make-brca.R"                         
##  [3] "make-brexit_polls.R"                 
##  [4] "make-death_prob.R"                   
##  [5] "make-divorce_margarine.R"            
##  [6] "make-gapminder-rdas.R"               
##  [7] "make-greenhouse_gases.R"             
##  [8] "make-historic_co2.R"                 
##  [9] "make-mnist_27.R"                     
## [10] "make-movielens.R"                    
## [11] "make-murders-rda.R"                  
## [12] "make-na_example-rda.R"               
## [13] "make-nyc_regents_scores.R"           
## [14] "make-olive.R"                        
## [15] "make-outlier_example.R"              
## [16] "make-polls_2008.R"                   
## [17] "make-polls_us_election_2016.R"       
## [18] "make-reported_heights-rda.R"         
## [19] "make-research_funding_rates.R"       
## [20] "make-stars.R"                        
## [21] "make-temp_carbon.R"                  
## [22] "make-tissue-gene-expression.R"       
## [23] "make-trump_tweets.R"                 
## [24] "make-weekly_us_contagious_diseases.R"
## [25] "save-gapminder-example-csv.R"
#install packages for Highcharter
library(readr) 
library(ggplot2)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
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
library(highcharter)
## Warning: package 'highcharter' was built under R version 4.0.4
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
## 
## Attaching package: 'highcharter'
## The following object is masked from 'package:dslabs':
## 
##     stars
library(RColorBrewer)
trump_tweets <- read_csv("Trump_tweets.csv") # uploads data
## 
## -- Column specification --------------------------------------------------------
## cols(
##   source = col_character(),
##   id_str = col_double(),
##   text = col_character(),
##   created_at = col_datetime(format = ""),
##   retweet_count = col_double(),
##   in_reply_to_user_id_str = col_double(),
##   favorite_count = col_double(),
##   is_retweet = col_logical()
## )
trump_tweet1 <- trump_tweets %>%
mutate(created_at=as.Date(created_at, format ="%Y/%m/%d")) #changes date format to only include year, month and day (time is dropped).
trump_tweet2 <- trump_tweet1 %>%
  count(created_at,sort=TRUE) 
# this  gives the count of number tweets per day
trump_retweets <- aggregate(x = trump_tweet1[c("retweet_count")],
                     FUN = sum,
                     by = list(Group.date = trump_tweet1$created_at)) #this aggregates all retweets per day
trumpmaster <- full_join(trump_retweets, trump_tweet2, by=c("Group.date"= "created_at"))#joins both tables created
cols <- brewer.pal(4, "Pastel1") #sets the color palette
highchart() %>%
  hc_yAxis_multiples(
    list(title = list(text = "Number of Tweets")),
    list(title = list(text = "Number of Retweets"),
         opposite = TRUE) 
  ) %>% # This adds two y axis to the same x axis
  
  hc_add_series(data = trumpmaster$n,
                name = "Tweets",
                type = "column",
                yAxis = 0) %>% #adds y axis
  hc_add_series(data = trumpmaster$retweet_count,
                name = "Retweets",
                type = "line",
                yAxis = 1) %>% #adds y  axis
  hc_xAxis(categories = trumpmaster$Group.date,
           tickInterval = 10) %>%
  hc_colors(cols) %>% #this is puts the palette that we selected previously
  hc_chart(style = list(fontFamily = "Georgia",
                        fontWeight = "bold"))%>% #formats font type 
 hc_legend(align = "right", 
            verticalAlign = "top") %>% #formats legend
    hc_title(text = "Trump Tweets and Retweets from 2009-2018") %>% #adds title to graph
hc_xAxis( title = list(text="Date")) %>% # adds title to x axis
  hc_tooltip(crosshairs = TRUE, borderWidth = 1, borderColor = "black", shared = TRUE,
    sort = TRUE,
    table = TRUE) #formats tooltip over each date

This graph shows how the number of retweets from Trump increased starting in 2015; which is the year he entered the presidential race. Also Trump tweeted and was retweeted the most on October 20, 2016 which was last debate against Hillary Clinton.