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
aesmaps columns to x, y, and color.geom_pointandgeom_smoothadd layers with+.labsandtheme_minimalfinish the presentation.
Keywords and builtins used here
aesfactorgeom_pointgeom_smoothggplotlabslibrarytheme_minimal
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.
Step 3 of 3 in The grammar, step 3 of 15 in ggplot2.