install.packages('readr')
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
install.packages('ggplot2')
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
install.packages('tidyr')
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
library(ggplot2)
library(readr)
drinks <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## country = col_character(),
## beer_servings = col_double(),
## spirit_servings = col_double(),
## wine_servings = col_double(),
## total_litres_of_pure_alcohol = col_double()
## )
Trim data - Greece, Iceland, Seychelles, USA
library(tidyr)
drinks_tar <- (drinks[c(68, 77, 153, 185), c(1:4)])
drinks_long <- gather(drinks_tar, type, serving, beer_servings:wine_servings, factor_key=TRUE)
drinks_long$type <- gsub("_servings", " ", drinks_long$type)
drinks_long
## # A tibble: 12 x 3
## country type serving
## <chr> <chr> <dbl>
## 1 Greece "beer " 133
## 2 Iceland "beer " 233
## 3 Seychelles "beer " 157
## 4 USA "beer " 249
## 5 Greece "spirit " 112
## 6 Iceland "spirit " 61
## 7 Seychelles "spirit " 25
## 8 USA "spirit " 158
## 9 Greece "wine " 218
## 10 Iceland "wine " 78
## 11 Seychelles "wine " 51
## 12 USA "wine " 84
Visualization
ggplot(drinks_long, aes(fill=type, y=country, x=serving)) +
geom_bar(position="dodge", stat="identity")
