Changing the Default ggplot theme

R
dataviz
ggplot2
Author

Robert W. Walker

Published

January 25, 2023

One can globally set the theme in an RMarkdown or Quarto Markdown document with theme_set(). If run at the console, every subsequent ggplot invocation will default to that theme. If set inside an R/Qmd file, then it will apply to everything in the file. First, let me load ggplot.

library(ggplot2)

The cars data

I will use R’s internal cars dataset to illustrate.

?cars will detail the variables.

cars description

This is the data.

library(DT)
datatable(cars)

A plot with defaults

ggplot(cars) + aes(x=speed, y=dist) + geom_point()

Setting a default theme

Let me set the theme for this to theme_minimal.

theme_set(theme_minimal())

The Same Plot with a New Default Theme

ggplot(cars) + aes(x=speed, y=dist) + geom_point()

Implications

This is the primary way that one might brand the use of ggplot in documents. There is a common mantra that says, if you write the same code more than twice, it is probably best redone as a function. I have done this in a gist.

To be continued…