---
title: "Live Code for Day 1"
author: "Dave Armstrong"
date: "5/27/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning=FALSE)
```

## mutate

```{r}
library(gapminder)
library(ggplot2)
library(dplyr)
gm4 <- gapminder %>% 
  filter(country %in% c("Canada", "United States", "Australia", "United Kingdom")) %>%
  mutate(country = droplevels(country), 
         loggdp = log(gdpPercap))
  
gm4a <- gapminder %>% 
  filter(country %in% c("Canada", "United States", "Australia", "United Kingdom")) %>%
  mutate(country = droplevels(country)) %>%
  group_by(country) %>% 
  mutate(meangdp = mean(gdpPercap, na.rm=TRUE))

head(gm4a)


g1 <- ggplot(gm4, aes(x=year, y=gdpPercap, colour=country)) + 
  geom_line() +
  theme_bw() + 
  labs(x="Year", y="GDP/capita", colour="Country") 
g1
```


## code to import data

```{r, eval=FALSE}
library(rio)
mydata <- import("path-to-file")
```

## plotly with linear model code

```{r}
# manage our data
library(gapminder)
gm4 <- gapminder %>% 
  filter(country %in% c("Canada", "United States", "Australia", "United Kingdom")) %>%
  mutate(country = droplevels(country))

# run a model


mod <- lm(lifeExp ~ gdpPercap, data=gm4)

# use model output to make the plot

library(broom)
library(plotly)
augment(mod) %>%
  plot_ly(showlegend=FALSE) %>% 
  add_markers(x=~gdpPercap, y=~lifeExp) %>% 
  add_lines(x=~gdpPercap, y=~.fitted) %>% 
  layout(
    xaxis = list(title="GDP/capita"),
    yaxis = list(title="Life Expectancy")
  )
```

## plotly with different regression 

```{r}
mod2 <- lm(lifeExp ~ gdpPercap*country, data=gm4)

# use model output to make the plot

library(broom)
library(plotly)
augment(mod2) %>%
  plot_ly(showlegend=FALSE) %>% 
  add_markers(x=~gdpPercap, y=~lifeExp, color = ~country) %>% 
  add_lines(x=~gdpPercap, y=~.fitted, color=~country) %>% 
  layout(
    xaxis = list(title="GDP/capita"),
    yaxis = list(title="Life Expectancy")
  )

```


## plotly with different regressions

```{r}
mod3 <- lm(lifeExp ~ gdpPercap*country + log(pop), data=gm4)

# use model output to make the plot

library(plotly)

pred.data <- gm4 %>% 
  mutate(pop= mean(pop, na.rm=TRUE))
preds <- predict(mod3, newdata = pred.data)
pred.data <- pred.data %>% 
  mutate(fitted= preds)

plot_ly(pred.data, showlegend=FALSE) %>% 
#  add_markers(x=~gdpPercap, y=~lifeExp, color = ~country) %>% 
  add_lines(x=~gdpPercap, y=~fitted, color=~country) %>% 
  layout(
    xaxis = list(title="GDP/capita"),
    yaxis = list(title="Life Expectancy")
  )

```



```{r}
predict.mat <- function(obj, data, nvals=25){
  X <- get_all_vars(obj, data)
  seq_range <- function(x, n)seq(min(x, na.rm=TRUE), max(x, na.rm=TRUE), length=n)
  s.x <- seq_range(X[,2], nvals)
  s.y <- seq_range(X[,3], nvals)
  eg <- expand.grid(s.x, s.y)
  names(eg) <- names(X)[2:3]
  preds <- predict(obj, newdata=eg)
  return(list(x=s.x, y=s.y, z=matrix(preds, ncol=nvals, byrow=TRUE)))
}
```

```{r}
mod4 <- lm(lifeExp ~ gdpPercap + pop, data=gm4)

preds <- predict.mat(mod4, gm4)

plot_ly(gm4, showlegend=FALSE) %>% 
  add_markers(x=~gdpPercap, y=~pop, z = ~lifeExp, color=~country, showlegend=FALSE) %>% 
  add_surface(x=preds$x, y=preds$y, z=preds$z, showscale=FALSE, opacity=.5) %>%
  layout(scene = list(
    xaxis=list(title="GDP/capita"), 
    yaxis=list(title="Population"), 
    zaxis=list(title="Life Expectancy")
  ))
```

# ohio map

```{r}
load("counties.rda")
library(sf)
library(dplyr)
library(plotly)

ohio <- counties %>% 
  filter(state %in% c("Ohio")) %>% 
  mutate(text = paste(NAMELSAD, "\n", cases, " cases", sep=""))

ohio_map <- plot_ly(ohio, split = ~ text,  color = ~log(cases),  alpha = 1, 
        hoverinfo="text", hoveron="fill",  showlegend=FALSE)
ohio_map
```



```{r}
library(sf)
library(plotly)
library(dplyr)
load("counties.rda")

florida <- counties %>% 
  filter(state %in% c("Florida")) %>% 
  mutate(text = paste(NAMELSAD, "\n", deaths, " deaths", sep=""))

plot_ly(florida, split = ~text,  color = ~asinh(deaths),  alpha = 1, type="scatter", 
        hoverinfo="text", hoveron="fill",  showlegend=FALSE)
plot_ly(florida, split = ~deaths,  color = ~asinh(deaths),  alpha = 1, type="scatter", 
        hoverinfo="text", hoveron="fill",  showlegend=FALSE)

```


```{r}
library(rio)
library(dplyr)
library(crosstalk)
library(leaflet)
library(DT)

scad <- import("~/Dropbox/Interactive_Viz/deck2/scad_africa.csv")
samp <- sample(1:nrow(scad), 500, replace=FALSE)
scad <- scad %>% 
  mutate(date = as.Date(scad$startdate, "%d-%b-%y")) %>% 
  select(latitude, longitude, countryname, styr, npart, ndeath, issue1) %>% 
  filter(1:nrow(scad) %in% samp)

scadShare <- SharedData$new(scad)
bscols(widths = c(6,6), 
       list(
         leaflet(scadShare, width="100%", height=400) %>% addTiles() %>% addMarkers(), 
         filter_slider("yr", "Year", scadShare, ~styr, sep="", step=1, round=TRUE)
       ),
       datatable(scadShare, extensions="Scroller", style="bootstrap", class="compact", width="100%", 
                 colnames=c("Longitude", "Latitude", "Country", "Start Year", "# Participants", 
                            "# Killed", "Issue"),
                 options = list(paging=FALSE, pageLength = 20, scrollY = "200px"), 
                 fillContainer = TRUE) %>%
         formatRound(columns=c("latitude", "longitude"), digits=1)
)

```


## with filter select

```{r}

scad <- scad %>% mutate(issue="")
iss <- c("elections", "economy", "jobs", "food/water", 
         "environment", "ethnic issues", "religious issues", 
         "education", "foreign affairs", "domestic war", 
         "human rights", "pro-government", "economic resources", "other")
for(i in 1:length(iss)){
  scad$issue[which(scad$issue1 == i)] <- iss[i]
}

scadShare <- SharedData$new(scad)
bscols(widths = c(6,6), 
       list(
         leaflet(scadShare, width="100%", height=400) %>% addTiles() %>% addMarkers(), 
         filter_slider("yr", "Year", scadShare, ~styr, sep="", step=1, round=TRUE),
         filter_select("iss", "Issue", scadShare, ~issue, multiple=TRUE)
       ),
       datatable(scadShare, extensions="Scroller", style="bootstrap", class="compact", width="100%", 
                 colnames=c("ID", "Longitude", "Latitude", "Country", "Start Year", "# Participants", 
                            "# Killed", "Issue Number", "Issue"),
                 options = list(paging=FALSE, pageLength = 20, scrollY = "200px"), 
                 fillContainer = TRUE) %>%
         formatRound(columns=c("latitude", "longitude"), digits=1)
)




```


# chart A

```{r}
library(ggplot2)
data(diamonds)
ggplot(diamonds, aes(x=carat, y=price, color=color)) + 
  geom_point(alpha=.15) + 
  facet_wrap(~cut) + 
  labs(x="Carat", y="Price", color="Color") + 
  theme_bw() + 
  guides(colour = guide_legend(override.aes = list(alpha=1))) + 
  scale_y_log10()
```


# Chart B

```{r}
ggplot(diamonds, aes(x=clarity)) + 
  geom_bar() + 
  theme_bw() + 
  labs(x="Clarity", y="Frequency")

```


# chart C

```{r}
diamonds %>% 
  filter(carat > .75 & carat <=1) %>% 
  group_by(clarity, cut) %>% 
  summarise(price = mean(price), n=n()) %>% 
  ggplot(aes(x=clarity, y=cut, fill=price)) + 
    geom_tile() + 
    geom_text(aes(label=n), col="white") + 
    theme_minimal() + 
    labs(x = "", y = "", fill="Average\nPrice")

```


## COVID Dashboard

```{r  include=FALSE, echo=FALSE}
library(flexdashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(DT)
library(sf)
library(stringr)
load("../../counties_covid.rda")

ohio <- counties_covid %>% 
  filter(state %in% c("Ohio")) %>% 
  mutate(text = paste(NAMELSAD, "\n", cases, " cases", sep=""))

library(crosstalk)
library(d3scatter)
library(leaflet)
ohio2 <- ohio %>% 
  select(county, st, cases, repvote, geometry) %>% 
  mutate(repvote = repvote*100, 
         text = paste0(county, "\n", cases, " cases"))
sohio <- SharedData$new(ohio2)
```

```{r, result='asis', echo=FALSE, eval=FALSE}
## using Hipster Ipsum place-holder text.
con <- curl::curl("https://hipsum.co/api/?type=hipster-centric&sentences=6")
hip <- rjson::fromJSON(readLines(con))
htmltools::p(hip)
```

### Chart A


```{r}
plot_ly(sohio, split = ~ text,  color = ~log(cases),  alpha = 1, 
        hoverinfo="text", hoveron="fill",  showlegend=FALSE)
```


### Chart B

```{r}
d3scatter(sohio, ~repvote, ~cases, width="100%")
```

### Chart C

```{r}
sohio %>% 
  datatable(extensions="Scroller", class="compact",
            colnames = c("County", "State", "# Cases", "Republican\nVote"), 
            rownames=FALSE, 
            options = list(paging=FALSE, pageLength = 20, scrollY = "350px",
                            columnDefs = list(list(className = 'dt-center', 
                                                   targets = 0:3), 
                                              list(visible=FALSE, targets=c(4,5))))) %>% 
  formatRound(columns = "repvote", digits=0)
```

