Themes in ggplot2

R Graphics
R Programming
Learn how to customize the appearance of plots using themes in ggplot2. This lecture covers essential techniques for enhancing the aesthetics of plots with themes in ggplot2.
Author

TERE

Published

June 21, 2024

Themes in ggplot2

Introduction

Themes in ggplot2 allow you to customize the appearance of your plots to make them more aesthetically pleasing and easier to interpret. By adjusting various theme elements, you can control the overall look and feel of your plots. In this lecture, we will learn how to apply and customize themes in ggplot2.

Key Concepts

1. What are Themes?

Themes in ggplot2 are collections of settings that control the non-data elements of a plot, such as titles, labels, legends, and background. They help in enhancing the visual appeal and readability of the plots.

2. Predefined Themes

ggplot2 provides several predefined themes that can be easily applied to any plot. These include:

  • theme_gray(): The default ggplot2 theme with a gray background.

  • theme_bw(): A black and white theme with a white background.

  • theme_minimal(): A minimalistic theme with no background annotations.

  • theme_classic(): A classic theme with a white background and black grid lines.

  • theme_void(): A completely empty theme.

3. Customizing Themes

You can customize individual theme elements to create a unique appearance for your plots. This includes adjusting the text size, color, font, and more.

Applying and Customizing Themes in ggplot2

1. Applying Predefined Themes

You can apply predefined themes to a plot using the corresponding theme function.

Installing and loading ggplot2


install.packages("ggplot2")
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
# Creating sample data

data <- data.frame(x = rnorm(100), y = rnorm(100))



# Basic scatter plot with the default theme

p <- ggplot(data, aes(x = x, y = y)) + geom_point() + labs(title = "Default Theme")

p

# Applying the theme_bw() theme

p + theme_bw() + labs(title = "Black and White Theme")

# Applying the theme_minimal() theme

p + theme_minimal() + labs(title = "Minimal Theme")

# Applying the theme_classic() theme

p + theme_classic() + labs(title = "Classic Theme")

# Applying the theme_void() theme

p + theme_void() + labs(title = "Void Theme")

2. Customizing Individual Theme Elements

You can customize individual theme elements using the theme() function.


# Customizing text elements

p + theme(

  plot.title = element_text(size = 20, face = "bold", color = "blue"),

  axis.title.x = element_text(size = 15, color = "darkred"),

  axis.title.y = element_text(size = 15, color = "darkred"),

  axis.text = element_text(size = 12, color = "black")

) + labs(title = "Customized Text Elements")
# Plot result

p + theme(

  plot.title = element_text(size = 20, face = "bold", color = "blue"),

  axis.title.x = element_text(size = 15, color = "darkred"),

  axis.title.y = element_text(size = 15, color = "darkred"),

  axis.text = element_text(size = 12, color = "black")

) + labs(title = "Customized Text Elements")

3. Customizing Background and Grid Lines

You can customize the background and grid lines to enhance the visual appeal of your plot.


# Customizing background and grid lines

p + theme(

  panel.background = element_rect(fill = "lightblue", color = "black"),

  panel.grid.major = element_line(size = 0.5, linetype = 'dashed', color = "gray"),

  panel.grid.minor = element_line(size = 0.25, linetype = 'dashed', color = "lightgray")

) + labs(title = "Customized Background and Grid Lines")
# Plot result

p + theme(

  panel.background = element_rect(fill = "lightblue", color = "black"),

  panel.grid.major = element_line(size = 0.5, linetype = 'dashed', color = "gray"),

  panel.grid.minor = element_line(size = 0.25, linetype = 'dashed', color = "lightgray")

) + labs(title = "Customized Background and Grid Lines")
Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.

4. Customizing Legends

You can customize the legend to make it more informative and visually appealing.

# Creating sample data with groups

data$group <- sample(c("Group 1", "Group 2"), 100, replace = TRUE)



# Customizing the legend

ggplot(data, aes(x = x, y = y, color = group)) +

  geom_point() +

  labs(title = "Customized Legend") +

  theme(

    legend.title = element_text(size = 15, face = "bold"),

    legend.text = element_text(size = 12),

    legend.position = "bottom",

    legend.background = element_rect(fill = "lightgray", color = "black")

  )

# Plot result

ggplot(data, aes(x = x, y = y, color = group)) +

  geom_point() +

  labs(title = "Customized Legend") +

  theme(

    legend.title = element_text(size = 15, face = "bold"),

    legend.text = element_text(size = 12),

    legend.position = "bottom",

    legend.background = element_rect(fill = "lightgray", color = "black")

  )

Example: Comprehensive Theme Customization

Here’s a comprehensive example of customizing a plot using themes in ggplot2.


# Creating sample data

data <- data.frame(x = rnorm(100), y = rnorm(100), group = sample(c("Group 1", "Group 2"), 100, replace = TRUE))



# Customizing various theme elements

ggplot(data, aes(x = x, y = y, color = group)) +

  geom_point() +

  labs(title = "Comprehensive Theme Customization", x = "X Axis", y = "Y Axis") +

  theme(

    plot.title = element_text(size = 20, face = "bold", color = "blue"),

    axis.title.x = element_text(size = 15, color = "darkred"),

    axis.title.y = element_text(size = 15, color = "darkred"),

    axis.text = element_text(size = 12, color = "black"),

    panel.background = element_rect(fill = "lightblue", color = "black"),

    panel.grid.major = element_line(size = 0.5, linetype = 'dashed', color = "gray"),

    panel.grid.minor = element_line(size = 0.25, linetype = 'dashed', color = "lightgray"),

    legend.title = element_text(size = 15, face = "bold"),

    legend.text = element_text(size = 12),

    legend.position = "bottom",

    legend.background = element_rect(fill = "lightgray", color = "black")

  )
# Plot result

ggplot(data, aes(x = x, y = y, color = group)) +

  geom_point() +

  labs(title = "Comprehensive Theme Customization", x = "X Axis", y = "Y Axis") +

  theme(

    plot.title = element_text(size = 20, face = "bold", color = "blue"),

    axis.title.x = element_text(size = 15, color = "darkred"),

    axis.title.y = element_text(size = 15, color = "darkred"),

    axis.text = element_text(size = 12, color = "black"),

    panel.background = element_rect(fill = "lightblue", color = "black"),

    panel.grid.major = element_line(size = 0.5, linetype = 'dashed', color = "gray"),

    panel.grid.minor = element_line(size = 0.25, linetype = 'dashed', color = "lightgray"),

    legend.title = element_text(size = 15, face = "bold"),

    legend.text = element_text(size = 12),

    legend.position = "bottom",

    legend.background = element_rect(fill = "lightgray", color = "black")

  )

Summary

In this lecture, we covered how to apply and customize themes in ggplot2. We explored various predefined themes and learned how to customize individual theme elements such as text, background, grid lines, and legends. Customizing themes is essential for enhancing the visual appeal and readability of your plots.