install.packages("fivethirtyeight")
library(fivethirtyeight)
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
Rename column names
names(drinks)
## [1] "country" "beer_servings"
## [3] "spirit_servings" "wine_servings"
## [5] "total_litres_of_pure_alcohol"
names(drinks) <- c("country", "beer", "spirit", "wine", "total_litres")
Transform data from long to wide format
library(tidyr)
drinks_2 <- gather(drinks, type, servings, 2:4)
library(ggplot2)
ggplot(subset(drinks_2, country %in% c("USA", "Seychelles", "Iceland", "Greece")), aes(x = servings, y = country, fill = type)) +
geom_bar(position="dodge", stat="identity")