Skip to content

Instantly share code, notes, and snippets.

@Pakillo
Last active September 6, 2018 05:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Pakillo/c89ac159a2b4e4009de0717b7ff2c745 to your computer and use it in GitHub Desktop.
Save Pakillo/c89ac159a2b4e4009de0717b7ff2c745 to your computer and use it in GitHub Desktop.
ggplot_themes
### My favourite ggplot2 themes, always accessible ###
## just source this gist from any project ##
#####################################################################################
# Noam's default ggplot2 styling #####
# Source: https://github.com/noamross/noamtools/blob/master/R/theme_nr.R
theme_nr <-theme(text=element_text(family="Helvetica", size=14),
panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_line(colour="#ECECEC", size=0.5, linetype=1),
axis.ticks.y=element_blank(),
panel.background=element_blank(),
legend.title=element_blank(),
legend.key=element_rect(fill="white"),
legend.key.size=unit(1.5, "cm"),
legend.text=element_text(size=22),
axis.text=element_text(color="black",size=13),
axis.title=element_text(size=22),
title=element_text(size=22))
#######################################################################################
# Tim Lucas' themes #######
# Source: https://github.com/timcdlucas/ggplotThemes/blob/master/theme_tcdl.R
theme_tcdl <-theme(text = element_text(family = "sans", size = 12),
#panel.grid.major.x = element_line(colour = "#ECECEC", size = 0.3, linetype = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
#panel.grid.major.y = element_line(colour = "#ECECEC", size = 0.3, linetype = 1),
#axis.ticks.y = element_blank(),
panel.background = element_blank(),
#legend.title = element_blank(),
legend.title = element_text(colour="gray50", size = 12),
legend.key = element_rect(fill = "white"),
legend.key.size = unit(1.2, "lines"),
legend.text = element_text(size = 12, colour = "#8B8B8B"),
axis.text = element_text(color = "black", size = 16, family = "sans"),
axis.title = element_text(size = 22),
axis.title.y = element_text(margin=margin(0,15,0,0)),
axis.title.x = element_text(margin=margin(10,0,0,0)),
title = element_text(size = 22),
panel.border = element_blank(),
axis.line = element_line(colour = "black"),
plot.margin = unit(c(0.3,0.1,1,1.3), "lines"))
theme_pres <- theme(text = element_text(family = "Helvetica", size = 12),
panel.grid.major.x = element_line(colour = "#ECECEC", size = 0.3, linetype = 1),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(colour = "#ECECEC", size = 0.3, linetype = 1),
legend.title = element_text(size = 16, colour = "#8B8B8B"),
legend.key = element_rect(fill = "transparent", colour = NA),
legend.background = element_rect(fill = "transparent", colour = NA),
legend.key.size = unit(1.2, "lines"),
legend.text = element_text(size = 18, colour = "#8B8B8B"),
axis.text = element_text(color = "darkgrey", size = 15, family = "Helvetica"),
axis.title = element_text(size = 25),
axis.title.y = element_text(vjust = 2.5),
axis.title.x = element_text(vjust = -1),
title = element_text(size = 22),
panel.border = element_blank(),
axis.line = element_line(colour = "darkgrey"),
panel.background = element_rect(fill = "transparent", colour = NA),
plot.background = element_rect(colour = NA, fill = 'transparent'),
plot.margin = unit(c(0.3,0.1,1,1.3), "lines")
)
##################################################################################################
## Koundy's theme ####
# Source: https://github.com/koundy/ggplot_theme_Publication/blob/master/R/ggplot_theme_Publication.R
theme_Publication <- function(base_size=14, base_family="Helvetica") {
library(grid)
library(ggthemes)
(theme_foundation(base_size=base_size, base_family=base_family)
+ theme(plot.title = element_text(face = "bold",
size = rel(1.2), hjust = 0.5),
text = element_text(),
panel.background = element_rect(colour = NA),
plot.background = element_rect(colour = NA),
panel.border = element_rect(colour = NA),
axis.title = element_text(face = "bold",size = rel(1)),
axis.title.y = element_text(angle=90,vjust =2),
axis.title.x = element_text(vjust = -0.2),
axis.text = element_text(),
axis.line = element_line(colour="black"),
axis.ticks = element_line(),
panel.grid.major = element_line(colour="#f0f0f0"),
panel.grid.minor = element_blank(),
legend.key = element_rect(colour = NA),
legend.position = "bottom",
legend.direction = "horizontal",
legend.key.size= unit(0.2, "cm"),
legend.margin = unit(0, "cm"),
legend.title = element_text(face="italic"),
plot.margin=unit(c(10,5,5,5),"mm"),
strip.background=element_rect(colour="#f0f0f0",fill="#f0f0f0"),
strip.text = element_text(face="bold")
))
}
scale_fill_Publication <- function(...){
library(scales)
discrete_scale("fill","Publication",manual_pal(values = c("#386cb0","#fdb462","#7fc97f","#ef3b2c","#662506","#a6cee3","#fb9a99","#984ea3","#ffff33")), ...)
}
scale_colour_Publication <- function(...){
library(scales)
discrete_scale("colour","Publication",manual_pal(values = c("#386cb0","#fdb462","#7fc97f","#ef3b2c","#662506","#a6cee3","#fb9a99","#984ea3","#ffff33")), ...)
}
###########################################################################################
##' A theme for plotting slopegraphs
##'
##' @param base_size a numeric giving the base font size
##' @param base_family a string giving the base font family
##' @export
##' @importFrom grid unit
theme_slopegraph <- function (base_size = 18, base_family = "") {
theme(axis.line = element_blank(),
axis.text = element_text(colour="darkgrey"),
axis.text.x = element_text(size = rel(2), lineheight = 0.9, vjust = 0),
#axis.text.y = element_text(size=rel(0.8)),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.length = unit(0, "lines"),
axis.ticks.margin = unit(0, "lines"),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(0.25, "lines"),
strip.background = element_blank(),
strip.text.x = element_text(size = rel(0.8)),
strip.text.y = element_blank(),
plot.background = element_blank(),
plot.title = element_text(size = rel(1)),
plot.margin = unit(c(1, 0.5, 0.5, 0.5), "lines"),
complete=FALSE,
legend.position = "none")
}
#################################################################################
#### From cowplot package #####
## https://github.com/wilkelab/cowplot/blob/master/R/themes.R
# *************************************************
# Themes
# *************************************************
#' Create the default cowplot theme
#'
#' After loading the cowplot package, this theme will be the default
#' for all graphs made with ggplot2.
#'
#' @param font_size Overall font size. Default is 14.
#' @param font_family Default font family.
#' @param line_size Default line size.
#' @return The theme.
#' @examples
#' qplot(1:10, (1:10)^2) + theme_cowplot(font_size = 15)
#' @export
theme_cowplot <- function(font_size = 14, font_family = "", line_size = .5) {
half_line <- font_size / 2
small_rel <- 0.857
small_size <- small_rel * font_size
theme_grey(base_size = font_size, base_family = font_family) %+replace%
theme(
rect = element_rect(fill = "transparent", colour = NA, color = NA, size = 0, linetype = 0),
text = element_text(family = font_family, face = "plain", colour = "black",
size = font_size, hjust = 0.5, vjust = 0.5, angle = 0, lineheight = .9,
margin = ggplot2::margin(), debug = FALSE),
axis.text = element_text(colour = "black", size = small_size),
#axis.title = element_text(face = "bold"),
axis.text.x = element_text(margin = ggplot2::margin(t = small_size / 4), vjust = 1),
axis.text.y = element_text(margin = ggplot2::margin(r = small_size / 4), hjust = 1),
axis.title.x = element_text(
margin = ggplot2::margin(t = small_size / 2, b = small_size / 4)
),
axis.title.y = element_text(
angle = 90,
margin = ggplot2::margin(r = small_size / 2, l = small_size / 4),
),
axis.ticks = element_line(colour = "black", size = line_size),
axis.line.x = element_line(colour = "black", size = line_size),
axis.line.y = element_line(colour = "black", size = line_size),
legend.key = element_blank(),
legend.margin = grid::unit(0.1, "cm"),
legend.key.size = grid::unit(1, "lines"),
legend.text = element_text(size = rel(small_rel)),
# legend.position = c(-0.03, 1.05),
# legend.justification = c("left", "top"),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text = element_text(size = rel(small_rel)),
strip.background = element_rect(fill = "grey80", colour = "grey50", size = 0),
plot.background = element_blank(),
plot.title = element_text(face = "bold",
size = font_size,
margin = ggplot2::margin(b = half_line))
)
}
#' Create a completely empty theme
#'
#' The theme created by this function shows nothing but the plot panel. Unfortunately,
#' due to ggplot2 peculiarities, a little bit of padding remains to the left and bottom of
#' the panel. This can be removed by adding \code{labs(x = NULL, y = NULL)} to the plot, see examples.
#' @param base_size Overall font size. Default is 14.
#' @param base_family Base font family.
#' @return The theme.
#' @examples
#' # plot with small amount of remaining padding
#' qplot(1:10, (1:10)^2) + theme_nothing()
#' # remaining padding removed
#' qplot(1:10, (1:10)^2) + theme_nothing() + labs(x = NULL, y = NULL)
#' @export
theme_nothing <- function(base_size = 12, base_family = ""){
theme_grey(base_size = base_size, base_family = base_family) %+replace%
theme(
rect = element_rect(fill = "transparent", colour = NA, color = NA, size = 0, linetype = 0),
line = element_blank(),
text = element_blank(),
title = element_blank(),
# to debug, uncomment next line
#plot.background = element_rect(colour = "blue", fill = "cyan"),
panel.background = element_blank(),
axis.ticks.length = grid::unit(0, "lines"),
legend.position = "none",
panel.margin = grid::unit(c(0, 0, 0, 0), "lines"),
plot.margin = grid::unit(c(0, 0, 0, 0), "lines")
)
}
#' Add/modify/remove the background grid in a ggplot2 plot
#'
#' This function provides a simple way to modify the background grid in ggplot2. It
#' doesn't do anything that can't be done just the same with \code{theme()}. However, it simplifies
#' creation of the most commonly needed variations.
#' @param major Specifies along which axes you would like to plot major grid lines. Options are "xy", "x",
#' "y", "only_minor" (disables major grid lines but allows you to switch on minor grid lines), "none".
#' @param minor Specifies along which axes you would like to plot minor grid lines. Options are "xy", "x",
#' "y", "none".
#' @param size.major Size of the major grid lines.
#' @param size.minor Size of the minor grid lines.
#' @param colour.major Color of the major grid lines.
#' @param colour.minor Color of the minor grid lines.
#' @export
background_grid <- function(major = c("xy", "x", "y", "only_minor", "none"),
minor = c("xy", "x", "y", "none"),
size.major = 0.2, size.minor = 0.5,
colour.major = "grey90", colour.minor = "grey98"){
if (major[1] == "none") return(theme(panel.grid = element_blank()))
t <- switch( major[1],
x = theme(panel.grid.major = element_line(colour = colour.major,
size = size.major),
panel.grid.major.y = element_blank()),
y = theme(panel.grid.major = element_line(colour = colour.major,
size = size.major),
panel.grid.major.x = element_blank()),
xy = theme(panel.grid.major = element_line(colour = colour.major,
size = size.major)),
yx = theme(panel.grid.major = element_line(colour = colour.major,
size = size.major)),
theme(panel.grid.major = element_blank()))
t + switch( minor[1],
x = theme(panel.grid.minor = element_line(colour = colour.minor,
size = size.minor),
panel.grid.minor.y = element_blank()),
y = theme(panel.grid.minor = element_line(colour = colour.minor,
size = size.minor),
panel.grid.minor.x = element_blank()),
xy = theme(panel.grid.minor = element_line(colour = colour.minor,
size = size.minor)),
yx = theme(panel.grid.minor = element_line(colour = colour.minor,
size = size.minor)),
theme(panel.grid.minor = element_blank()))
}
#' Add/remove the panel border in a ggplot2 plot
#'
#' This function provides a simple way to modify the panel border in ggplot2. It
#' doesn't do anything that can't be done just the same with \code{theme()}. However, it
#' saves some typing.
#' @param colour The color of the border.
#' @param size Size.
#' @param linetype Line type.
#' @param remove If \code{TRUE}, removes the current panel border.
#' @export
panel_border <- function(colour = 'gray80', size = 0.5, linetype = 1, remove = FALSE){
if (remove){
return(theme(panel.border = element_blank()))
}
theme(panel.border = element_rect(colour = colour, fill=NA, linetype = linetype, size = size))
}
################################################################################################
### Robert Wilson's publication theme: https://github.com/robertwilson190/ggplot2-theme
# Theme used to create journal ready figures easilys
#
# base_size Font size
# base_family Font used for everything, unless axis fonts etc. are defined
# lines_lwd = width of lines
# plot_grid = Do you want a grid?
# font_type Font type
# title_size = font size of title
# legend_size = font size of legend
# bg_col = background colour
# title_font = font use for title
# base_col = font colour
# horz_grid = Do you want a horizontal grid?
# bord_size = width of a rectangular border
# alpha_leg = opacity of the legend. 0 = totally transparent
# strip_bg = colour background for facets
# grid_thick = A multiplier to apply to the grid lines. 0.8 would reduce thickness by 20%
# grid_type = Grid type. Default is a solid line
# ticks_xy = Do you want ticks on the x or y axis? "x" = x-axis only, "y" = y-axis only, "xy" = both axes.
# grid_cols = Colour of the grid. 2 element vector. First element is major grid colour. If only one element, the first will be used for minor grid.
theme_agile <- function(base_size = 11, base_family = "Arial", lines_lwd = 0.50, plot_grid = TRUE, axis_font = base_family, title_size = base_size*1.2, legend_size = base_size,
bg_col = "white",title_font = base_family , base_col = "black", axis_lines = TRUE,
minor_grid = ifelse(plot_grid, TRUE, FALSE), vert_grid = ifelse(plot_grid, TRUE, FALSE), ticks_type = "outer", horz_grid = ifelse(plot_grid, TRUE, FALSE), alpha_leg = 0.1, bord_size = 0,
legend_bg = "white", strip_bg = "white", grid_thick = 1,
grid_type = "solid", ticks_xy = "xy", grid_cols = c("grey50", "grey70")){
theme_bw()+
ggplot2::theme(
plot.margin = grid::unit(c(1, 1, .5, .7), "cm"),
text = ggplot2::element_text(family = base_family, size = base_size),
axis.line = element_line(size = ifelse(axis_lines, grid::unit(lines_lwd, "mm"),0), color = "black"),
axis.ticks.length = grid::unit(ifelse(ticks_type == "outer", 0.15, -0.15), "cm"),
axis.ticks.x = element_line(size = ifelse(stringr::str_detect(ticks_xy, "x"), grid::unit(lines_lwd, "cm"),0), color = "black"),
axis.ticks.y = element_line(size = ifelse(stringr::str_detect(ticks_xy, "y"), grid::unit(lines_lwd, "cm") ,0), color = "black"),
axis.text.x = ggplot2::element_text(size = base_size, colour = base_col , family = axis_font,margin=margin(ifelse(ticks_type == "inner", 11, 5),5,10,5,"pt")),
axis.text.y = ggplot2::element_text(size = base_size, colour = base_col , family = axis_font, margin=margin(5,ifelse(ticks_type == "inner", 11, 5),10,5,"pt")),
axis.title.y = ggplot2::element_text(size = base_size, colour = base_col , vjust = 1.5, family = axis_font),
axis.title.x = ggplot2::element_text(size = base_size,colour = base_col ,vjust = -.5, family = axis_font),
panel.background = ggplot2::element_rect(fill = bg_col),
plot.background = ggplot2::element_rect(fill = bg_col),
panel.border = ggplot2::element_rect(colour = "black", fill=NA, size = bord_size),
panel.grid.major.x = ggplot2::element_line(linetype = grid_type,colour = ifelse(vert_grid, grid_cols[1],bg_col), size = ifelse(vert_grid,0.25 * grid_thick, 0)),
panel.grid.minor.x = ggplot2::element_line(linetype = grid_type,colour = ifelse(vert_grid, ifelse(minor_grid, grid_cols[2 - (length(grid_cols) == 1) ],bg_col),bg_col), size = ifelse(vert_grid,0.15* grid_thick, 0)),
panel.grid.major.y = ggplot2::element_line(linetype = grid_type,colour = ifelse(horz_grid, grid_cols[1],bg_col), size = ifelse(horz_grid,0.25* grid_thick, 0)),
panel.grid.minor.y = ggplot2::element_line(linetype = grid_type,colour = ifelse(horz_grid, ifelse(minor_grid, grid_cols[2 - (length(grid_cols) == 1) ],bg_col),bg_col), size = ifelse(horz_grid,0.15* grid_thick, 0)),
plot.title = ggplot2::element_text(face="bold",vjust = 2, colour = base_col , size = title_size, family = title_font),
legend.background = ggplot2::element_rect(fill = scales::alpha(legend_bg, alpha_leg)), legend.key = ggplot2::element_blank(),
legend.text = ggplot2::element_text(size = legend_size, family = base_family),
legend.title = element_blank(),
strip.background = ggplot2::element_rect(colour = strip_bg, fill = strip_bg),
strip.text.x = ggplot2::element_text(size = base_size + 1),
strip.text.y = ggplot2::element_text(size = base_size + 1)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment