Faceting
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.
Getting ready
Ensure you have completed the previous steps by storing sample_sum
in your R environment.
How to do it…
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 theProvince
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')
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...