Graphics Reference
In-Depth Information
# These will have the same result: 2 rows and 4 cols
p + facet_wrap( ~ class, nrow = 2 )
p + facet_wrap( ~ class, ncol = 4 )
The choice of faceting direction depends on the kind of comparison you would like to encourage.
For example, if you want to compare heights of bars, it's useful to make the facets go horizont-
ally. If, on the other hand, you want to compare the horizontal distribution of histograms, it
makes sense to make the facets go vertically.
Sometimes both kinds of comparisons are important—there may not be a clear answer as to
which faceting direction is best. It may turn out that displaying the groups in a single plot by
mapping the grouping variable to an aesthetic like color works better than using facets. In these
situations, you'll have to rely on your judgment.
Using Facets with Different Axes
Problem
You want subplots with different ranges or items on their axes.
Solution
Set the scales to "free_x" , "free_y" , or "free" ( Figure 11-3 ):
# The base plot
p <- ggplot(mpg, aes(x = displ, y = hwy)) + geom_point()
# With free y scales
p + facet_grid(drv ~ cyl, scales = "free_y" )
# With free x and y scales
p + facet_grid(drv ~ cyl, scales = "free" )
Search WWH ::




Custom Search