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.

extra_no
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 = "")
  1. responses ← yes, no, yes, maybe, yes, no

    1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 0
    values this stepyes, no, yes, maybe, yes, noresponses
  2. extra_no ← 0

    1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 03responses <- c(responses, rep("no", extra_no))
    values this step0extra_no
  3. responses ← 6 responses

    2extra_no <- 03responses <- c(responses, rep("no", extra_no))4counts <- table(responses)
    values this step6 responsesresponses0extra_no
  4. counts ← 3 categories

    3responses <- c(responses, rep("no", extra_no))4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")
    values this step3 categoriescounts
  5. colors ← 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=seagreencolors
  6. mids ← 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 3counts
  7. winner ← 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 3counts
  8. cat(winner, " ", sep = "")

    7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")
    outputyes
    values this stepyeswinner
  1. responses ← yes, no, yes, maybe, yes, no

    1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 2
    values this stepyes, no, yes, maybe, yes, noresponses
  2. extra_no ← 2

    1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 23responses <- c(responses, rep("no", extra_no))
    values this step2extra_no
  3. responses ← 8 responses

    2extra_no <- 23responses <- c(responses, rep("no", extra_no))4counts <- table(responses)
    values this step8 responsesresponses2extra_no
  4. counts ← 3 categories

    3responses <- c(responses, rep("no", extra_no))4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")
    values this step3 categoriescounts
  5. colors ← 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=seagreencolors
  6. mids ← 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 3counts
  7. winner ← 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 3counts
  8. cat(winner, " ", sep = "")

    7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")
    outputno
    values this stepnowinner
  1. responses ← yes, no, yes, maybe, yes, no

    1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 4
    values this stepyes, no, yes, maybe, yes, noresponses
  2. extra_no ← 4

    1responses <- c("yes", "no", "yes", "maybe", "yes", "no")2extra_no <- 43responses <- c(responses, rep("no", extra_no))
    values this step4extra_no
  3. responses ← 10 responses

    2extra_no <- 43responses <- c(responses, rep("no", extra_no))4counts <- table(responses)
    values this step10 responsesresponses4extra_no
  4. counts ← 3 categories

    3responses <- c(responses, rep("no", extra_no))4counts <- table(responses)5colors <- c(maybe = "gold", no = "tomato", yes = "seagreen")
    values this step3 categoriescounts
  5. colors ← 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=seagreencolors
  6. mids ← 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 3counts
  7. winner ← 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 3counts
  8. cat(winner, " ", sep = "")

    7winner <- names(counts)[which.max(counts)]8cat(winner, "\n", sep = "")
    outputno
    values 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.