DS Labs Assignment

# Loading the dslabs library and trump_tweets dataset. Loading tidyverse.
library("dslabs")
Warning: package 'dslabs' was built under R version 4.6.1
data(package="dslabs")
data("trump_tweets")
library(tidyverse)
Warning: package 'stringr' was built under R version 4.6.1
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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
# Investigating the available columns.  
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
# Installing and loading the stringr package to count words. Instructions from: https://r-statistics.co/stringr-str_count-in-R.html
install.packages("stringr")
Warning: package 'stringr' is in use and will not be installed
library(stringr)

# Creating a new column with the word count for each tweet. Instructions form: https://r-statistics.co/stringr-str_count-in-R.html.
trump_tweets <- trump_tweets %>%
  mutate(word_count = str_count(text, boundary("word")))

# Creating a new column denotating what political era the tweet took place in. Instructions for as.Date found here: https://rpubs.com/odenipinedo/working-with-dates-and-times-in-R.
trump_tweets <- trump_tweets %>%
  mutate(political_era = case_when(
    created_at < as.Date("2015-06-15") ~ "Pre Campaign",
    created_at < as.Date("2016-11-09") ~ "Campaign",
    TRUE ~ "Post Election")
  )
# Using ggplot and geom point to plot tweet length on the x axis, favorite count on the y axis, and color by political era. Setting alpha = 0.3 to make the points easier to see since there are so many.
tweet_scatter <- trump_tweets %>%
  ggplot(aes(x=word_count, y=favorite_count)) +
  geom_point(aes(color=political_era), alpha=0.3) +
# Setting a log scale for y since there is such a wide range and the numbers go so high. 
  scale_y_log10("Total Favorites (log scale)") +
# Adding labels and a title. 
  labs(x="Tweet Word Count", title="Trump Tweet Length vs Favorites Colored by Political Era") +
# Adding the minimal ggplot theme.
  theme_minimal() +
# Adding the color brewer set 2 palette. 
  scale_color_brewer(name="Poltical Era", palette = "Set2") 
tweet_scatter
Warning in scale_y_log10("Total Favorites (log scale)"): log-10 transformation
introduced infinite values.

Essay

I used the trump_tweets dslabs dataset for this project, which provides data on trump’s tweets from 2009-2017. I counted the words in each tweet and placed this word count in a new column using the stringr and mutate functions. I found instructions on how to use stringr to count words here: https://r-statistics.co/stringr-str_count-in-R.html. I also made a new column noting which political era a tweet came from by using the mutate and case_when functions to send tweets to different categories based on if they were before 06/15/2015 (campaign announcement) or 11/09/2016 (the day after the election). To make the scatterplot, I used ggplot and geom_point and plotted word count in the x axis, favorite count on the y axis, and colored the points by political era. I used a log scale for the y axis.