install.packages("fivethirtyeight")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
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')
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
drinks <- drinks
head(drinks)
## # A tibble: 6 x 5
## country beer_servings spirit_servings wine_servings total_litres_of_pure…
## <chr> <int> <int> <int> <dbl>
## 1 Afghanistan 0 0 0 0
## 2 Albania 89 132 54 4.9
## 3 Algeria 25 0 14 0.7
## 4 Andorra 245 138 312 12.4
## 5 Angola 217 57 45 5.9
## 6 Antigua & B… 102 128 45 4.9
drinks_clean <- drinks %>%
filter(country %in% c("USA", "Seychelles", "Iceland", "Greece")) %>%
select(-total_litres_of_pure_alcohol) %>%
rename(beer = beer_servings, spirit = spirit_servings, wine = wine_servings)
drinks_clean
## # A tibble: 4 x 4
## country beer spirit wine
## <chr> <int> <int> <int>
## 1 Greece 133 112 218
## 2 Iceland 233 61 78
## 3 Seychelles 157 25 51
## 4 USA 249 158 84
drinks_final <- drinks_clean %>%
pivot_longer(names_to = "type",
values_to = "servings",
cols = -country)
head(drinks_final)
## # A tibble: 6 x 3
## country type servings
## <chr> <chr> <int>
## 1 Greece beer 133
## 2 Greece spirit 112
## 3 Greece wine 218
## 4 Iceland beer 233
## 5 Iceland spirit 61
## 6 Iceland wine 78
ggplot(drinks_final, aes(x = country, y = servings, fill = type)) +
geom_col(position = "dodge")+
coord_flip()
