---
title: "Advanced Graphics"
author: "JJB + Course"
date: "03/08/2019"
output:
   html_document:
     toc: true
     toc_float:
       collapsed: false
---

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

## Example: Pyramid Plot

```{r}
par(mar = c(0, 1, 0, 1))
pie(
  c(280, 60, 20),
  c('Sky', 'Sunny side of pyramid', 'Shady side of pyramid'),
  col = c('#0292D8', '#F7EA39', '#C4B632'),
  init.angle = -50, border = NA
)
```

## Example: Pie vs. Bar Plot

```{r pie-v-bar}
library("ggplot2")

# produce a pie chart
ggplot(mpg, aes(x = 1, fill=factor(drv))) + 
  geom_bar() +
  coord_polar(theta='y')

# produce an equivalent bar chart
ggplot(mpg, aes(x = drv, fill=factor(drv))) + 
  geom_bar() 
```

## Example: Animation

Stagnate ggplot2

```{r stagnet-ggplot2}
# install.packages("gapminder")
library("gapminder")
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent)
```

Animated ggplot2

```{r ggplot2-animated-new, eval = FALSE}
# install.packages("devtools")
# devtools::install_github("thomasp85/gganimate")
library("gganimate")
a = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  labs(title = "Year: {frame_time}", x = "GDP per capita", y = "Life Expectancy") +
  transition_time(year) +
  ease_aes("linear")

anim_save("gapminder_evolve.gif", animation = last_animation())

animate( plot = a, width = 700, height = 400)
anim_save("gapminder_evolve.gif")
```


```{r adv-seasonality, eval = FALSE}
library("tidyverse")
library("lubridate")
library("gganimate")

# Fake Data
set.seed(42)

sample = tibble(
  date = seq.Date(from = ymd(20160101), to = ymd(20181001), by = "day"),
  chg = runif(length(date), min = -10, max = 10),
  value = cumsum(chg)
) %>%
  select(-chg)

frame_count = (max(sample$date) - min(sample$date)) / lubridate::ddays(1)
cycle_length = 365

sample3 = map_df(seq_len(frame_count), ~sample, .id = "id") %>%
  mutate(id = as.integer(id)) %>%
  mutate(view_date = min(sample$date) + id - 1) %>%
  filter(date <= view_date) %>%
  mutate(days_ago = (view_date - date) / ddays(1), 
         phase_dif = (days_ago %% cycle_length) / cycle_length,
         x_pos = -sin(2*pi * phase_dif),
         nearness = cos(2*pi * phase_dif))

b = ggplot(sample3) + 
  geom_path(aes(x_pos, value, alpha = nearness,
                color = days_ago, size = -days_ago)) + 
  scale_size(range = c(0, 2)) + 
  transition_manual(id) + theme_void() + 
  guides(size = "none", alpha = "none", color = "none")

animate(b, fps = 25, duration = 15, width = 500, height = 300)
anim_save("seasonality_evolve.gif")
```


## Example: Interactive Graphics

```{r graphing-boxplot-with-points-plotly}
# install.packages("plotly")
library("plotly")
g = ggplot(mtcars, aes(x = factor(gear), y = mpg, color = cyl)) +
  geom_boxplot() + 
  geom_jitter(size = 2) + labs(
    title = "Box Plot",
    x = "Gears",
    y = "MPG"
  )

g
ggplotly(g)
```


```{r graphing-barplot-plotly}
# devtools::install_github("tidyverse/ggplot2")
library("plotly")
a = ggplot(mpg, aes(class)) + 
 geom_bar(aes(fill = drv), 
          position = position_stack(reverse = TRUE)) +
 coord_flip() +
 theme(legend.position = "top")

a

ggplotly(a)
```



```{r view-airquality}
head(airquality)

g = ggplot(airquality, aes(x = Ozone, y = Solar.R, color = Month)) +
  geom_point()

ggplotly(g)
```



