Replication of ‘Where Do People Drink’ Bar Chart
1. Install libraries
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(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ stringr 1.4.0
## ✓ tidyr 1.0.2 ✓ forcats 0.4.0
## ✓ readr 1.3.1
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
2. Review and Subset Drinks Dataset
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$country
## [1] "Afghanistan" "Albania"
## [3] "Algeria" "Andorra"
## [5] "Angola" "Antigua & Barbuda"
## [7] "Argentina" "Armenia"
## [9] "Australia" "Austria"
## [11] "Azerbaijan" "Bahamas"
## [13] "Bahrain" "Bangladesh"
## [15] "Barbados" "Belarus"
## [17] "Belgium" "Belize"
## [19] "Benin" "Bhutan"
## [21] "Bolivia" "Bosnia-Herzegovina"
## [23] "Botswana" "Brazil"
## [25] "Brunei" "Bulgaria"
## [27] "Burkina Faso" "Burundi"
## [29] "Cote d'Ivoire" "Cabo Verde"
## [31] "Cambodia" "Cameroon"
## [33] "Canada" "Central African Republic"
## [35] "Chad" "Chile"
## [37] "China" "Colombia"
## [39] "Comoros" "Congo"
## [41] "Cook Islands" "Costa Rica"
## [43] "Croatia" "Cuba"
## [45] "Cyprus" "Czech Republic"
## [47] "North Korea" "DR Congo"
## [49] "Denmark" "Djibouti"
## [51] "Dominica" "Dominican Republic"
## [53] "Ecuador" "Egypt"
## [55] "El Salvador" "Equatorial Guinea"
## [57] "Eritrea" "Estonia"
## [59] "Ethiopia" "Fiji"
## [61] "Finland" "France"
## [63] "Gabon" "Gambia"
## [65] "Georgia" "Germany"
## [67] "Ghana" "Greece"
## [69] "Grenada" "Guatemala"
## [71] "Guinea" "Guinea-Bissau"
## [73] "Guyana" "Haiti"
## [75] "Honduras" "Hungary"
## [77] "Iceland" "India"
## [79] "Indonesia" "Iran"
## [81] "Iraq" "Ireland"
## [83] "Israel" "Italy"
## [85] "Jamaica" "Japan"
## [87] "Jordan" "Kazakhstan"
## [89] "Kenya" "Kiribati"
## [91] "Kuwait" "Kyrgyzstan"
## [93] "Laos" "Latvia"
## [95] "Lebanon" "Lesotho"
## [97] "Liberia" "Libya"
## [99] "Lithuania" "Luxembourg"
## [101] "Madagascar" "Malawi"
## [103] "Malaysia" "Maldives"
## [105] "Mali" "Malta"
## [107] "Marshall Islands" "Mauritania"
## [109] "Mauritius" "Mexico"
## [111] "Micronesia" "Monaco"
## [113] "Mongolia" "Montenegro"
## [115] "Morocco" "Mozambique"
## [117] "Myanmar" "Namibia"
## [119] "Nauru" "Nepal"
## [121] "Netherlands" "New Zealand"
## [123] "Nicaragua" "Niger"
## [125] "Nigeria" "Niue"
## [127] "Norway" "Oman"
## [129] "Pakistan" "Palau"
## [131] "Panama" "Papua New Guinea"
## [133] "Paraguay" "Peru"
## [135] "Philippines" "Poland"
## [137] "Portugal" "Qatar"
## [139] "South Korea" "Moldova"
## [141] "Romania" "Russian Federation"
## [143] "Rwanda" "St. Kitts & Nevis"
## [145] "St. Lucia" "St. Vincent & the Grenadines"
## [147] "Samoa" "San Marino"
## [149] "Sao Tome & Principe" "Saudi Arabia"
## [151] "Senegal" "Serbia"
## [153] "Seychelles" "Sierra Leone"
## [155] "Singapore" "Slovakia"
## [157] "Slovenia" "Solomon Islands"
## [159] "Somalia" "South Africa"
## [161] "Spain" "Sri Lanka"
## [163] "Sudan" "Suriname"
## [165] "Swaziland" "Sweden"
## [167] "Switzerland" "Syria"
## [169] "Tajikistan" "Thailand"
## [171] "Macedonia" "Timor-Leste"
## [173] "Togo" "Tonga"
## [175] "Trinidad & Tobago" "Tunisia"
## [177] "Turkey" "Turkmenistan"
## [179] "Tuvalu" "Uganda"
## [181] "Ukraine" "United Arab Emirates"
## [183] "United Kingdom" "Tanzania"
## [185] "USA" "Uruguay"
## [187] "Uzbekistan" "Vanuatu"
## [189] "Venezuela" "Vietnam"
## [191] "Yemen" "Zambia"
## [193] "Zimbabwe"
subset_drinks <- drinks %>%
select(country, beer = beer_servings, spirit = spirit_servings, wine = wine_servings) %>%
filter(country %in% c("USA", "Seychelles", "Iceland", "Greece"))
subset_drinks
## # 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
beer <- subset_drinks %>%
select (country, serving = beer)
head(beer)
## # A tibble: 4 x 2
## country serving
## <chr> <int>
## 1 Greece 133
## 2 Iceland 233
## 3 Seychelles 157
## 4 USA 249
beer$drink_type <- "beer"
head(beer)
## # A tibble: 4 x 3
## country serving drink_type
## <chr> <int> <chr>
## 1 Greece 133 beer
## 2 Iceland 233 beer
## 3 Seychelles 157 beer
## 4 USA 249 beer
spirit <-subset_drinks %>%
select (country, serving = spirit)
head(spirit)
## # A tibble: 4 x 2
## country serving
## <chr> <int>
## 1 Greece 112
## 2 Iceland 61
## 3 Seychelles 25
## 4 USA 158
spirit$drink_type <- "spirit"
head(spirit)
## # A tibble: 4 x 3
## country serving drink_type
## <chr> <int> <chr>
## 1 Greece 112 spirit
## 2 Iceland 61 spirit
## 3 Seychelles 25 spirit
## 4 USA 158 spirit
wine <-subset_drinks %>%
select (country, serving = wine)
head(wine)
## # A tibble: 4 x 2
## country serving
## <chr> <int>
## 1 Greece 218
## 2 Iceland 78
## 3 Seychelles 51
## 4 USA 84
wine$drink_type <- "wine"
head(wine)
## # A tibble: 4 x 3
## country serving drink_type
## <chr> <int> <chr>
## 1 Greece 218 wine
## 2 Iceland 78 wine
## 3 Seychelles 51 wine
## 4 USA 84 wine
drink_plot_data <- rbind(beer, spirit, wine)
drink_plot_data
## # A tibble: 12 x 3
## country serving drink_type
## <chr> <int> <chr>
## 1 Greece 133 beer
## 2 Iceland 233 beer
## 3 Seychelles 157 beer
## 4 USA 249 beer
## 5 Greece 112 spirit
## 6 Iceland 61 spirit
## 7 Seychelles 25 spirit
## 8 USA 158 spirit
## 9 Greece 218 wine
## 10 Iceland 78 wine
## 11 Seychelles 51 wine
## 12 USA 84 wine
drink_plot_data$type <- drink_plot_data$drink_type
drink_plot_data$servings <- drink_plot_data$serving
3. Replicate the Plot
ggplot(data = drink_plot_data, aes(x = country, y = servings, fill=type)) +
geom_bar(stat="identity", position=position_dodge()) +
coord_flip()
