Add VT2-examples: all 6 chapters of 'Visualize This' (110 files) — data scraping, ETL, trends, categorical, multivariate, spatial, distribution analysis
This commit is contained in:
1 parent
088771a9cf
commit
b42b866a8c
110 files changed
+400895
No files matched your search
@@ -0,0 +1,158 @@
|
||||
#
|
||||
# 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")
|
||||
Reference in new issue
Block a user