In this post we will learn how to change axis tick label size in ggplot2 with multiple example. When we make a plot with ggplot2, plots have axis ticks- small vertical bars on both x and y axis, and tick labels – right below the ticks.
We can change the size of axis tick labels in ggplot2 using theme() function in combination with element_text() function.
library(tidyverse) library(palmerpenguins) theme_set(theme_bw(16))
Let us make a scatter plot with default axis tick label size. In this example below, the size of axis tick label is fixed by our theme_bw(16) setting upfront.
penguins |> ggplot(aes(body_mass_g, bill_length_mm, color=species))+ geom_point()+ labs(title="How to change axis tick label size in ggplot2") ggsave("how_to_change_axis_tick_label_size_ggplot2.png")
Change axis text label size using axis.text argument to theme() function
We can change axis text label size using theme() function by providing the argument axis.text to modify axis text label properties of both x and y axes. With size argument to element_text(), we can change the size of the axis tick labels.
penguins |> ggplot(aes(body_mass_g, bill_length_mm, color=species))+ geom_point()+ labs(title=stringr::str_wrap("Change axis tick label size in ggplot2 with element_text()", width=40)) + theme(axis.text = element_text(size=24)) ggsave("change_axis_tick_label_size_ggplot2_using_theme.png")
Change x-axis text label size using axis.text argument to theme() function
If needed, we can also selectively change either x or y axis text label size using the same approach used above. In the example, we are changing the size of x-axis tick labels alone using axis.text.x argument to theme() function.
penguins |> ggplot(aes(body_mass_g, bill_length_mm, color=species))+ geom_point()+ labs(title=stringr::str_wrap("Change x-axis tick label size in ggplot2 with element_text()", width=40)) + theme(axis.text.x= element_text(size=24)) ggsave("change_x_axis_tick_label_size_ggplot2_using_theme.png")
Change y-axis text label size using axis.text argument to theme() function
Here we show how to changing the size of y-axis tick labels alone using axis.text.y argument to theme() function.
penguins |> ggplot(aes(body_mass_g, bill_length_mm, color=species))+ geom_point()+ labs(title=stringr::str_wrap("Change y-axis tick label size in ggplot2 with element_text()", width=40)) + theme(axis.text.y = element_text(size=24)) ggsave("change_y_axis_tick_label_size_ggplot2_using_theme.png")