Data 110 week 8

Author

Ask Moystad

Data 110: Week 8 assignment

In this assignment I have chosen the trump_tweets dataset. I really wanted to see how trumps tweeting habits changed leading up to and during the election, however I eventually settled on visualising not only is tweets over the span of a year, and the 24 hour cycle, but also with respect to the number of retweets he recieved. The result of which was a graph that tells us more about his impact during the election. The time of day appears to matter less, and with more time I may have chosen a different way to incorporate that variable. What we might see looking at this graph is Trumps experimental phase, his gradual rise on twitter and the effect of the election as well.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)
library(lubridate)
library(extrafont)
Registering fonts with R
library(ggthemes)
library(ggrepel)
library(dslabs)
head(trump_tweets)
              source     id_str
1 Twitter Web Client 6971079756
2 Twitter Web Client 6312794445
3 Twitter Web Client 6090839867
4 Twitter Web Client 5775731054
5 Twitter Web Client 5364614040
6 Twitter Web Client 5203117820
                                                                                                                                        text
1       From Donald Trump: Wishing everyone a wonderful holiday & a happy, healthy, prosperous New Year. Let’s think like champions in 2010!
2 Trump International Tower in Chicago ranked 6th tallest building in world by Council on Tall Buildings & Urban Habitat http://bit.ly/sqvQq
3                                                                             Wishing you and yours a very Happy and Bountiful Thanksgiving!
4                       Donald Trump Partners with TV1 on New Reality Series Entitled, Omarosa's Ultimate Merger: http://tinyurl.com/yk5m3lc
5                         --Work has begun, ahead of schedule, to build the greatest golf course in history: Trump International – Scotland.
6              --From Donald Trump: "Ivanka and Jared’s wedding was spectacular, and they make a beautiful couple. I’m a very proud father."
           created_at retweet_count in_reply_to_user_id_str favorite_count
1 2009-12-23 12:38:18            28                    <NA>             12
2 2009-12-03 14:39:09            33                    <NA>              6
3 2009-11-26 14:55:38            13                    <NA>             11
4 2009-11-16 16:06:10             5                    <NA>              3
5 2009-11-02 09:57:56             7                    <NA>              6
6 2009-10-27 10:31:48             4                    <NA>              5
  is_retweet
1      FALSE
2      FALSE
3      FALSE
4      FALSE
5      FALSE
6      FALSE
trump <- trump_tweets 
# I want to seperate the date and the time of day from each other.
trump$date <- as.Date(trump$created_at)  
trump$time <- format(as.POSIXct(trump$created_at), "%H:%M")  
head(trump)
              source     id_str
1 Twitter Web Client 6971079756
2 Twitter Web Client 6312794445
3 Twitter Web Client 6090839867
4 Twitter Web Client 5775731054
5 Twitter Web Client 5364614040
6 Twitter Web Client 5203117820
                                                                                                                                        text
1       From Donald Trump: Wishing everyone a wonderful holiday & a happy, healthy, prosperous New Year. Let’s think like champions in 2010!
2 Trump International Tower in Chicago ranked 6th tallest building in world by Council on Tall Buildings & Urban Habitat http://bit.ly/sqvQq
3                                                                             Wishing you and yours a very Happy and Bountiful Thanksgiving!
4                       Donald Trump Partners with TV1 on New Reality Series Entitled, Omarosa's Ultimate Merger: http://tinyurl.com/yk5m3lc
5                         --Work has begun, ahead of schedule, to build the greatest golf course in history: Trump International – Scotland.
6              --From Donald Trump: "Ivanka and Jared’s wedding was spectacular, and they make a beautiful couple. I’m a very proud father."
           created_at retweet_count in_reply_to_user_id_str favorite_count
1 2009-12-23 12:38:18            28                    <NA>             12
2 2009-12-03 14:39:09            33                    <NA>              6
3 2009-11-26 14:55:38            13                    <NA>             11
4 2009-11-16 16:06:10             5                    <NA>              3
5 2009-11-02 09:57:56             7                    <NA>              6
6 2009-10-27 10:31:48             4                    <NA>              5
  is_retweet       date  time
1      FALSE 2009-12-23 12:38
2      FALSE 2009-12-03 14:39
3      FALSE 2009-11-26 14:55
4      FALSE 2009-11-16 16:06
5      FALSE 2009-11-02 09:57
6      FALSE 2009-10-27 10:31
# I am here trying to make the time variable into a numeric variable, this got clunky fast
trump$time_numeric <- with(trump, as.numeric(substr(time, 1, 2)) + 
                               as.numeric(substr(time, 5, 7))/60 + 
                               as.numeric(substr(time, 10, 11))/3600)
trump$time_num <- with(trump, as.numeric(substr(time, 1, 2)) + 
                               as.numeric(substr(time, 4, 5))/60)
trump |> 
  mutate(retweet_count_thousands = retweet_count / 1000) |> 
  ggplot(aes(x = date, y = time_num, color = retweet_count_thousands)) + 
  geom_point(alpha = 0.7) +
  scale_color_distiller(palette = "Spectral", trans = "sqrt") + 
  xlab("Date") +
  ylab("Time of Day") +
  geom_vline(xintercept = as.Date("2016-11-06"), col = "orange") +
  theme_minimal() +
   labs(title = "Trump's Twitter Transformation", 
        subtitle = "The orange line denotes the presidential election", 
        color = "# of Retweets\n(in thousands)")