Skip to main content

11  Bar charts

library(rtemis.draw)

draw_bar() draws one bar per category, either vertically or horizontally; several series sit side by side or stack.

draw_bar(
  names(table(penguins$species)),
  table(penguins$species)
)

A horizontal chart lists its categories from the top; order = "decreasing" puts the largest first:

islands <- table(penguins$island)
draw_bar(
  names(islands),
  islands,
  horizontal = TRUE,
  order = "decreasing",
  xlab = "Penguins observed"
)

11.1 Grouped and stacked series

A named list supplies one value vector per series. Each vector follows the category order in x; stack = TRUE combines the series within each category.

counts <- with(penguins, table(species, sex))
by_sex <- lapply(seq_len(ncol(counts)), function(i) as.numeric(counts[, i]))
names(by_sex) <- colnames(counts)
draw_bar(rownames(counts), by_sex, ylab = "Count")
draw_bar(rownames(counts), by_sex, stack = TRUE, horizontal = TRUE,
         xlab = "Count")

11.2 Configuration

setup_BarConfig() describes a bar chart by column names. Without y, each bar counts the rows of its category:

config <- setup_BarConfig(x = "island", group = "species", stack = TRUE,
                          ylab = "Penguins")
draw(config, data = penguins)
© 2026 E.D. Gennatas