Module 10 assignment

Nathan’s Hot Dog Eating Contest


# Load data hotdogs <- read_csv("http://datasets.flowingdata.com/hot-dog-contest-winners.csv") head(hotdogs) # Load ggplot2 library(ggplot2) # Create color scheme for new records colors <- ifelse(hotdogs$New.record == 1, "darkred", "grey") # Base R plot 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")


A base R bar chart showing the number of hot dogs eaten each year with new records in dark red.

ggplot2 for the same data:

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")


A more polished ggplot2 bar chart that automatically includes a legend and modern styling.

Stacked Bar Plot:

hotdog_places <- as.matrix(hotdog_places) 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")


Part 2: Time Series Visualization Using the Economics Dataset

The economics dataset comes preloaded in ggplot2 and provides U.S. economic data over time. 

library(ggplot2) head(economics) # Add year column year <- function(x) as.POSIXlt(x)$year + 1900 economics$year <- year(economics$date) # Plot unemployment rate over time plot1 <- qplot(date, unemploy / pop, data = economics, geom = "line") plot1


A simple line plot showing unemployment rate trends over time.

We can also visualize the median unemployment duration and compare it to unemployment rate:

plot2 <- qplot(date, uempmed, data = economics, geom = "line") grid.arrange(plot1, plot2, ncol = 2)

And add color by year to make trends more visible


plot1 <- qplot(unemploy / pop, uempmed, data = economics, geom = c("point", "path")) plot2 <- qplot(unemploy / pop, uempmed, data = economics, geom = c("point", "path"), color = year) grid.arrange(plot1, plot2, ncol = 2)


Visualization plays a major role in understanding time series data. It allows us to:

  • Quickly identify trends, patterns, and seasonal variations.

  • Spot anomalies or turning points in data.

  • Compare multiple variables over time.

  • Communicate findings clearly to others.

Using ggplot2 makes it easier to produce clear, professional-quality time series plots. The ability to add layers, colors, and annotations helps us explore and interpret data more effectively.


http://datasets.flowingdata.com/hot-dog-contest-winners.csv

Comments

Popular posts from this blog

Final Project LIS4317

Module 11 assignment