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:
neo committed 2026-06-24 14:49:15 +05:00
1 parent 088771a9cf
commit b42b866a8c
110 files changed
+400895

No files matched your search

+182
View File
@@ -0,0 +1,182 @@
#
# Timeline
#
# Load data
egots <- read.csv("data/EGOT-winners.csv")
# Dimensions of data frame
dim(egots)
# Column names
colnames(egots)
# Range of completion years
range(egots$completion_year)
# Blank plot
plot(NA,
xlim=range(egots$completion_year), ylim=c(0,2),
xlab="Completion Year", ylab="",
yaxt="n", bty="n", type="n")
# Baseline
abline(h=1)
# Points
points(egots$completion_year,
rep(1, dim(egots)[1]))
# e.g. 2001
egots[egots$completion_year == 2001, c("completion_year", "name")]
# Jitter
points(jitter(egots$completion_year),
jitter(rep(1, dim(egots)[1]), factor=2),
pch=21, bg="#efe2aa", cex=2)
# Last rows of data frame
tail(egots)
# Annotation
anntext <- paste0(egots$name[18], "\ncompletes first \nEGOT in ",
egots$completion_year[18])
text(egots$completion_year[18]-1,
1.5,
anntext,
pos=4)
#
# Dot plot
#
# Load data
egots <- read.csv("data/EGOT-winners.csv")
# Awards and colors
year_cols <- c("emmy_year", "grammy_year", "oscar_year", "tony_year")
award_colors <- c("#3F78E7", "#cb84cc", "#BDB63B", "#83c4b3")
# Blank plot
par(las=1)
plot(NA, xlim=c(1930, 2023),
ylim=c(0, dim(egots)[1]),
type="n", xlab="", ylab="")
# Points
for (i in 1:length(year_cols)) {
points(egots[,year_cols[i]], 1:dim(egots)[1], pch=19)
}
# With color
plot(NA, xlim=c(1930, 2023),
ylim=c(0, dim(egots)[1]),
type="n", xlab="", ylab="")
for (i in 1:length(year_cols)) {
points(egots[,year_cols[i]], 1:dim(egots)[1],
pch=21, cex=1.5,
col="black",
bg=award_colors[i])
}
# Connect
plot(NA, xlim=c(1930, 2023),
ylim=c(0, dim(egots)[1]),
type="n", xlab="", ylab="")
for (j in 1:dim(egots)[1]) {
endyrs <- range(egots[j, year_cols])
lines(endyrs, c(j, j))
}
for (i in 1:length(year_cols)) {
points(egots[,year_cols[i]], 1:dim(egots)[1],
pch=21, cex=1.5,
col="black",
bg=award_colors[i])
}
### With Labels
# Blank plot, margin
par(las=1, mar=c(4,12,2,2))
plot(NA, xlim=c(1930, 2023),
ylim=c(0, dim(egots)[1]),
yaxt="n", bty="n",
type="n", xlab="", ylab="")
# Axis and grid
axis(side=2, at=1:dim(egots)[1], labels = egots$name,
tick=FALSE)
grid(NULL, 0, lty=1, lwd=.5)
# Lines and points
for (j in 1:dim(egots)[1]) {
endyrs <- range(egots[j, year_cols])
lines(endyrs, c(j, j))
}
for (i in 1:length(year_cols)) {
points(egots[,year_cols[i]], 1:dim(egots)[1],
pch=21, cex=1.5,
col="black",
bg=award_colors[i])
}
#
# Bar chart, highlighted
#
# Load data
winners <- read.csv("data/hot-dog-contest-winners.csv")
# Bar chart
barplot(winners$hotdogs,
names.arg=winners$year,
border=NA,
space=.1,
xlab="Year",
ylab="Hot dogs and buns (HDB) eaten")
# Record years
head(winners)
# Bar chart, color
barplot(winners$hotdogs,
names.arg=winners$year,
col="lightblue",
border=NA,
space=.1,
xlab="Year",
ylab="Hot dogs and buns (HDB) eaten")
# Bar chart, some color
winners$barcolor <- NA
head(winners)
winners$barcolor[winners$record == 1] <- "#04b8c8"
winners$barcolor[winners$record != 1] <- "lightgray"
barplot(winners$hotdogs,
names.arg=winners$year,
col=winners$barcolor,
border=NA,
space=.1,
xlab="Year",
ylab="Hot dogs and buns (HDB) eaten")