Files

159 lines
3.3 KiB
R

#
# Bar chart
#
# Load data
winners <- read.csv("data/hot-dog-contest-winners.csv")
head(winners)
# Default bar chart
barplot(winners$hotdogs)
# Year labels
barplot(winners$hotdogs, names.arg=winners$year)
# Axis labels
barplot(winners$hotdogs,
names.arg=winners$year,
border=NA,
space=.1,
xlab="Year",
ylab="Hot dogs and buns (HDB) eaten",
main="Contest Winners")
#
# Line chart
#
# Graphical parameter to make axis labels horizontal
par(las=1)
# Line chart with plot()
plot(winners$year, winners$hotdogs,
type="l",
xlab="",
ylab="Hot dogs and buns (HDB) eaten",
main="Nathan's Hot Dog Eating Contest, Winning Results, 1980-2023")
par(mfrow=c(3,2), las=1)
for (currtype in c("p", "l", "b", "c", "o", "n")) {
plot(winners$year, winners$hotdogs, type=currtype,
xlab="", ylab="",
main=paste0("type='", currtype, "'"))
}
# Text
text(2001, 50,
"In 2001, Takeru Kobayashi nearly doubled the previous record with 50 HDBs.",
pos=2)
# Text with line breaks
text(2001, 48,
"In 2001, Takeru Kobayashi
nearly doubled the previous
record with 50 HDBs.",
pos=2)
# Find documentation
?text
# Wrapped text
anno <- "In 2001, Takeru Kobayashi nearly doubled the previous record with 50 HDBs."
text(2001, 48,
paste(strwrap(anno, 30), collapse="\n"),
pos=2)
#
# Dot plot
#
par(las=1)
plot(winners$year, winners$hotdogs,
type="p",
bty="n",
xlab="",
ylab="Hot dogs and buns (HDB) eaten",
main="Nathan's Hot Dog Eating Contest, Winning Results, 1980-2023")
#
# Step chart
#
# Load data
postage <- read.csv("data/us-postage.csv")
# Summary
head(postage)
summary(postage)
# Plot as regular time series
par(las=1)
plot(postage$Year, postage$Price, type="l",
xlab="Year", ylab="Postage Rate (Dollars)",
main="US Postage Rates for Letters, First Ounce, 1863-2023")
# Default step chart
plot(postage$Year, postage$Price, type="s")
# Add labels
par(las=1)
plot(postage$Year, postage$Price, type="s",
xlab="Year", ylab="Postage Rate (Dollars)",
main="US Postage Rates for Letters, First Ounce, 1863-2023")
# Change limits ("zoom")
postagesub <- postage[postage$Year >= 1991,]
head(postagesub)
par(las=1)
plot(postagesub$Year, postagesub$Price, type="s",
xlab="Year", ylab="Postage Rate (Dollars)",
main="US Postage Rates for Letters, First Ounce, 1991-2023")
#
# Smoothing
#
# Load data.
# install.packages("babynames")
library(babynames)
nathan <- babynames[babynames$name == "Nathan" & babynames$sex == "M", ]
beatrice <- babynames[babynames$name == "Beatrice" & babynames$sex == "F",]
# Line chart, no smoothing
par(las=1, mar=c(4,5,3,2))
plot(nathan$year, nathan$prop,
type="l",
xlab="", ylab="Proportion")
lines(beatrice$year, beatrice$prop,
lwd=2)
# Splines
natcoords <- spline(nathan$year, nathan$prop)
beacoords <- spline(beatrice$year, beatrice$prop)
par(las=1, mar=c(4,5,3,2))
plot(natcoords,
col="#ac5c5a",
type="l",
xlab="", ylab="Proportion")
lines(beacoords, lwd=2, col="#176572")
natcoords <- spline(nathan$year, nathan$prop, n=20)
beacoords <- spline(beatrice$year, beatrice$prop, n=20)
par(las=1, mar=c(4,5,3,2))
plot(natcoords,
col="#ac5c5a",
type="l",
xlab="", ylab="Proportion")
lines(beacoords, lwd=2, col="#176572")