DS Labs Assignment

Author

Sephora Apeti

For this assignment I will use the polls_us_election_2016.

Loading packages:

I loaded the dslabs package so I can retrieve the data set I will be using. I also loaded tidyverse which has the functions I need to make my scatter-plot.

library(dslabs)
library(tidyverse)
── 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

Loading Data Set:

I will load the data set and check for the first few rows of the data-set.

data(polls_us_election_2016)
head(polls_us_election_2016 )
  state  startdate    enddate
1  U.S. 2016-11-03 2016-11-06
2  U.S. 2016-11-01 2016-11-07
3  U.S. 2016-11-02 2016-11-06
4  U.S. 2016-11-04 2016-11-07
5  U.S. 2016-11-03 2016-11-06
6  U.S. 2016-11-03 2016-11-06
                                                    pollster grade samplesize
1                                   ABC News/Washington Post    A+       2220
2                                    Google Consumer Surveys     B      26574
3                                                      Ipsos    A-       2195
4                                                     YouGov     B       3677
5                                           Gravis Marketing    B-      16639
6 Fox News/Anderson Robbins Research/Shaw & Company Research     A       1295
  population rawpoll_clinton rawpoll_trump rawpoll_johnson rawpoll_mcmullin
1         lv           47.00         43.00            4.00               NA
2         lv           38.03         35.69            5.46               NA
3         lv           42.00         39.00            6.00               NA
4         lv           45.00         41.00            5.00               NA
5         rv           47.00         43.00            3.00               NA
6         lv           48.00         44.00            3.00               NA
  adjpoll_clinton adjpoll_trump adjpoll_johnson adjpoll_mcmullin
1        45.20163      41.72430        4.626221               NA
2        43.34557      41.21439        5.175792               NA
3        42.02638      38.81620        6.844734               NA
4        45.65676      40.92004        6.069454               NA
5        46.84089      42.33184        3.726098               NA
6        49.02208      43.95631        3.057876               NA

Scatter-plot:

compares Clinton and Trump polling results

ggplot(polls_us_election_2016,
       aes(x = rawpoll_clinton,
           y = rawpoll_trump,
           color = population)) +
  geom_point() +
  labs(
    title = "Clinton and Trump Polling Results in 2016",
    x = "Clinton Polling Percentage",
    y = "Trump Polling Percentage",
    color = "Population",
    caption = "Source: dslabs package"
  ) +
  theme_minimal()

The dataset I used is polls_us_election_2016 from the dslabs package. This dataset contains polling information from the 2016 U.S. presidential election. I created a scatterplot comparing the polling percentages for Clinton and Trump. I used population as a third variable and represented it with different colors. I also added a title, meaningful axis labels, a legend, and the theme_minimal() theme to change the default ggplot appearance.