Graphics Reference
In-Depth Information
Making Multiple Histograms from Grouped Data
Problem
You want to make histograms of multiple groups of data.
Solution
Use geom_histogram() and use facets for each group, as shown in Figure 6-4 :
library(MASS) # For the data set
# Use smoke as the faceting variable
ggplot(birthwt, aes(x = bwt)) + geom_histogram(fill = "white" , colour = "black" ) +
facet_grid(smoke ~ . )
Figure 6-4. Left: two histograms with facets; right: with different facet labels
Discussion
To make these plots, the data must all be in one data frame, with one column containing a cat-
egorical variable used for grouping.
For this example, we used the birthwt data set. It contains data about birth weights and a num-
ber of risk factors for low birth weight:
Search WWH ::




Custom Search