typestar

Bar charts in R

geom_col takes the heights you supply; geom_bar counts rows for you.

library(ggplot2)
library(dplyr)

steps <- tibble(
  lang = c("python", "rust", "c", "go", "r"),
  steps = c(364, 181, 165, 128, 138)
)

ranked <- ggplot(steps, aes(x = reorder(lang, steps), y = steps)) +
  geom_col(fill = "#7c5cff") +
  coord_flip() +
  labs(x = NULL, y = "steps")

counts <- tibble(lang = c("r", "r", "python"), stars = c(3L, 2L, 3L))
stacked <- ggplot(counts, aes(lang, fill = factor(stars))) +
  geom_bar(position = "dodge")

print(class(ranked)[1])
ggsave(tempfile(fileext = ".png"), stacked, width = 4, height = 3, dpi = 100)

How it works

  1. geom_col needs a y; geom_bar computes one.
  2. position decides stacked, dodged or filled.
  3. reorder sorts the categories by the value.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
535
Tokens
182
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 109 seconds.

Type this snippet

Step 1 of 4 in Geometries, step 4 of 15 in ggplot2.

← Previous Next →

Bar charts in other languages