Menu

Guide10 min read

Mastering ggplot2 Themes: Customize Backgrounds, Fonts, and Gridlines

By Francesco Villasmunta
Mastering ggplot2 Themes: Customize Backgrounds, Fonts, and Gridlines

The visual appearance of a plot (everything that represents non-data details, such as font families, backgrounds, grid lines, and tick marks) is controlled by the theme system. In ggplot2, the theme() function provides granular control over all theme elements.

In This Guide

0.Live Code: Theme Design

1.Using Built-in Themes (theme_minimal)

2.Modifying Text Fonts and Sizes

3.Customizing Gridlines and Panels

4.Working with Legend Properties

0. Live Code: Theme Design

Polishing layout grids. Customize parameters using Python below, or upload your data to run R directly.

1. Using Built-in Themes

Start with a clean default base theme instead of the default grey. We recommend theme_minimal() or theme_classic():

R / ggplot2

ggplot(df, aes(x, y)) +
  geom_line() +
  theme_minimal(base_size = 11, base_family = "Arial")

2. Modifying Text Fonts and Sizes

Try it

Try it now: turn this method into your next figure

Apply the same approach to your own dataset and generate clean, publication-ready code and plots in minutes.

Open in Plotivy Analyze

Newsletter

Get a weekly Python plotting tip

One concise tip each week for cleaner, faster scientific figures. Built for researchers who publish.

No spam. Unsubscribe anytime.

Customize labels using `element_text()`. Modify titles, subtitles, axis text, and legends:

R / ggplot2

ggplot(df, aes(x, y)) +
  geom_line() +
  theme(
    plot.title = element_text(face = "bold", size = 12, hjust = 0.5),
    axis.title = element_text(face = "italic", size = 10),
    axis.text = element_text(size = 8, color = "gray30")
  )

3. Customizing Gridlines and Panels

Control grid lines using `element_line()` or remove them entirely using `element_blank()` to comply with strict journal formatting:

R / ggplot2

ggplot(df, aes(x, y)) +
  geom_line() +
  theme(
    panel.background = element_rect(fill = "white", color = "black"),
    panel.grid.major = element_line(size = 0.25, color = "gray90"),
    panel.grid.minor = element_blank() # remove minor grid lines
  )

4. Working with Legend Properties

Position the legend inside the plot boundary, shift it to the bottom, or remove borders:

R / ggplot2

ggplot(df, aes(x, y, color = group)) +
  geom_line() +
  theme(
    legend.position = "bottom",
    legend.title = element_blank(),
    legend.key = element_blank()
  )

Chart gallery

Explore related formats

Review visual formats.

Browse all chart types →
Scatter plot of height vs weight colored by gender with regression line
Statisticalmatplotlib, seaborn
From the chart galleryCorrelation analysis between metrics

Scatterplot

Displays values for two variables as points on a Cartesian coordinate system.

Sample code / prompt

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import pandas as pd

# Generate sample data
np.random.seed(42)
n_samples = 200
height = np.random.normal(170, 8, n_samples)
weight = height * 0.6 + np.random.normal(0, 8, n_samples) - 50
Multi-line graph showing temperature trends for 3 cities over a year
Time Seriesmatplotlib, seaborn
From the chart galleryStock price tracking over time

Line Graph

Displays data points connected by straight line segments to show trends over time.

Sample code / prompt

import matplotlib.pyplot as plt
import numpy as np

# Generate temperature data for 3 major US cities over 12 months
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
nyc = [30, 32, 40, 52, 65, 75, 82, 81, 74, 63, 50, 38]
miami = [65, 66, 70, 76, 82, 87, 90, 90, 87, 80, 72, 66]
chicago = [25, 27, 35, 48, 62, 72, 80, 79, 71, 60, 45, 32]

# Create figure with enhanced styling

Customize ggplot2 Themes Online

Upload your data and describe the design. Plotivy writes the ggplot2 code and executes it instantly.

Use R Analyzer
Tags:#R#ggplot2#theme#custom theme#theme_minimal

Found this helpful? Share it with your network.

FV
Francesco Villasmunta

Experimental Physicist & Photonics Researcher

Hands-on experience in silicon photonics, semiconductor fabrication (DRIE/ICP-RIE), optical simulation, and data-driven analysis. Built Plotivy to help researchers focus on discoveries instead of data struggles.

More about the author

Visualize your own data

Apply the techniques from this article to your own datasets. Upload CSV, Excel, or paste data directly.

Start Analyzing - Free