231

I have a really simple question, which I am struggling to find the answer to. I hoped someone here might be able to help me.

An example dataframe is presented below:

a <- c(1:10)
b <- c(10:1)
df <- data.frame(a,b)
library(ggplot2)
g = ggplot(data=df) + geom_point(aes(x=a, y=b)) +
  xlab("x axis")
g

I just want to learn how I change the text size of the axes titles and the axes labels.

4 Answers 4

432

You can change axis text and label size with arguments axis.text= and axis.title= in function theme(). If you need, for example, change only x axis title size, then use axis.title.x=.

g+theme(axis.text=element_text(size=12),
        axis.title=element_text(size=14,face="bold"))

There is good examples about setting of different theme() parameters in ggplot2 page.

2
  • 15
    There is also a very intuitive function rel() now. e.g. size = rel(2.0)
    – HongboZhu
    Oct 10, 2018 at 14:59
  • 6
    how would one change the labs(x = "x axis") text size without changing the value of the x axis ticks?
    – baxx
    Dec 23, 2018 at 14:36
75

I think a better way to do this is to change the base_size argument. It will increase the text sizes consistently.

g + theme_grey(base_size = 22)

As seen here.

2
  • 10
    If you already have other theme instances, this can cause conflicts. Feb 5, 2019 at 20:58
  • 2
    @AdamErickson Yes so you just add base_size = 22 to your theme of choice..
    – geotheory
    Oct 13, 2020 at 23:29
40

If you are creating many graphs, you could be tired of typing for each graph the lines of code controlling for the size of the titles and texts. What I typically do is creating an object (of class "theme" "gg") that defines the desired theme characteristics. You can do that at the beginning of your code.

My_Theme = theme(
  axis.title.x = element_text(size = 16),
  axis.text.x = element_text(size = 14),
  axis.title.y = element_text(size = 16))

Next, all you will have to do is adding My_Theme to your graphs.

g + My_Theme
if you have another graph, g1, just write:
g1 + My_Theme 
and so on.
1
  • This is the best answer, imo. There won't be many (if any) conflicts with pre-set themes.
    – riverotter
    Mar 28, 2021 at 15:25
11

To change the size of (almost) all text elements, in one place, and synchronously, rel() is quite efficient:
g+theme(text = element_text(size=rel(3.5))

You might want to tweak the number a bit, to get the optimum result. It sets both the horizontal and vertical axis labels and titles, and other text elements, on the same scale. One exception is faceted grids' titles which must be manually set to the same value, for example if both x and y facets are used in a graph:
theme(text = element_text(size=rel(3.5)), strip.text.x = element_text(size=rel(3.5)), strip.text.y = element_text(size=rel(3.5)))

1
  • 1
    This is a much more convenient way to modify all text sizes at once, rather than element by element.
    – lambertj
    Oct 24, 2022 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.