Download
library(here)
# paths
url <- "https://www.dropbox.com/sh/z289k8mztx326lf/AAArsgbv3pUHxaM1p_ygslUsa?dl=1"
dir_data <- here("data")
zip <- file.path(dir_data, "ESRM_DataViz.zip")
xlsx <- file.path(dir_data, "CoastalOpinionPoll_thru2017.xlsx")
if (!file.exists(xlsx)){
# download and unzip
dir.create(dir_data)
download.file(url, zip)
unzip(zip, exdir = dir_data)
unlink(zip)
}
Read
library(readxl)
library(tidyverse)
library(knitr)
n_max <- 12891-7
d <- read_excel(
xlsx,
n_max = n_max, guess_max = n_max, skip = 7) %>%
select(
year = "..9",
answered = answered..157,
yes = yes..158,
no = no..159,
unsure = unsure..160) %>%
filter(
!is.na(year),
answered == 1)
d %>%
head() %>%
kable()
2008 |
1 |
1 |
0 |
0 |
2008 |
1 |
1 |
0 |
0 |
2008 |
1 |
0 |
0 |
1 |
2008 |
1 |
0 |
1 |
0 |
2008 |
1 |
1 |
0 |
0 |
2008 |
1 |
0 |
0 |
1 |
Summarize
library(DT)
d_s <- d %>%
mutate(
year = as.integer(year)) %>%
group_by(year) %>%
summarise(
yes = sum(yes),
no = sum(no),
unsure = sum(unsure)) %>%
gather(
answer, sum, -year) %>%
arrange(year, answer)
datatable(d_s)
Plot, static
library(RColorBrewer)
# Stacked Percent
rdylgn <- brewer.pal(5,"RdYlGn")
g <- ggplot(d_s, aes(fill=answer, y=sum, x=year)) +
geom_bar( stat="identity", position="fill") +
scale_fill_manual(values=c(rdylgn[1], "grey50", rdylgn[5])) +
ylab("%") +
theme_minimal()
g
Plot, dynamic
library(plotly)
ggplotly(g)