Skip to content

Instantly share code, notes, and snippets.

@jmclawson
Forked from jslefche/README.md
Last active November 15, 2023 01:45
Show Gist options
  • Save jmclawson/47c84f8f3d4e6e09edfd42ed112f4e03 to your computer and use it in GitHub Desktop.
Save jmclawson/47c84f8f3d4e6e09edfd42ed112f4e03 to your computer and use it in GitHub Desktop.
ggplot2: theme_black()

Black theme for ggplot2

This is an additional theme for ggplot2 that generates an inverse black and white color scheme.

Example

ggplot(mtcars, aes(wt, mpg)) + geom_point()
# Add theme_black()
ggplot(mtcars, aes(wt, mpg)) + geom_point(color = "white") + theme_black()
library(ggplot2)
library(gridExtra)
theme_black <- function(base_size = 12,
base_family = "",
black = "black") {
half_line <- base_size/2
theme_grey(
base_size = base_size,
base_family = base_family) %+replace%
theme(
text = element_text(family = base_family, face = "plain",
colour = "white", size = base_size, lineheight = 0.9,
hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(),
debug = FALSE),
# Specify axis options
axis.line = element_blank(),
axis.text.x = element_text(size = base_size*0.8, color = "white", lineheight = 0.9),
axis.text.y = element_text(size = base_size*0.8, color = "white", lineheight = 0.9),
axis.ticks = element_line(color = "white", size = 0.2),
axis.title.x = element_text(size = base_size, color = "white", margin = margin(0, 10, 0, 0)),
axis.title.y = element_text(size = base_size, color = "white", angle = 90, margin = margin(0, 10, 0, 0)),
axis.ticks.length = unit(0.3, "lines"),
# Specify legend options
legend.background = element_rect(color = NA, fill = black),
legend.key = element_rect(color = NA, fill = black),
legend.key.size = unit(1.2, "lines"),
legend.key.height = NULL,
legend.key.width = NULL,
legend.text = element_text(size = base_size*0.8, color = "white"),
legend.title = element_text(size = base_size*0.8, face = "bold", hjust = 0, color = "white"),
legend.position = "right",
legend.text.align = NULL,
legend.title.align = NULL,
legend.direction = "vertical",
legend.box = NULL,
# Specify panel options
panel.background = element_rect(fill = black, color = NA),
panel.border = element_rect(fill = NA, color = "white"),
panel.grid.major = element_line(color = "grey35"),
panel.grid.minor = element_line(color = "grey20"),
panel.spacing = unit(0.5, "lines"),
# Specify facetting options
strip.background = element_rect(fill = "grey30", color = "grey10"),
strip.text.x = element_text(size = base_size*0.8, color = "white"),
strip.text.y = element_text(size = base_size*0.8, color = "white",angle = -90),
# Specify plot options
plot.background = element_rect(color = black, fill = black),
plot.title = element_text(size = rel(1.2), hjust = 0,
vjust = 1, margin = margin(b = half_line),
color = "white"),
plot.subtitle = element_text(hjust = 0, vjust = 1, margin = margin(b = half_line), color = "white"),
plot.margin = unit(rep(1, 4), "lines")
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment