Home > OS >  theme for ggplot2 in r
theme for ggplot2 in r

Time:03-30

I found some code for a ggplot2 graph that really like:

ggplot(subset(diamonds, diamonds$color == "E"))  
  geom_point(aes(carat, price), size = 2)  
  scale_y_continuous("Price ($)")  
  scale_x_continuous("Carat")  
  ggtitle("Colourless E Diamonds")  
  theme(plot.title = element_text(family ="serif", color = "black", 
                                  face = "bold", size = 20),
        axis.title.x = element_text(family = "serif", color = "black", 
                                    size = 20),
        axis.title.y = element_text(family = "serif", color = "black", 
                                    size = 20),
        axis.text.x = element_text(size = 10))

I want to Create a custom function (my_theme) that has all the theme information of the code for the graph like (above), but this function should allow me to adjust the size of the plot title as well as the angle of the numbers on the x-axis. Finally I should be able to change all the titles (meaning plot, x-axis, and y-axis titles) from on colour to another. By default, it should have the plot title be size 20, x-axis angle be 0, and the colour of all the titles be black. and I write these codes but I received error. i need flexible code thats work for different code ,for example

ggplot(subset(diamonds, diamonds$color == "E"))  
  geom_point(aes(carat, price), size = 2)  
  scale_y_continuous("Price ($)")  
  scale_x_continuous("Carat")  
  ggtitle("Colourless E Diamonds")  
  my_theme(titles.colour = "grey", plot.title.size = 30, x.angle = -45) # should be able to take these arguements

and also this code

ggplot(subset(diamonds, diamonds$color == "E"))  
  geom_point(aes(carat, price), size = 2)  
  scale_y_continuous("Price ($)")  
  scale_x_continuous("Carat")  
  ggtitle("Colourless E Diamonds")  
  my_theme() #should have defaults

CodePudding user response:

First define a function with the elements that need to be changed as arguments. Set those arguments to defaults in the graph you really like. To change the defaults, pass them on to the function in its calls.

library(ggplot2)

my_theme <- function(plot_title_size = 30, colour = "black", size = 20, angle= 0){
  theme(plot.title = element_text(family ="serif", 
                                  face = "bold",
                                  colour = colour,
                                  size = plot_title_size),
        axis.title.x = element_text(family = "serif", 
                                    colour = colour, 
                                    size = size),
        axis.title.y = element_text(family = "serif",
                                    colour = colour, 
                                    size = size),
        axis.text.x = element_text(size = 10, angle = angle, vjust = 0.5)) 
}


ggplot(subset(diamonds, color == "E"), aes(carat, price))  
  geom_point(size = 2)  
  scale_y_continuous("Price ($)")  
  scale_x_continuous("Carat")  
  ggtitle("Colourless E Diamonds")  
  geom_smooth(formula = y ~ x,method=lm,se=FALSE)  
  my_theme()

ggplot(subset(diamonds, color == "E"), aes(carat, price))  
  geom_point(size = 2)  
  scale_y_continuous("Price ($)")  
  scale_x_continuous("Carat")  
  ggtitle("Colourless E Diamonds")  
  geom_smooth(formula = y ~ x,method=lm,se=FALSE)  
  my_theme(colour = "grey30", angle = 45)

Created on 2022-03-28 by the reprex package (v2.0.1)

  • Related