---
title: "Lecture 0" 
subtitle: "Spatio-temporal data"
author: "Colin Rundel"
date: "8/28/2018"
fontsize: 11pt
output: 
  beamer_presentation:
    theme: metropolis
    highlight: pygments
    fig_height: 6
    fig_caption: FALSE
    latex_engine: xelatex
    keep_tex: true
    includes:
      in_header: ../settings.tex
---

```{r packages, include=FALSE}
library(sf)
library(ggplot2)
library(dplyr)
library(tidyr)
```

```{r config, include=FALSE}
knitr::opts_chunk$set(
  fig.width=7,
  fig.height=5,
  out.width="\\textwidth",
  fig.align="center",
  echo=FALSE,
  warning=FALSE
)

theme_set(theme_bw())
```


## Time Series Data - Discrete
    
```{r message=FALSE}
x = quantmod::getSymbols("^GSPC", src = "yahoo", from = as.Date("2018-01-01"), auto.assign = FALSE)

plot(x[,"GSPC.Open"], main='S&P 500 Open (^GSPC)', type='b', pch=16, col="black")
```

## Time Series Data - Continuous
  
```{r}
load("data/frn_example.Rdata")

pm25 = pm25 %>% 
  transmute(date = lubridate::mdy(Date), pm25 = PM25) %>%
  filter(lubridate::month(date) %in% 1:12)

ggplot(pm25, aes(x=date, y=pm25)) +
  geom_point() +
  geom_line() +
  labs(
    title="FRN Measured PM25",
    ylab="PM25 (ug/m^3)"
  )
```


## Spatial Data - Areal
    
```{r out.width="0.8\\textwidth"}
nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>%
  select(starts_with("SID")) %>%
  gather(var,value, -geometry)

ggplot(nc, aes(fill=value)) +
  geom_sf() +
  coord_sf(datum = NA) +
  facet_grid(var~.) +
  scale_fill_viridis_c()
```

## Spatial Data - Point referenced
    
```{r}
data(meuse, package = "sp")
m = st_as_sf(meuse, coords=c("x","y")) %>% 
  select(2:4) %>%
  mutate_if(is.numeric, function(x) x / max(x)) %>%
  gather(metal, value, -geometry)

data(meuse.riv, package = "sp")
mr = st_polygon(list(meuse.riv)) %>% st_sfc() %>% st_sf()

ggplot(m, aes(color=value)) +
  geom_sf(data=mr, color=NA, fill="lightblue", alpha=0.5) +
  geom_sf() +
  coord_sf(datum = NA) +
  facet_wrap(~metal) + 
  theme_bw() +
  ylim(328000,334000) +
  scale_colour_viridis_c() +
  #scale_color_distiller(palette = "Spectral") +
  labs(title="Meuse River", color="% of max")
```

## Point Pattern Data - Time
   
```{r}
old_faithful = MASS::geyser %>% 
  mutate(time = cumsum(waiting)+cumsum(lag(duration,default=0))) %>% 
  select(time, duration) %>% 
  slice(1:25)

plot(old_faithful, type='p', main="Old Faithful Eruption Duration", pch=16)
```


## Point Pattern Data - Space
    
```{r out.width="0.8\\textwidth"}
knitr::include_graphics("imgs/fires.png")
```


## Point Pattern Data - Space + Time
        
```{r out.width="0.8\\textwidth"}
knitr::include_graphics("imgs/earthquakes.png")
```


