In R, I plot with ggplot
. Inspired by others who have also made themes that look nicer than the default options provided in the package, I present my own theme (at the end).
# package contains ggplot
library(tidyverse)
Examples of default plots
I’ll plot the built-in mtcars
data, with some added titles and with modifications to the points (colors and shapes).
This default plot theme has one glaring issue, in my opinion: the gray background. It reduced contrast and obscures patterns. It can work, but even then it needs an all-gray plot to work, also requiring really dark points to see well. Here are two other default plot themes.
# theme_bw
p + theme_bw()
# theme_classic
p + theme_classic()
The classic choice isn’t bad; has a clean feel that says, “I’m ready for a look”. But making your own theme isn’t that hard, and the results can be more rewarding. Of course, packages like ggthemes
and hrbrthemes
1 exist, but again, it isn’t that hard to do yourself.
theme_gradcylinder
library(extrafont)
## Registering fonts with R
rc <- c("Roboto Condensed")
# My own ggplot2 theme
theme_set(
theme_minimal(base_family = rc,
base_size = 14) +
theme(
plot.background = NULL,
plot.margin = margin(t = 2, r = 2, b = 2, l = 2, unit = "pt"),
panel.grid = element_line(color = "#F1F1F1"),
panel.border = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.title.y = element_text(
hjust = 1,
margin = margin(t = 0, r = 10, b = 0, l = 0, unit = "pt")),
axis.title.x = element_text(
hjust = 0,
margin = margin(t = 10, r = 0, b = 0, l = 0, unit = "pt")),
axis.text = element_text(family = rc),
legend.title.align = 0,
legend.key.height = unit(x = 5, units = "mm"),
legend.justification = c(1, 1),
legend.position = c(1, 1)
)
)
p
I currently like the Roboto Condensed font (used with extrafont
). I like using faint lines behind the data, that help interested viewers extract data. I like aligning the axis titles to the top and left. This makes sense as most plots are read left-to-right.
My theme is clearly inspired by `hrbrthemes`, but my own modifications mean that I claim it as my own now. Plus I don’t have to load an extra package.↩︎