Kenny
Kenny

Reputation: 3

title font size in ggplot2 does not change

I know this is a very basic question, but I have trouble changing font size of axis labels in ggplot2. I used the code like below:

a <- ggplot(data1,  aes(x=data2, y=data3)) + 
geom_hline(yintercept=c(1, -1)) + 
labs(x = data2, y = data3) + 
theme_bw() + 
theme(axis.text=element_text(size=10))

I tried to change the axis label size but they do not change.... Does anybody have suggestion?

Upvotes: 0

Views: 2720

Answers (2)

Mairi
Mairi

Reputation: 45

I was trying to solve the same problem, and realised that if you have specified e.g. theme_classic() or theme_bw, then the theme() argument has to come AFTER it!

So

g +
theme_classic() +
theme(axis.title = element_text(size = 12))

will work, but

g +
theme(axis.title = element_text(size = 12)) +
theme_classic()

will not!

Upvotes: 0

Mikey Harper
Mikey Harper

Reputation: 15429

Check out here how to alter labels in ggplot: http://www.sthda.com/english/wiki/ggplot2-title-main-axis-and-legend-titles

In the example below we use axis.title to change the size, colour and face of the text.

library(ggplot2)
ggplot(mtcars,  aes(x=mpg, y=disp)) +
  geom_point() + 
  theme_bw() + 
  theme(axis.title=element_text(size=10, colour = "red", face = "bold"))

enter image description here

Upvotes: 2

Related Questions