In this tutorial, we will learn how to move the strip label title text in ggplot2’s facet* functions to the bottom. By default, facet_wrap() creates a box for each strip with a label at the top of the small multiple plot. In this post, we will show how to move the strip label to the bottom using ‘strip.position = “bottom”‘ argument to facet_wrap() function first and then placing the label at the correct bottom location using `strip.placement = “outside”`argument to theme() function.
Let us load the packages needed.
library(palmerpenguins) library(tidyverse) theme_set(theme_bw(16))
penguins |> head() # A tibble: 6 × 8 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g <fct> <fct> <dbl> <dbl> <int> <int> 1 Adelie Torgersen 39.1 18.7 181 3750 2 Adelie Torgersen 39.5 17.4 186 3800 3 Adelie Torgersen 40.3 18 195 3250 4 Adelie Torgersen NA NA NA NA 5 Adelie Torgersen 36.7 19.3 193 3450 6 Adelie Torgersen 39.3 20.6 190 3650 # ℹ 2 more variables: sex <fct>, year <int>
Here is how the default plot made with facet_wrap() in ggplot2 looks like. Each small plot has the strip label at the top in a grey box.
penguins |> drop_na() |> ggplot(aes(x=sex, y=flipper_length_mm, color=species))+ geom_boxplot()+ facet_wrap(~species)+ labs(title="Palmer Penguins Dataset")+ theme(legend.position="none") ggsave("default_facet_wrap_ggplot.png")
Move facet strip label from top to bottom using “strip.position”
Let us move the strip label from top to bottom by adding the argument “strip.position =
bottom” to the facet_wrap() function.
penguins |> drop_na() |> ggplot(aes(x=sex, y=flipper_length_mm, color=species))+ geom_boxplot()+ facet_wrap(~species, strip.position = "bottom")+ labs(title="Palmer Penguins Dataset")+ theme(legend.position="none") ggsave("Move_facet_strip_label_to_bottom_ggplot.png")
Move facet strip label to outside x-axis tick at bottom using “strip.placement”
Although we have move the strip label or the tile text to the facet to the bottom, it is not in the right place. We can see that the strip label is above the x-axis tick labels. Let us move the strip label to outside by adding the argument strip.placement = “outside” to the theme() function.
penguins |> drop_na() |> ggplot(aes(x=sex, y=flipper_length_mm, color=species))+ geom_boxplot()+ facet_wrap(~species, strip.position = "bottom")+ labs(title="Palmer Penguins Dataset")+ theme(legend.position="none", strip.placement = "outside") ggsave("Move_facet_strip_label_outside_x_axis_tick_at_bottom_ggplot.png")
Customize facet strip label at bottom by removing strip box
We can further customize by removing the grey box around the strip label.
penguins |> drop_na() |> ggplot(aes(x=sex, y=flipper_length_mm, color=species))+ geom_boxplot()+ facet_wrap(~species, strip.position = "bottom")+ labs(title="Palmer Penguins Dataset", x=NULL)+ theme(legend.position="none", strip.placement = "outside", strip.background = element_blank()) ggsave("remove_facet_strip_box_ggplot.png")