typestar

ggplot2 bars and facets in R

Categorical charts and small multiples.

library(ggplot2)

bars <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(am))) +
  geom_bar(position = "dodge") +
  scale_fill_brewer(palette = "Set2") +
  labs(x = "Cylinders", y = "Count", fill = "Manual")

histogram <- ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(bins = 10, fill = "steelblue") +
  facet_wrap(~ cyl, ncol = 2)

How it works

  1. geom_bar with position = "dodge" groups bars.
  2. scale_fill_brewer applies a palette.
  3. facet_wrap splits one plot into panels.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
325
Tokens
96
Three-star pace
100 tpm

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

Type this snippet

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

← Previous Next →