-
Book Overview & Buying
-
Table Of Contents
R for Data Science Cookbook
By :
When performing data exploration, it is essential to compare data across different groups. Faceting is a technique that enables the user to create graphs for subsets of data. In this recipe, we demonstrate how to use the facet function to create a chart for multiple subsets of data.
Ensure you have completed the previous steps by storing sample_sum in your R environment.
Please perform the following steps to create a chart for multiple subsets of data:
First, use the facet_wrap function to create multiple subplots by using the Province variable as the condition:
> g <- ggplot(data=sample_sum, mapping=aes(x=Year_Month, y=Total_Sales, colour = Province )) > g+geom_point(size = 5) + facet_wrap(~Province) + ggtitle('Create Multiple Subplots by Province')

Figure 19: Create multiple subplots by province
On the other hand, we can change the layout of the plot in a vertical direction if we set the number of columns to 1:
> g+geom_point() + facet_wrap...
Change the font size
Change margin width
Change background colour