I have a problem with storing my_theme for ggplot. I want to have the same theme for my ggplot and be flexible for different ggplot
# my plot is
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))
# my code is
my_theme <- function(size=size, color=colour, angel=angle){
theme(plot.title = element_text(family ="serif", color = titles.colour,
face = "bold", size =plot.title.size),
axis.title.x = element_text(family = "serif", color = titles.colour,
size = 20),
axis.title.y = element_text(family = "serif", color = titles.colour,
size = 20),
axis.text.x = element_text(size = 10,angel=x.angle))
}
but that doesn't work for my different plot
# my different plot code
# Code you should be able to run without changing any of 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(titles.colour = "grey", plot.title.size = 30, x.angle = -45) # should be able to take these arguements
# and this
# Code you should be able to run without changing any of 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
thanks for any help you can provide
CodePudding user response:
You are passing arguments to parameters that your function does not have, and using variables inside your function that do not exist.
Your function could have NULL
defaults (since ggplot uses fallback default values in this case). When you want to use the values passed into the function inside the function, you must use the names of the passed arguments exactly as they were in the function definition (you can't have size
in the function definition and use titles.size
inside the function, for example)
Your theme function could look like this:
my_theme <- function(size = NULL, color = NULL, angle = NULL) {
theme(plot.title = element_text(family ="serif", color = color,
face = "bold", size = size),
axis.title.x = element_text(family = "serif", color = color,
size = 20),
axis.title.y = element_text(family = "serif", color = color,
size = 20),
axis.text.x = element_text(size = 10, angle = angle))
}
When you use your function, it has to have exactly the same names as you put in the function definition
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(color = "grey", size = 30, angle = -45)
And the default will look like this:
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()
Created on 2022-03-29 by the reprex package (v2.0.1)