Insights Using ggplot2’s Facet Functions

Introduction

Facet functions from ggplot2 package helps to easily plot and compare multiple subsets. It is highly efficient for exploratory data analysis and helps in comparing patterns within different subset of data.
We will be learning about below 3 functions:

  • facet_null(): used for default single graph (No faceting)

  • facet_wrap(): used to wrap(arrange) graphs in 2 dimension(grid layout) form to save space.

  • facet_grid(): used to create a matrix of plots defined by row and column faceting(dividing plots by categories)

Let us begin with Installing and loading the required package

install.packages("ggplot2")
library(ggplot2)
library(dplyr)

A glance at the dataset

  Gender     Age Overall_Experience Rating
1   Male 45 - 54          Excellent     10
2 Female 18 - 24               Good      7
3 Female 25 - 34               Good      8
4 Female 45 - 54               Good     10

facet_null()

  • Syntax: Default setting, no specific function call needed.
    ggplot(data, aes(x, y)) + geom_point()

    Here, we will Visualize the overall rating distribution without faceting
ggplot(data, aes(x = Age, y = Rating, color = Gender)) +
  geom_point() +
  facet_null() +
  labs(title = "Ratings by Age without Faceting",
       x = "Age Group",
       y = "Rating") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

facet_wrap()

  • Syntax: Facet by one variable, flexible grid layout
    ggplot(data, aes(x, y)) + geom_point() + facet_wrap(~ var)

    Here, we will compare ratings across different Overall experience levels.
ggplot(data, aes(x = Age, y = Rating, color = Gender)) +
  geom_point() +                        # for plotting Age vs. Rating, colored by Gender.
  facet_wrap(~ Overall_Experience, ncol = 2) +    # Creates a separate panel for each Overall_Experience
  labs(title = "Ratings by Age and Overall Experience",
       x = "Age Group",
       y = "Rating") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Note:
  • nrow and ncol: to specify number of rows and columns for plotting the graphs

facet_grid()

  • Syntax: Facet by one or two variables, strict grid layout.
    ggplot(data, aes(x, y)) + geom_point() + facet_grid(rows~ cols)

    Here, will try to analyze how ratings differ by gender and overall experience level.
ggplot(data, aes(x = Age, y = Rating, color = Gender)) +
  geom_point() +                            # for plotting Age vs. Rating, colored by Gender.
  facet_grid(Gender ~ Overall_Experience, scales = "free_x") + # creates a grid. Rows: Gender, Columns: Overall_Experience
  labs(title = "Ratings by Age, Gender, and Overall Experience",
       x = "Age Group",
       y = "Rating") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Note:
  • scales = “free_x” ,“free_y” or “free”: is used to have a separate x ,y or both xy scales for each sub graph. In the above example, the x-axis varies across subplots based on the data in each subset.

Conclusion:

  • Divides the data into smaller, easier-to-manage visualisations, reducing clutter in a single plot.

  • Helps in identifying patterns and trends that might not be visible in a combined plot.