ggplot2 Tutorial

Create clear, publication-ready charts with ggplot2

Scatter plots, bar charts, histograms, boxplots and more — plus themes, labels and exporting for reports.

ggplot2 cheat sheet Get help with your graphs

ggplot2 Themes: Customize the Appearance of Plots

Updated

Themes control the non-data parts of a plot: backgrounds, grid lines, text, axis ticks, titles and legend placement. They help make charts consistent and publication-ready.

This lesson is part of the ggplot2 tutorial. It uses the built-in mpg dataset included with ggplot2, so you can run the examples after loading the package.

Apply a built-in theme

ggplot2 includes several ready-made themes. Try them on the same plot and choose the one that suits the data and publication context.

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()

p + theme_minimal()
p + theme_classic()
p + theme_bw()
p + theme_light()

Customize text

Use element_text() to change font size, face, colour, alignment and margins for titles, axis labels and tick labels.

p + theme(
  plot.title = element_text(
    size = 16, face = "bold",
    hjust = 0.5
  ),
  axis.title = element_text(size = 12),
  axis.text = element_text(size = 10)
)

Control grid lines and backgrounds

Use element_blank() to remove a theme component. You can also set panel and plot backgrounds independently.

p + theme(
  panel.grid.minor = element_blank(),
  panel.grid.major = element_line(
    colour = "grey90"
  ),
  panel.background = element_rect(
    fill = "white"
  )
)

Move the legend

Legend position can be set with theme(). Common choices are "top", "bottom", "left", "right" and "none".

ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  theme_minimal() +
  theme(legend.position = "bottom")

Create a reusable theme

Save your preferred settings in a function and add it to plots. This keeps a report or website visually consistent.

theme_report <- function() {
  theme_minimal(base_size = 12) +
    theme(
      plot.title = element_text(face = "bold"),
      legend.position = "bottom"
    )
}

p + theme_report()

Common mistakes and troubleshooting

  • Could not find function: run library(ggplot2) in the current R session. Install it first with install.packages("ggplot2") if needed.
  • Object not found: check spelling and confirm the column exists in the data frame. Use names(mpg) to inspect column names.
  • Unexpected legend: a variable mapped inside aes() creates a scale and usually a legend. A fixed setting belongs outside aes().
  • Plot does not appear: make sure the complete expression is evaluated. In a script or function, explicitly call print(p) when displaying a stored ggplot object.
  • Warning about removed rows: inspect missing or out-of-range values. Do not suppress the warning until you understand why observations were removed.

Next step: Continue with the next lesson using the navigation at the bottom of this page, or return to the ggplot2 tutorial overview.

ggplot2 FAQ

What does theme_minimal() do?

theme_minimal() applies a clean theme with minimal background decoration and no panel border.

How do I remove grid lines in ggplot2?

Use theme(panel.grid = element_blank()) to remove major and minor panel grid lines.

How do I change the font size in ggplot2?

Use theme(text = element_text(size = 12)) or target a component such as axis.title or plot.title.