The purpose of assignment 4 is to replicate a horizontal bar chart which shows the average alcohol consumption per capita (for those 15 and older), by beverage type for four countries: the USA, Seychelles, Iceland, and Greece.
The data for the horizontal bar chart will be drawn from the fivethirtyeight package. The unnecessary columns have been removed and the column headers have been renamed for ease later in the process.
library(fivethirtyeight)
library(dplyr)
library(tidyverse)
library(rsconnect) # I can't seem to publish to rpubs without loading this.
drinks1 <- drinks %>% select(country, beer = beer_servings, wine = wine_servings,spirit = spirit_servings)
head(drinks1)
## # A tibble: 6 x 4
## country beer wine spirit
## <chr> <int> <int> <int>
## 1 Afghanistan 0 0 0
## 2 Albania 89 54 132
## 3 Algeria 25 14 0
## 4 Andorra 245 312 138
## 5 Angola 217 45 57
## 6 Antigua & Barbuda 102 45 128
In order to tidy the data, it needs to be melted. Below, the data is converted from wide to long.
drinks2 <- gather(drinks1, "type", "servings", -country)
Below is the final bar chart, designed to mirror the original example given.
ggplot(data = drinks2 %>%
filter(country %in% c("USA", "Seychelles", "Iceland", "Greece")),
aes(x= country, y= servings, col = type, fill = type)) +
geom_bar(stat= "identity", position= "dodge") +
coord_flip()