We are going to be using the drinks dataset from the fivethirtyeight package reported in Mona Chalabi’s article Dear Mona Followup: Where Do People Drink The Most Beer, Wine, and Spirits?
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(readr)
library(dplyr)
library(ggplot2)
library(fivethirtyeight)
## Some larger datasets need to be installed separately, like senators and
## house_district_forecast. To install these, we recommend you install the
## fivethirtyeightdata package by running:
## install.packages('fivethirtyeightdata', repos =
## 'https://fivethirtyeightdata.github.io/drat/', type = 'source')
# read the given csv and verify
drinks <- read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv")
head(drinks)
## country beer_servings spirit_servings wine_servings
## 1 Afghanistan 0 0 0
## 2 Albania 89 132 54
## 3 Algeria 25 0 14
## 4 Andorra 245 138 312
## 5 Angola 217 57 45
## 6 Antigua & Barbuda 102 128 45
## total_litres_of_pure_alcohol
## 1 0.0
## 2 4.9
## 3 0.7
## 4 12.4
## 5 5.9
## 6 4.9
# Show new "names" or filtered fields in dataset "type_alcohol" and delete total_litres_of_pure_alchohol from our view
Total_drinks <- drinks %>% rename(Beer=beer_servings, Spirits=spirit_servings, Wine=wine_servings) %>% filter(country %in% c("USA","Greece","Seychelles","Iceland")) %>% select(-total_litres_of_pure_alcohol)
# Use gather function for data sort on "type" field. With the gather syntax, we set our key to type of beverage and our value on number of servings from the dataset. The -country function remove the totals from the given fields. Also, gather() initiates key-value pairs for our 2 fields, type and servings.
drinks1 <- Total_drinks %>% gather(key = "Type", value = "servings", -country)
head(drinks1)
## country Type servings
## 1 Greece Beer 133
## 2 Iceland Beer 233
## 3 Seychelles Beer 157
## 4 USA Beer 249
## 5 Greece Spirits 112
## 6 Iceland Spirits 61
# Plot our type of drinks, sort by country and our set value of servings. Fill = type allows for the type of drink in our legend, rather than the total count of servings per country.
ggplot(drinks1, aes(x = country, y = servings,fill = Type)) +
geom_col(position = "dodge") + coord_flip()