KT_1
KT_1

Reputation: 8494

Change size of axes title and labels in ggplot2

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.

Upvotes: 256

Views: 804776

Answers (6)

ORBITTING ARYABHATA
ORBITTING ARYABHATA

Reputation: 37

You can change the axis fonts and sizes using these commands, assuming that p is your command line:

  axis.title.x = element_text(size = 14),  # Change x-axis title size
  axis.title.y = element_text(size = 14),  # Change y-axis title size
  axis.text.x = element_text(size = 12),   # Change x-axis label size
  axis.text.y = element_text(size = 12)    # Change y-axis label size
) ````

Upvotes: 2

Alfredo Gonzalez
Alfredo Gonzalez

Reputation: 11

To adjust the axis titles individually use:

theme(axis.title = element_text(size = 20))

To adjust the axis labels individually use:

theme(axis.text = element_text(size = 20))

Upvotes: 1

Didzis Elferts
Didzis Elferts

Reputation: 98569

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.

Upvotes: 467

Rtist
Rtist

Reputation: 4205

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.

Upvotes: 50

InterestedInR
InterestedInR

Reputation: 195

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)))

Upvotes: 15

chunjiw
chunjiw

Reputation: 1201

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.

Upvotes: 84

Related Questions