Faceted plots are meant for use when you have a categorical variable. You can use the categorical variable to partition the data into different groups and the groups are then plotted in different panels called facets. Thus, you can use a facet grid to compare the distributions of two (or more) groups on the same variable.
Example 1:
# Make a faceted histogram of Thumb by Sex in the Fingers data frame
gf_histogram(~Thumb, data = Fingers) %>%
gf_facet_grid(Sex ~ .)
Example of output from running the code above:
Example 2:
# Make a faceted bar graph of RaceEthnic by Sex in the Fingers data frame
gf_bar(~RaceEthnic, data = Fingers) %>%
gf_facet_grid(Sex ~ .)
Example of output from running the code above:
# You can switch the facet variable to the other side of the tilde (~) to change whether the facets are vertical or horizontal
gf_bar(~RaceEthnic, data = Fingers) %>%
gf_facet_grid(. ~ Sex)
Example of output from running the code above:
Example 3:
# Make a faceted jitter plot of Thumb and Height by Sex in the Fingers data frame
gf_jitter(Thumb ~ Height, data = Fingers) %>%
gf_facet_grid(Sex ~ .)
Example of output from running the code above:
Related Articles
gf_facet_grid()
The gf_facet_grid() function will create separate plots for each group of a categorical variable. It can be chained onto plots such as gf_histogram(), gf_jitter(), and gf_bar(). Example 1: # Density histogram of Thumb faceted by Sex gf_dhistogram( ~ ...
gf_labs()
The gf_labs() function can be used to modify the labels of your plots. You can add a title for your plot, or modify the label for the x- or y-axis. Example: # Add a title and change the label for the x-axis gf_histogram(~Thumb, data = Fingers) %>% ...
gf_jitter()
The gf_jitter() function will generate a jitter plot. A jitter plot is a point plot (similar to a scatter plot, such as gf_point()) but the points are moved slightly ("jittered") so that they do not overlap as much. This can help make it easier to ...
gf_boxplot()
The gf_boxplot() function will generate a boxplot (also known as a box and whiskers plot). A boxplot splits the data into quartiles, where each whisker and each half of the box contains 25% of all the observations. They are helpful for visualizing ...
gf_density()
The gf_density() function will overlay a density plot onto a density histogram (i.e., gf_dhistogram(), not gf_histogram()). The density plot is a smoothed out version of the distribution. They can be helpful when you want to get a better idea of the ...