facet_grid() and facet_wrap() both produce faceted plots where the labels of the categorical variables are displayed on top and/or to the right by default. The axes and their title are at the bottom and to the left, as usual. This is illustrated with the plot below (which code is stored in the object baseplot for later use):

While axis titles, ticks, scales, etc are editable in the same way as a normal plot (see here), the labels may be edited via a series of specific arguments in the function theme(). These arguments and options are described below.

Background color

Use theme(strip.background = element_rect()) to edit the box of the labels. This includes background color and frame:

baseplot +
  theme(strip.background = element_rect(fill="lightblue", size=1, color="darkblue"))

Have a look at this page or this tool to find the code/name for the colors you want to use with fill= or color=.

Label text

Use theme(strip.text = element_text()) to change the look of the text in ALL the labels. Alternatively use strip.text.x = or strip.text.y = to affect specifically the horizontal or the vertical labels:

baseplot +    # left plot, ALL labels are treated the same way
  theme(strip.text = element_text(size=12, face="bold", color="darkblue"))

baseplot +    # right plot, horizontal or vertical labels are treated independently
  theme(strip.text.x = element_text(size=12, face="italic", color="darkblue")) +
  theme(strip.text.y = element_text(size=13, face="bold", color="red")))

Note that the names of the labels are directly taken from the variables in the dataframe and displayed without further modification in the plot. If you wish to modify these names without changing the dataframe itself, here is a quick procedure:

  1. build a vector (called for example new_labels) to link the old names to the new names with =. In the example below, we wish to replace the labels of the variable category used to create the columns of the matrix. The old names “1”, “2”, “3” and “4” will be replaced by “cat.1”, “cat.2”, “cat.3” and “cat.4”:
new_labels <- c("1" = "cat.1", "2" = "cat.2", "3" = "cat.3", "4" = "cat.4")
  1. use the function labeller = labeller() with the variable to modify (here category=) followed by the vector:
ggplot(df, aes(group, values)) +
  geom_boxplot() +
  facet_grid(class ~ category, labeller = labeller(category = new_labels))

NB: this works only for the variables which are used to create the matrix with facet_grid() or facet_wrap(), not the variable used on the X-axis.

Panel margins

Use theme(panel.spacing = unit()) to change the spacing between ALL the labels (and the corresponding panels). Alternatively use panel.spacing.x = or panel.spacing.y = to only affect spacing between the horizontal or the vertical labels (and corresponding panels):

baseplot +    # left plot, ALL margins are set to 1 centimeter
  theme(panel.spacing = unit(1, "cm"))

baseplot +    # right plot, X and Y margins are treated independently
  theme(panel.spacing.x = unit(.25, "cm")) +
  theme(panel.spacing.y = unit(.5, "cm"))