List of Ggplot2 Theme Options

List of ggplot2 theme options?

The closest thing to a comprehensive list that I have been able to find is in the ggplot2 wiki on github. I think that most of the options are covered there with examples.

Update
If you would like to see the options that are in use on a current plot, you can use plot_theme(x) to see all of the options that are currently set for the plot named x. It will not be a comprehensive list, but should help if you want to change something like the font size for an axis label.

Update 2
With the transition to version 0.9.0 it's worth noting that the built in documentation has been dramatically improved, and the transition guide from 0.8.9 to 0.9.0 is also worth checking out.

Update 3
There is now a ggplot2 documentation website. Look at the documentation for theme for a complete list. Also, ?theme has a pretty complete list as of 0.9.3.

Update 4
There is now a ggthemes package that has some nice themes and scales to choose from. It might save you from having to create your own. See their github page for more information.

Can ggplot theme formatting be saved as an object?

You can make a theme object without any problems, e.g:

mytheme<-theme(panel.background=element_rect(colour="green"))

It is even easier, if this is your standard theme to type

old_theme<- theme_update(panel.background=element_rect(colour="green"))

In the former case you write:

ggplot(...)+mytheme

while in the latter, because your custom theme is now the standard theme, it is only necessary to type:

ggplot(...)

How to set themes globally for ggplot2?

What I do is to set

th <- theme()

at the top of my script and then include this to all ggplots. It needs to be added before any plot specific themes or it will overwrite them.

df <- data.frame(x = rnorm(100))
ggplot(df, aes(x = x)) +
geom_histogram() +
th +
theme(panel.grid.major = element_line(colour = "pink"))

At a later stage, you can then change th to a different theme

Edit

theme_set and related functions theme_replace and theme_update suggested by hrbrmstr in the comments are probably better solutions to this problem. They don't require existing code to be edited.

g <- ggplot(df, aes(x = x)) +
geom_histogram() +

g
old <- theme_set(theme_bw()) #capture current theme
g
theme_set(old) #reset theme to previous

Include guide() in ggplot2 theme

The + operation for ggplot objects and lists work such that every element of the list is added to the plot seperately. For this to work, you can just wrap the theme and guides in a list, which you can then add just like you would a regular theme.

A word of warning though, if you map a continuous variable to the guide, it will try to pick guide_legend() instead of guide_colorbar(), which is arguably more appropriate, if you use it in this way.

library(ggplot2)

pie_theme <- list(
theme(
text = element_text(face = "bold", size = 7, color = "black"),
plot.caption = element_text(hjust = 0, size = 6),
legend.position = "bottom"
),
guides(fill = guide_legend(ncol = 1))
)

ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +
geom_bar(width = 1) +
coord_polar(theta = "y") +
pie_theme

Sample Image

Created on 2020-12-23 by the reprex package (v0.3.0)

Set a theme and palette for all plots

One solution would be to write a custom wrapper:

ggcust <- function(...){
ggplot(...) +
theme_bw()
}

fill in all the theme options you need, then use it like this:

ggcust(data = mtcars, aes(x = mpg, y = cyl)) +
geom_point()

Sample Image

GGPlot2 Preset - Creating a function with certain ggplot2 aesthetics options

You can store things to be added to a plot in a list. You can then re-use this list by adding it to a plot.

library(ggplot2)
library(ggthemes)
library(magrittr)

common_options <- list(
scale_fill_viridis_d(name = "", option = "inferno", begin = 0.3, end = 0.7, direction = -1),
scale_shape_tableau(),
theme_economist(),
theme(plot.background = element_rect(fill = "white"),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
plot.margin = unit(c(1,1,1,1), "cm"))
)



df1 <- data.frame(name = c("name1","name2","name3","name4"),
variable = c("var1","var1","var2","var2"),
value = c(15,16,17,18))

df1 %>%
ggplot(aes(x = name, y = value, fill = variable)) +
geom_bar(stat = "identity", position = position_stack()) +
labs(title = "Plot Title",
subtitle = "month 1",
x="",
y="Count") +
common_options

Sample Image

Created on 2021-07-20 by the reprex package (v1.0.0)

Add item to legend by theme options

Not saying that this is a good idea, but you can add a nonsensical geom to force the adding of a guide:

d <- data.frame("name" = c("pippo","pluto","paperino"), 
"id" = c(1,2,3),
"count" = c(10,20,30),
"value"=c(0.01,0.02,0.05),
geneRatio=c(0.5,0.8,0.2),
type=c("KEGG","Reactome","Reactome")
)

library(ggplot2)

ggplot(data = d, aes(geneRatio,name,colour = pvalue)) +
geom_point(aes(size=count))+
geom_polygon(aes(geneRatio,name,fill = type)) +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways") +
scale_fill_manual(values = c('Reactome'='red', 'KEGG'='black')) +
theme(axis.text.y = element_text(color=d$type))

Sample Image

geom_polygon may not work with your actual data, and you may not find a suitable 'nonsensical' geom. I agree with @zx8754, a facet would be clearer:

ggplot(data = d, aes(geneRatio,name,colour = pvalue)) + 
geom_point(aes(size=count)) +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways") +
facet_grid(type ~ ., scales = 'free_y', switch = 'y')

Sample Image

Writing a custom function for ggplot: How to avoid overriding `theme()` settings when adding external themes

You could add an optional theme as an argument to the function. If you need to add a color scale, you can tack this on after the function runs, since the scale function won't override any theme elements.

library(tidyverse)
library(ggthemes)

plot_from_data <- function(data_input, x_col, y_col, my_theme=NULL) {

my_theme = list(my_theme)

p_barplot <- ggplot(data = data_input, aes(x = {{ x_col }}, y = {{ y_col }}, fill = as_factor({{ x_col}} ))) +
geom_col() +
labs(caption = "caption blah") +
my_theme +
theme(plot.title = element_text(hjust = 0.5, size = 14),
axis.text.x=element_text(angle = -60, hjust = 0),
axis.title.x = element_blank(),
legend.title = element_blank(),
panel.grid = element_blank(),
panel.grid.major=element_blank(),
plot.caption = element_text(hjust = 0, size = 8),
legend.position = "none")

return(p_barplot)

}

plot_from_data(iris, Species, Petal.Width)
plot_from_data(iris, Species, Petal.Width, theme_bw())
plot_from_data(iris, Species, Petal.Width, theme_economist()) +
scale_fill_economist()

Sample Image

You could even add the scale function in the my_theme argument if you enter the argument as a list:

plot_from_data(iris, Species, Petal.Width, 
list(theme_economist(), scale_fill_economist())

Another option (maybe overkill) would be to allow the user to optionally add the name of a fill scale as a string. For example:

  plot_from_data <- function(data_input, x_col, y_col, my_theme=NULL, my_fill_scale=NULL) {


my_theme = list(my_theme)

p_barplot <- ggplot(data = data_input, aes(x = {{ x_col }}, y = {{ y_col }}, fill = as_factor({{ x_col}} ))) +
geom_col() +
labs(caption = "caption blah") +
my_theme +
theme(plot.title = element_text(hjust = 0.5, size = 14),
axis.text.x=element_text(angle = -60, hjust = 0),
axis.title.x = element_blank(),
legend.title = element_blank(),
panel.grid = element_blank(),
panel.grid.major=element_blank(),
plot.caption = element_text(hjust = 0, size = 8),
legend.position = "none")

if(!is.null(my_fill_scale)) {
p_barplot = p_barplot +
match.fun(paste0("scale_fill_", my_fill_scale))()
}

return(p_barplot)

}

plot_from_data(iris, Species, Petal.Width, theme_economist())
plot_from_data(iris, Species, Petal.Width, theme_economist(), "economist")
plot_from_data(iris, Species, Petal.Width, theme_economist(), "fivethirtyeight")

Sample Image

Going even further, you could use the same trick to enter the theme as a string and also set up the function to automatically pick the corresponding scale, where appropriate:

plot_from_data <- function(data_input, x_col, y_col, my_theme=NULL, my_fill_scale=NULL) {

p <- ggplot(data = data_input, aes(x ={{ x_col }}, y={{ y_col }}, fill=as_factor({{ x_col}} ))) +
geom_bar(stat = "identity") +
labs(caption = "caption blah")

if(!is.null(my_theme)) {
p = p + match.fun(paste0("theme_", my_theme))()
}

p = p + theme(plot.title = element_text(hjust = 0.5, size = 14),
axis.text.x=element_text(angle = -60, hjust = 0),
axis.title.x = element_blank(),
legend.title = element_blank(),
panel.grid = element_blank(),
panel.grid.major=element_blank(),
plot.caption = element_text(hjust = 0, size = 8),
legend.position = "none")

if(!is.null(my_fill_scale)) {
p = p +
match.fun(paste0("scale_fill_", my_fill_scale))()
}

if(!is.null(my_theme)) {
if(my_theme %in% c("economist","fivethirtyeight","few") &
is.null(my_fill_scale)) {
p = p + match.fun(paste0("scale_fill_", my_theme))()
}
}

return(p)
}

plot_from_data(iris, Species, Petal.Width, "economist")
plot_from_data(iris, Species, Petal.Width, "economist", "viridis_d")
plot_from_data(iris, Species, Petal.Width, "economist", "few")
plot_from_data(iris, Species, Petal.Width, "fivethirtyeight")

Sample Image

Store custom ggplot styles in object

You can create a list of customizations and then apply that to each plot. For example:

customPlot = list(
theme(plot.margin = unit(c(1,1,2,2), "cm"),
axis.text.x = element_text(vjust=0.5, size=20),
plot.title=element_text(size=20, vjust=2),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.title.x = element_blank(), axis.title.y = element_blank(),
panel.background = element_rect(fill = "#D9D9D9")),
coord_cartesian(ylim = c(0, 1)),
scale_fill_manual(values=c("#05f2ae", "#17b0c4"))
)

ggplot(mdf, aes (x=group, y=value, fill = variable)) +
geom_bar(stat="identity", position="dodge", alpha = 0.8) +
geom_bar(stat="identity", position="dodge", color = "#A9A9A9", alpha = 0.8) +
geom_text(aes(x=group, y=value, ymax=value, label=value),
position=position_dodge(1), vjust=-1, size=12) +
customPlot


Related Topics



Leave a reply



Submit