Plotting Concepts
Bar Plot
Counting Categories
A bar plot often starts from category counts. table creates those counts, and barplot maps them to bar heights.
Program
Play the script to add extra no responses and watch the tallest bar change.
barplot_counts.R
Replay: real traced execution (multi-file project)
responses <- c("yes", "no", "yes", "maybe", "yes", "no")
extra_no <- 0
responses <- c(responses, rep("no", extra_no))
counts <- table(responses)
colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")
mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)
winner <- names(counts)[which.max(counts)]
cat(winner, "\n", sep = "")
responses <- c("yes", "no", "yes", "maybe", "yes", "no")
extra_no <- 2
responses <- c(responses, rep("no", extra_no))
counts <- table(responses)
colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")
mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)
winner <- names(counts)[which.max(counts)]
cat(winner, "\n", sep = "")
responses <- c("yes", "no", "yes", "maybe", "yes", "no")
extra_no <- 4
responses <- c(responses, rep("no", extra_no))
counts <- table(responses)
colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")
mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)
winner <- names(counts)[which.max(counts)]
cat(winner, "\n", sep = "")
responses ← yes, no, yes, maybe, yes, no
1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 0values this stepyes, no, yes, maybe, yes, noresponsesextra_no ← 0
1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 03responses <- c(responses, rep("no", extra_no))values this step0extra_noresponses ← 6 responses
2extra_no <- 03responses <- c(responses, rep("no", extra_no))4counts <- table(responses)values this step6 responsesresponses0extra_nocounts ← 3 categories
3responses <- c(responses, rep("no", extra_no))4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")values this step3 categoriescountscolors ← maybe=gold, no=tomato, yes=seagreen
4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)values this stepmaybe=gold, no=tomato, yes=seagreencolorsmids ← 3 bar positions
5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)7winner <- names(counts)[which.max(counts)]values this step3 bar positionsmidsmaybe 1, no 2, yes 3countswinner ← yes
6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")values this stepyeswinnermaybe 1, no 2, yes 3countscat(winner, " ", sep = "")
7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")outputyesvalues this stepyeswinner
responses ← yes, no, yes, maybe, yes, no
1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 2values this stepyes, no, yes, maybe, yes, noresponsesextra_no ← 2
1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 23responses <- c(responses, rep("no", extra_no))values this step2extra_noresponses ← 8 responses
2extra_no <- 23responses <- c(responses, rep("no", extra_no))4counts <- table(responses)values this step8 responsesresponses2extra_nocounts ← 3 categories
3responses <- c(responses, rep("no", extra_no))4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")values this step3 categoriescountscolors ← maybe=gold, no=tomato, yes=seagreen
4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)values this stepmaybe=gold, no=tomato, yes=seagreencolorsmids ← 3 bar positions
5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)7winner <- names(counts)[which.max(counts)]values this step3 bar positionsmidsmaybe 1, no 4, yes 3countswinner ← no
6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")values this stepnowinnermaybe 1, no 4, yes 3countscat(winner, " ", sep = "")
7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")outputnovalues this stepnowinner
responses ← yes, no, yes, maybe, yes, no
1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 4values this stepyes, no, yes, maybe, yes, noresponsesextra_no ← 4
1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 43responses <- c(responses, rep("no", extra_no))values this step4extra_noresponses ← 10 responses
2extra_no <- 43responses <- c(responses, rep("no", extra_no))4counts <- table(responses)values this step10 responsesresponses4extra_nocounts ← 3 categories
3responses <- c(responses, rep("no", extra_no))4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")values this step3 categoriescountscolors ← maybe=gold, no=tomato, yes=seagreen
4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)values this stepmaybe=gold, no=tomato, yes=seagreencolorsmids ← 3 bar positions
5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)7winner <- names(counts)[which.max(counts)]values this step3 bar positionsmidsmaybe 1, no 6, yes 3countswinner ← no
6mids <- barplot(counts, col = colors[names(counts)], main = "Survey", plot = FALSE)7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")values this stepnowinnermaybe 1, no 6, yes 3countscat(winner, " ", sep = "")
7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")outputnovalues this stepnowinner
table
`table(responses)` counts how often each category appears.
barplot
`barplot(counts, plot = FALSE)` computes bar positions from category heights.
named vector
`colors[names(counts)]` lines colors up with the table's category names.