Load all necessary libraries

library("dslabs")
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
#data(package="dslabs")
#list.files(system.file("script", package = "dslabs"))

Brexit Polls

This dataset includes 127 online and telephone polls taken at random dates 6 months prior to the Brexit referendum

data("brexit_polls")

Restructure Dataset

filterbr3<-brexit_polls %>%
  gather(decision,value="percentage", remain, leave, undecided)%>%
  filter(decision!="undecided")#, decision!="remain")

Create Interactive Scatterplot

intpoll<-filterbr3 %>%
  
  ggplot(aes(startdate, percentage, colour=decision, text = paste("Sample Size:", samplesize)))+
  scale_color_brewer(palette = "Dark2")+
  scale_color_discrete(name =  "Answers to Polls ")+
  facet_wrap(~poll_type, nrow = 1)+ #, scale="free_y")+
  theme_bw()+
  theme(plot.title = element_text(color = "blue"))+
  geom_point()+
  labs(title = "Brexit: Pollsters Got it Wrong, Again!")+
  xlab("Polls Starting Date") +
  ylab ("Percentage") 
## Scale for 'colour' is already present. Adding another scale for 'colour',
## which will replace the existing scale.
intpoll <- ggplotly(intpoll)

intpoll

For this project, I picked the “brexit_poll” dataset. I wanted to find out if this dataset was in line with the outcome of the Brexit referendum. I also wanted to find out if people’s opinions about Brexit were shifting as we were getting closer to the referendum date (June 23, 2016).

To answer those questions, I had to restructure the dataset. First, I created a new object, by adding a new column so I could gather the variables “remain”, “leave”, and “undecided”. And then, I filtered all the “undecided” out. Finally, I created two scatter plots from the new object to graphically depict the relationship between the polling dates and the polling results.

The graphs show no strong correlations between the two variables. However, the graphs, especially the one derived from the telephone’s polling, strongly suggest that Brexit would not happen.