---
title: "Exploring wine data"
author: "VP"
date: "2017-08-17"
output: html_document
---

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


Here are some examples of charts made with the [`billboarder`](https://github.com/dreamRs/billboarder) package, an htmlwidget interfacing R and the [billboard.js](https://naver.github.io/billboard.js/) library.
We'll use data about wine in european countries from [Eurostat](http://ec.europa.eu/eurostat), we'll get those with [`eurostat`](https://ropengov.github.io/eurostat/) package.



## Packages & Data

We'll use these packages :

```{r packages}
library( "billboarder" ) # for charts
library( "eurostat" ) # for data
library( "dplyr" ) # for data manipulation
library( "tidyr" ) # for spread
```

Get the data :

```{r data}
eu_wine <- get_eurostat(id = "apro_cpb_wine", time_format = "num")
eu_wine <- label_eurostat(eu_wine)
str(eu_wine)
```



## Top 10 producer in 2016

We represent the 10 countries that produce the most wine for the year 2016 :

```{r top10}
# some data munging
top_10 <- eu_wine %>% 
  filter(prod_bal == "Wine - Total",
         bal_item == "Official production (1000 hl)",
         time == "2016") %>% 
  top_n(n = 10, wt = values) %>% 
  mutate(geo = gsub(" \\(.*", "", geo)) %>% 
  arrange(desc(values)) %>% 
  select(geo, values)

# and the chart
billboarder() %>% 
  bb_barchart(data = top_10, rotated = TRUE) %>% 
  bb_data(names = list("values" = "Official production (1000 hl)")) %>%
  bb_y_grid(show = TRUE) %>%
  bb_y_axis(tick = list(values = seq(0, 5e4, 1e4))) %>% 
  bb_labs(title = "Top 10 wine producer in EU for 2016", caption = "Data source: Eurostat")
```


## Gross human consumption

Which European countries consume the most P.D.O. wines?

PDO (Protected Designation of Origin) is an European label, in French it's called "AOC (Appellation d'Origine Contrôlée)".


```{r}
ghc <- eu_wine %>% 
  filter(time == 2016) %>% 
  filter(prod_bal %in% c("P.D.O. - Red and rose wine", "P.D.O. -  white wine")) %>% 
  filter(bal_item == "Gross human consumption (1000 hl)") %>% 
  mutate(geo = gsub(" \\(.*", "", geo)) %>% 
  mutate(type = if_else(grepl("white", prod_bal), "White wine", "Red and rose wine")) %>% 
  select(geo, type, values) %>% 
  spread(type, values) %>% 
  mutate(total = `Red and rose wine` + `White wine`) %>% 
  arrange(total) %>% 
  select(-total)


billboarder() %>% 
  bb_barchart(data = ghc, stacked = TRUE, rotated = TRUE) %>% 
  bb_color(palette = c("#A74947", "#D1C659")) %>% 
  bb_y_grid(show = TRUE) %>% 
  bb_labs(title = "Gross human consumption of P.D.O. wines",
          y = "In 1000hl",
          caption = "Data source: Eurostat")
```

So you we drink a lot in France, but is it ?



## Gross human consumption per capita

In which country do you drink most?

In 2015 because there's no data for data, and for any wine (with label or not)

```{r}
ghcpc <- eu_wine %>% 
  filter(time == 2015) %>%
  filter(prod_bal %in% c("Red and rose wine", "White wine")) %>%
  filter(bal_item == "Gross human consumption per capita (lt/head)") %>% 
  mutate(geo = gsub(" \\(.*", "", geo)) %>% 
  select(geo, prod_bal, values) %>% 
  spread(prod_bal, values) %>% 
  mutate(total = `Red and rose wine` + `White wine`) %>% 
  arrange(total) %>% 
  select(-total)

billboarder() %>% 
  bb_barchart(data = ghcpc, stacked = TRUE, rotated = FALSE) %>% 
  bb_colors_manual("Red and rose wine" = "#A74947", "White wine" = "#D1C659") %>% 
  bb_y_grid(show = TRUE) %>% 
  bb_labs(title = "Gross human consumption per capita",
          y = "In lt/head",
          caption = "Data source: Eurostat")
```


Yeah Luxembourg ! And lol UK ;)



## Imports and exports for France

From 1965 to 2016 :

```{r}
balance_fr <- eu_wine %>% 
  filter(prod_bal == "Wine - Total",
         bal_item == "Total exports (for EUR : Exports to third countries) (1000 hl)" |
           bal_item == "Total imports (for EUR : imports from third countries) (1000 hl)",
         geo == "France") %>% 
  mutate(type = if_else(grepl("imports", bal_item), "imports", "exports")) %>% 
  select(time, type, values) %>% 
  spread(type, values) %>% 
  arrange(time) %>% 
  mutate(balance = exports - imports)

billboarder() %>% 
  bb_barchart(data = balance_fr %>% select(time, balance)) %>% 
  bb_y_grid(show = TRUE) %>% 
  bb_x_axis(tick = list(fit = FALSE)) %>% 
  bb_legend(show = FALSE) %>% 
  bb_labs(title = "Wine imports and exports in France", 
          y = "Exports minus Imports",
          caption = "Data source: Eurostat")
```



## PDO wine production for top 3 producers

In Italy, Spain and France :

```{r}
pdo_frspit <- eu_wine %>% 
  filter(geo %in% c("France", "Spain", "Italy")) %>% 
  filter(prod_bal %in% c("P.D.O. Total wine")) %>% 
  filter(bal_item == "Official production (1000 hl)") %>% 
  select(time, geo, values) %>% 
  spread(geo, values)

billboarder() %>% 
  bb_barchart(data = pdo_frspit) %>% 
  bb_colors_manual("France" = "#00267F", "Italy" = "#009246", "Spain" = "#C60B1E") %>% 
  bb_data(labels = TRUE) %>% 
  bb_y_grid(show = TRUE) %>% 
  bb_legend(position = "right") %>% 
  bb_labs(title = "PDO (AOC) production in France, Spain and Italy", 
          y = "Official production (1000 hl)", 
          caption = "Data source: Eurostat")
```




## Wine with EU label in Italy


<div class="row">
<style>.bb-chart-arcs-title{font-size: 180%; font-weight: bold;}</style>
<div class="col-sm-6">
```{r}
labeled2010_it <- eu_wine %>% 
  filter(
    bal_item=="Official production (1000 hl)",
    prod_bal %in% c("P.G.I. Total wine", "P.D.O. Total wine", "Varietal wine - Total", "Other wine - Total"),
    geo == "Italy", time == 2010
  ) %>% 
  mutate(label = if_else(grepl("P\\.D\\.O\\.|P\\.G\\.I\\.", prod_bal), "With EU label", "No label")) %>% 
  group_by(label) %>% 
  summarise(values = sum(values))


billboarder() %>% 
  bb_donutchart(data = labeled2010_it) %>% 
  bb_donut(title = "2010")
```
</div>

<div class="col-sm-6">
```{r}
labeled2016_it <- eu_wine %>% 
  filter(
    bal_item=="Official production (1000 hl)",
    prod_bal %in% c("P.G.I. Total wine", "P.D.O. Total wine", "Varietal wine - Total", "Other wine - Total"),
    geo == "Italy", time == 2016
  ) %>% 
  mutate(label = if_else(grepl("P\\.D\\.O\\.|P\\.G\\.I\\.", prod_bal), "With EU label", "No label")) %>% 
  group_by(label) %>% 
  summarise(values = sum(values))


billboarder() %>% 
  bb_donutchart(data = labeled2016_it) %>% 
  bb_donut(title = "2016")
```
</div>

</div>




## Wine production 1965 - 2016

For readibility we kept only France, Italy and Spain (the top 3 producer)

```{r}
# prep data
wine_prod <- eu_wine %>% 
  filter(geo %in% c("France", "Italy", "Spain"),
         bal_item == "Official production (1000 hl)",
         prod_bal == "Wine - Total") %>% 
  select(geo, time, values) %>% 
  spread(geo, values) %>% 
  arrange(time)
  

# line chart
billboarder() %>% 
  bb_linechart(data = wine_prod, type = "spline") %>% 
  bb_data(x = "time") %>% 
  bb_colors_manual("France" = "#00267F", "Italy" = "#009246", "Spain" = "#C60B1E") %>% 
  bb_data(color = htmlwidgets::JS("function(color, d) {return d3.rgb(color).brighter().toString();}")) %>% 
  bb_y_grid(show = TRUE) %>% 
  bb_x_grid(show = TRUE) %>% 
  bb_x_axis(tick = list(fit = FALSE)) %>% 
  bb_legend(position = "inset", inset = list(anchor = "top-right")) %>% 
  bb_labs(title = "Wine production from 1965 to 2016", 
          y = "Official production (1000 hl)", 
          caption = "Data source: Eurostat")
```


