Here are some examples of charts made with the billboarder package, an htmlwidget interfacing R and the billboard.js library. We’ll use data about wine in european countries from Eurostat, we’ll get those with eurostat package.
We’ll use these packages :
library( "billboarder" ) # for charts
library( "eurostat" ) # for data
library( "dplyr" ) # for data manipulation
library( "tidyr" ) # for spread
Get the data :
eu_wine <- get_eurostat(id = "apro_cpb_wine", time_format = "num")
eu_wine <- label_eurostat(eu_wine)
str(eu_wine)
## Classes 'tbl_df', 'tbl' and 'data.frame': 160791 obs. of 5 variables:
## $ prod_bal: Factor w/ 31 levels "Wine - Total",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ bal_item: Factor w/ 43 levels "Official production (1000 hl)",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ geo : Factor w/ 36 levels "Austria","Belgium",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ time : num 2016 2016 2016 2016 2016 ...
## $ values : num 2268.4 10.3 1568 79.3 783 ...
We represent the 10 countries that produce the most wine for the year 2016 :
# 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")
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)”.
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 ?
In which country do you drink most?
In 2015 because there’s no data for data, and for any wine (with label or not)
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 ;)
From 1965 to 2016 :
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")
In Italy, Spain and France :
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")
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")
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")
For readibility we kept only France, Italy and Spain (the top 3 producer)
# 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")