Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a customized theme similar to theme_bw in ggplot2?

Tags:

r

ggplot2

I have the following codes in ggplot2:

require("ggplot2")
df <- data.frame(x=factor(rep(1:2,5)), y=rnorm(10))

ggplot(df , aes(x,y)) + geom_point(size = 3) + 
theme(axis.text.x = element_text(angle = 40, hjust = 1, colour = "black", size=12),
      plot.title = element_text(size=16, face="bold", hjust=0.5)) +
labs(title = "Plot")

Now, I want to change the background color to theme_bw, but then the main title and the x-axis options will be changed back to default.

ggplot(df , aes(x,y)) + geom_point(size = 3) + 
theme(axis.text.x = element_text(angle = 40, hjust = 1, colour = "black", size=12),
      plot.title = element_text(size=16, face="bold", hjust=0.5)) +
labs(title = "Plot") + 
theme_bw()

So, how can I change the theme to be the same as theme_bw but without losing other options?

Thanks

like image 271
Alirsd Avatar asked Mar 08 '23 20:03

Alirsd


1 Answers

Here's the solution (make sure that theme_bw() is used before the theme() that you want to apply:

ggplot(df , aes(x,y)) + geom_point(size = 3) + 
theme_bw() + 
theme(axis.text.x = element_text(angle = 40, hjust = 1, colour = "black", size=12),
      plot.title = element_text(size=16, face="bold", hjust=0.5)) +
labs(title = "Plot")
like image 70
A Gore Avatar answered Mar 11 '23 14:03

A Gore