---
title: "Make a bar chart race of the biggest cities in France"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{bar-chart-race}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(vizoR)
library(dplyr, warn.conflicts = FALSE)
library(ggplot2)
library(hrbrthemes)
library(gganimate)
```


Load data
```{r load_data}
# load data
data("base_pop")

# create the ranks based on census populations
base_pop<- base_pop %>%
  group_by( annee) %>%
  arrange(desc(population)) %>%
  mutate(rang = 1:n()) %>%
  ungroup()

```


Extract the list of top n cities
```{r extract_top_n}
n_cities = 10

top_cities <- base_pop %>% filter(rang <= n_cities) %>%
  select(ville, dep, region) %>% distinct()

```


Create all missing dates
```{r, combine_dates}
# create a data frame with all the years between min and max Year
all_years <- data.frame(annee = seq(min(base_pop$annee), max(base_pop$annee), 1))

# combine top_cities and all_years
all_combos <- merge(top_cities, all_years, all = T)

# combine all_combos with the original dataset
res_interp <- merge(base_pop, all_combos, all.y = T)
```

Interpolate the populations when missing (linear interpolation here)
```{r, interpolate}
res_interp  <- res_interp  %>%
  group_by(ville) %>%
  mutate(population=approx(annee,population,annee)$y)
```


```{r, filter_for_plot}
# filter the top ten cities per year
to_plot <- res_interp %>%
  group_by(annee) %>%
  arrange(-population) %>%
  mutate(rang=row_number()) %>%
  filter(rang<=n_cities) %>%
  rename(`Région` = region)

```


Make the plot
```{r, make_plot}
p <- to_plot %>%

  ggplot(aes(x = -rang,y = population, group =ville)) +
  geom_tile(aes(y = population / 2, height = population, fill = `Région`), width = 0.9) +
  geom_text(aes(label = ville), hjust = "right", colour = "white", fontface="bold", nudge_y = -10000) +
  geom_text(aes(label = scales::comma(population,big.mark = ' ')), hjust = "left", nudge_y = 10000, colour = "grey90") +
  coord_flip(clip="off") +
  hrbrthemes::scale_fill_ipsum() +
  scale_x_discrete("") +
  scale_y_continuous("",labels=scales::comma_format(big.mark = " ")) +
  theme_elegant_dark(base_size = 20) +
  theme(
    panel.grid.minor.x=element_blank(),
    axis.line = element_blank(),
    panel.grid.major= element_line(color='lightgrey', size=.2),
    legend.position = c(0.6, 0.2),
    plot.margin = margin(1,1,1,2,"cm"),
    plot.title = element_text(hjust = 0),
    axis.text.y=element_blank(),
    legend.text = element_text(size = 15),
    legend.background = element_blank()) +
  # gganimate code to transition by year:
  transition_time(annee) +
  ease_aes('cubic-in-out') +
  labs(title='Evolution des plus grandes villes de France',
       subtitle='Population en {round(frame_time,0)}',
       caption='Source: INSEE Base populations historiques 1876-2015')

```

```{r, animate}
animate(p, nframes = 350, fps = 25, end_pause = 30, width = 1200, height = 1200, start_pause = 15 )

```


