typestar

ggplot2 scatter plots in R

The grammar of graphics: data, aesthetics, layers.

library(ggplot2)

plot <- ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) +
  geom_point(size = 3, alpha = 0.8) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    title = "Weight versus efficiency",
    x = "Weight (1000 lbs)",
    y = "Miles per gallon",
    colour = "Cylinders"
  ) +
  theme_minimal()

How it works

  1. aes maps columns to x, y, and color.
  2. geom_point and geom_smooth add layers with +.
  3. labs and theme_minimal finish the presentation.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
295
Tokens
78
Three-star pace
95 tpm

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

Type this snippet

Step 3 of 3 in The grammar, step 3 of 15 in ggplot2.

← Previous Next →