Module 10. Time Series and Visualization
Visualization plays a big role in time series analysis because it helps us actually see how data changes over time instead of just looking at numbers. By plotting data, we can quickly spot patterns, trends, or seasonal behaviors that might not be obvious from raw values alone. For example, a line chart can show whether unemployment rates rise or fall during certain periods, helping us understand the bigger picture. Visuals also make it easier to detect sudden changes or outliers that could affect the analysis. Overall, visualization turns complex time-based data into something more intuitive and meaningful, making it a key first step in analyzing and interpreting time series data.
R code for future reference:
#Hot Dog eating contest
library(readr)
library(ggplot2)
# Load the dataset
hotdogs <- read_csv("http://datasets.flowingdata.com/hot-dog-contest-winners.csv")
#view
head(hotdogs)
# Set color for bars: dark red if new record, grey otherwise
colors <- ifelse(hotdogs$`New record` == 1, "darkred", "grey")
heights <- as.numeric(hotdogs$`Dogs eaten`)
# Create the bar plot
barplot(
heights,
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"
)
#ggplot2
ggplot(hotdogs, aes(x = Year, y = `Dogs eaten`, fill = factor(`New record`))) +
geom_col() + # same as geom_bar(stat = "identity")
labs(
title = "Nathan's Hot Dog Eating Contest Results, 1980-2010",
x = "Year",
y = "Hot dogs and buns (HDBs) eaten",
fill = "New Record"
)
#Stacked bar plot using R-base graphics
hotdogs_2000 <- subset(hotdogs, Year >= 2000 & Year <= 2010)
# Convert to matrix
hotdog_places <- as.matrix(hotdog_places)
colnames(hotdog_places) <- as.character(2000:2010)
#create plot
barplot(
hotdog_places,
border = NA,
main = "Hot Dog Eating Contest Results, 1980–2010",
xlab = "Year",
ylab = "Hot dogs and buns (HDBs) eaten",
col = gray.colors(3)
)
#economics data
library(ggplot2)
# Load the built-in dataset
head(economics)
# Extract the year from the date column
year <- function(x) as.POSIXlt(x)$year + 1900
economics$year <- year(economics$date)
head(economics)
# Base R plot equivalent of qplot(date, unemploy / pop, data = economics, geom = "line")
plot(economics$date, economics$unemploy / economics$pop,
type = "l",
main = "Unemployment as a Fraction of Population (1967–2015)",
xlab = "Date",
ylab = "unemploy/pop")
#unemployment graph
# Load data
data(economics)
# Create the year variable
year <- function(x) as.POSIXlt(x)$year + 1900
economics$year <- year(economics$date)
# Define x and y
x <- economics$unemploy / economics$pop
y <- economics$uempmed
# Create a color gradient for the years
pal <- colorRampPalette(c("#08306b", "#2171b5", "#6baed6", "#bdd7e7"))
n <- 100
brks <- seq(min(economics$year), max(economics$year), length.out = n)
col_index <- cut(economics$year, breaks = brks, include.lowest = TRUE, labels = FALSE)
cols <- pal(n)[col_index]
plot(x, y, type = "n",
xlab = "unemploy/pop",
ylab = "uempmed",
main = "Median Unemployment vs. Unemployment Rate (colored by Year)")
# Draw colored path and points
for (i in 1:(length(x) - 1)) {
segments(x[i], y[i], x[i + 1], y[i + 1], col = cols[i], lwd = 2)
}
points(x, y, pch = 16, col = cols)
# Add a simple legend for years
legend("topleft",
legend = c(1970, 1980, 1990, 2000, 2010),
title = "year",
pch = 16,
col = pal(5),
bty = "n")
Comments
Post a Comment