---
title: "Line charts for population data"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{bar-chart-race}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(vizoR)
library(ggplot2)
library(gghighlight)
library(dplyr)
```


```{r}

data("us_city_populations")

n_cities = 5

top_cities <- us_city_populations %>% filter(Rank <= n_cities) %>%
  select(City, State, Region) %>% distinct()

to_plot <- filter(us_city_populations, City %in% top_cities$City)
to_plot <- us_city_populations

last_ranks <- to_plot %>% filter(Year == max(Year)) %>%
  mutate(last_rank = Rank) %>% select(City, last_rank)

to_plot <- left_join(to_plot, last_ranks, by= 'City')

right_axis <-  to_plot %>% 
  group_by(City) %>% 
  top_n(1, Year) %>% 
  ungroup() %>%
  top_n(n_cities, -last_rank)

ends <- right_axis %>%
  pull(Population) 

labels <-  right_axis %>%
  pull(City)

```

```{r}
ggplot(to_plot, aes(x=Year, y = Population, group = City, color = Region)) + 
  geom_line(size=1) + 
  #geom_text(data = subset(to_plot, Year == 2010), aes(x=Inf, y = Population, label=City), hjust = 1) +
  scale_x_continuous("", expand=c(0,0))+
  scale_y_continuous("",
                     labels=scales::comma_format(big.mark = " "),
                     sec.axis = sec_axis(~ ., breaks = ends, labels = labels ))+
  theme_elegant() + 
  theme(legend.position = "bottom",
        plot.margin = unit(c(1,3,1,1), "lines"), 
        axis.line.y = element_blank(),
        axis.line.x = element_blank(), 
        axis.ticks.x = element_line(), 
        panel.grid.major.y = element_line(color= 'grey90', size = .2) ) + 
  gghighlight(max(last_rank) <= n_cities, use_direct_label = FALSE, label_key = City)
  
```

```{r}

data("base_pop")

n_cities = 10

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

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

to_plot <- filter(base_pop, ville %in% top_cities$ville)
#to_plot <- base_pop

last_ranks <- to_plot %>% filter(annee == max(annee)) %>%
  mutate(last_rank = rang) %>% select(ville, last_rank)

to_plot <- left_join(to_plot, last_ranks, by= 'ville')

right_axis <-  to_plot %>% 
  group_by(ville) %>% 
  top_n(1, annee) %>% 
  ungroup() %>%
  top_n(n_cities, -last_rank)

ends <- right_axis %>%
  pull(population) 

labels <-  right_axis %>%
  pull(ville)

```

```{r}
ggplot(to_plot, aes(x=annee, y = population, group = ville, color = region)) + 
  geom_line(size=1) + 
  #geom_text(data = subset(to_plot, Year == 2010), aes(x=Inf, y = Population, label=City), hjust = 1) +
  scale_x_continuous("", expand=c(0,0))+
  scale_y_continuous("",
                     labels=scales::comma_format(big.mark = " "),
                     sec.axis = sec_axis(~ ., breaks = ends, labels = labels ))+
  theme_elegant() + 
  theme(legend.position = "bottom",
        plot.margin = unit(c(1,3,1,1), "lines"), 
        axis.line.y = element_blank(),
        axis.line.x = element_blank(), 
        axis.ticks.x = element_line(), 
        panel.grid.major.y = element_line(color= 'grey90', size = .2) ) + 
  gghighlight(max(last_rank) <= n_cities, use_direct_label = FALSE, label_key = ville)
  
```
