Map & bar chart

.

---
title: "Map & bar chart with ggbump"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    orientation: columns
    source_code: embed
    vertical_layout: fill
    theme: simplex
---



```{r setup, include=FALSE}


# PACKAGES / LIBRARIES:

library(flexdashboard)
library(tidyverse)
library(sf)
library(rnaturalearth)
library(lubridate)
library(BBmisc)
library(hablar)
library(ggbump)
library(feather)
library(janitor)
library(gapminder)

options(scipen=1000)


## SOURCE: https://github.com/davidsjoberg/tidytuesday/blob/master/2020w17/2020w17_skript.R

library(gapminder)


# 1. SELECTION OF COUNTRIES. 

data <- gapminder::gapminder %>%
  filter(continent == "Americas", year == 2007) %>%
  arrange(desc(gdpPercap)) %>%
  select(country, gdpPercap)
  

df <- data[-c(1,2,3,4,7,11,13,15,18, 19, 20 ,23, 24, 25),]



# 2. SPATIAL DATAFRAME

sdf <- rnaturalearthdata::countries110 %>% 
  st_as_sf()  %>% 
  st_crop(xmin = -100, xmax = -25, ymin = -60, ymax = 20) %>% 
  filter(admin %in% df$country) %>%
  left_join(df, by = c("admin" = "country"))



# 3. RANKING


ranking <- st_geometry(sdf) %>% 
  st_point_on_surface() %>% 
  st_coordinates() %>% 
  as_tibble() %>% 
  bind_cols(tibble(fine_cap = normalize(rank(sdf$gdpPercap), range = c(-40.2, -10.2), method = "range"),
                   country = sdf$admin,
                   xend = 0,
                   x_axis_start = xend + 10,
                   fine_cap_x = normalize(sdf$gdpPercap, range = c(first(x_axis_start), 80), method = "range"),
                   val_txt = paste0(format(sdf$gdpPercap, digits = 1, nsmall = 0))))

ranking$X[4] = -74
ranking$Y[4] = -48.5

sdf <- sdf %>% 
  bind_cols(ranking %>% select(fine_cap))

```


Map & bar chart
=================================================================================================

### .

```{r, out.width="100%", fig.width=12, fig.height=5}


ggplot() + 
  geom_sf(data = sdf, size = .1, fill = "transparent", color = "white") +  
  geom_sigmoid(data = ranking, 
               aes(x = X, y = Y, xend = x_axis_start - .4, yend = fine_cap, group = country, color = fine_cap), 
               alpha = .6, smooth = 15, size = 1) + 
  geom_segment(data = ranking, 
               aes(x = x_axis_start + 1 , y = fine_cap, xend = fine_cap_x, yend = fine_cap, color = fine_cap), 
               alpha = .8, size = 3, 
               lineend = "round") + 
  geom_segment(data = ranking, 
               aes(x = x_axis_start, y = -45, xend = x_axis_start, yend = 0), alpha = .8, size = 1.5, color = "black") +
  geom_point(data = ranking, 
             aes(x = X, y = Y, color = fine_cap), size = 2) +
  geom_text(data = ranking, aes(x = x_axis_start-.5, y = fine_cap, label = country, color = fine_cap), 
            hjust = 1, size = 3, nudge_y = 1.5) +
  geom_text(data = ranking, aes(x = fine_cap_x + 1, y = fine_cap, label = val_txt, color = fine_cap), 
            hjust = 0, size = 2.5, nudge_x = 1) +
  coord_sf(clip = "off") +
  scale_fill_viridis_c(option = "plasma") +
  scale_color_viridis_c(option = "plasma") +
  theme_void() +
  labs(title = "GDP per capita (year 2007)",
       subtitle = str_wrap("US Dollars, inflation-adjusted", 100),
       caption = "This plot is a replication of David Sjöberg's (@davsjob) example but applied to South America with data from gapminder.org") + 
  theme(plot.margin = margin(.5, 1, .5, .5, "cm"),
        legend.position = "none",
        plot.background = element_rect(fill = "black"),
        plot.caption = element_text(color = "lightgrey"),
        plot.title = element_text(color = "lightgrey", size = 16, family = "Helvetica", face = "bold"),
        plot.subtitle = element_text(color = "lightgrey", size = 8))



```