In this example we will create a bar graph to show results from the past three decades of Nathan’s Hot Dog Eating Contest.
First we must read in the data. Lucky for us Nathan Yau has provided us with a nicely formatted database.
hotdogs <- read.csv("http://datasets.flowingdata.com/hot-dog-contest-winners.csv", sep=",", header=TRUE)
head(hotdogs)
## Year Winner Dogs.eaten Country New.record
## 1 1980 Paul Siederman & Joe Baldini 9.10 United States 0
## 2 1981 Thomas DeBerry 11.00 United States 0
## 3 1982 Steven Abrams 11.00 United States 0
## 4 1983 Luis Llamas 19.50 Mexico 0
## 5 1984 Birgit Felden 9.50 Germany 0
## 6 1985 Oscar Rodriguez 11.75 United States 0
We are interested in showing the number of HDBs eaten over time. To acheive this, we will plot the number of HDBs eaten by the winner for each year.
colors <- ifelse(hotdogs$New.record == 1, "darkred", "grey")
barplot(hotdogs$Dogs.eaten, names.arg = hotdogs$Year, col=colors, border=NA,
main = "Nathan's Hot Dog Eating Contest Results, 1980-2010",
xlab="Year", ylab="Hot dogs and buns (HDBs) eaten")
Now let’s try to create the same figure using ggplot2
.
library(ggplot2)
ggplot(hotdogs) + geom_bar(aes(x=Year, y=Dogs.eaten, fill=factor(New.record)), stat="identity") +
labs(title="Nathan's Hot Dog Eating Contest Results, 1980-2010", fill="New Record") + xlab("Year") +
ylab("Hot dogs and buns (HDBs) eaten")
Instead of viewing the number of HDBs eaten by the top contestant for each year we can view the top three.
# Read in the data, each row is a different contestant
hotdog_places <- read.csv("http://datasets.flowingdata.com/hot-dog-places.csv", sep=",", header=TRUE)
# Convert to matrix
hotdog_places <- as.matrix(hotdog_places)
# Rename the columns to correspond to the years 2000-2010
colnames(hotdog_places) <- lapply(2000:2010, as.character)
barplot(hotdog_places, border=NA, main="Hot Dog Eating Contest Results, 1980-2010", xlab="Year",
ylab="Hot dogs and buns (HDBs) eaten")
Throughout the 1990s, the winners ate 10-20 hot dogs and buns (HDBs) in about 15 minutes. However, in 2001, Takeru Kobayashi, a professional eater from Japan, obliterated the competition by eating 50 HDBs. That was more than twice the amount anyone in the world had eaten before him.