Package Downloads

library(fivethirtyeightdata)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(readr)

Filtering Imported Data

drinkdata <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv")
## Rows: 193 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): country
## dbl (4): beer_servings, spirit_servings, wine_servings, total_litres_of_pure...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
drinkdata <- drinkdata %>% drop_na() %>%
  rename(beers=beer_servings, spirits=spirit_servings, wine=wine_servings,  total=total_litres_of_pure_alcohol) %>%
  filter(country %in% c("USA", "Seychelles", "Iceland", "Greece"))

Tibble of Filtered Data

head(drinkdata)
## # A tibble: 4 × 5
##   country    beers spirits  wine total
##   <chr>      <dbl>   <dbl> <dbl> <dbl>
## 1 Greece       133     112   218   8.3
## 2 Iceland      233      61    78   6.6
## 3 Seychelles   157      25    51   4.1
## 4 USA          249     158    84   8.7

Gathering Graph Data

names(drinkdata) <- c("country", "beers", "spirits", "wine", "total")
graphdata <- gather(drinkdata, key = "type", value = "servings", -country, -total)

Tibble of Graph Data

head(graphdata)
## # A tibble: 6 × 4
##   country    total type    servings
##   <chr>      <dbl> <chr>      <dbl>
## 1 Greece       8.3 beers        133
## 2 Iceland      6.6 beers        233
## 3 Seychelles   4.1 beers        157
## 4 USA          8.7 beers        249
## 5 Greece       8.3 spirits      112
## 6 Iceland      6.6 spirits       61

Graph and Graph Code

ggplot(data = graphdata) +
  geom_bar(mapping = aes(x = country,y = servings, fill=type), stat = "identity", position = "dodge", show.legend = TRUE) + coord_flip()