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
geom_barwithposition = "dodge"groups bars.scale_fill_brewerapplies a palette.facet_wrapsplits one plot into panels.
Keywords and builtins used here
aesfacet_wrapfactorgeom_bargeom_histogramggplotlabslibraryscale_fill_brewer
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.
Step 4 of 4 in Geometries, step 7 of 15 in ggplot2.