Themes in ggplot

In ggplot2, themes control the non-data elements of your plots, such as titles, labels, and backgrounds. Using themes, you can customize the appearance of your plots to match your desired style or branding.

Default Themes in ggplot2

ggplot2 comes with several built-in themes:

  • theme_gray(): The default theme.
  • theme_bw(): A theme with a white background and black gridlines.
  • theme_minimal(): A minimalistic theme with no background annotations.
  • theme_classic(): A classic theme with no gridlines and a white background.

Here’s how you can apply a default theme to a plot:

library(ggplot2)

data(mpg)
p <- ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  labs(title = "Fuel Efficiency",
       subtitle = "Engine Displacement vs. Highway Miles per Gallon",
       x = "Engine Displacement (L)",
       y = "Highway Miles per Gallon")

# Apply the default theme
p + theme_gray()

p + theme_classic()

p+theme_minimal()

Customizing Themes

You can customize themes by modifying existing themes or creating your own. This allows you to control the appearance of specific components of your plots.

Modifying Existing Themes

Here’s how you can modify elements of an existing theme:

custom_theme <- theme(
  plot.title = element_text(size = 20, face = "bold", color = "blue"),
  plot.subtitle = element_text(size = 14, face = "italic", color = "darkblue"),
  axis.title.x = element_text(size = 12, color = "purple"),
  axis.title.y = element_text(size = 12, color = "purple"),
  panel.background = element_rect(fill = "lightgray"),
  panel.grid.major = element_line(color = "white"),
  panel.grid.minor = element_line(color = "lightgray")
)

# Apply the custom theme
p + custom_theme

Creating a Reusable Custom Theme Function

To make your customizations reusable, you can create a theme function:

theme_custom <- function() {
  theme(
    plot.title = element_text(size = 20, face = "bold", color = "blue"),
    plot.subtitle = element_text(size = 14, face = "italic", color = "darkblue"),
    axis.title.x = element_text(size = 12, color = "purple"),
    axis.title.y = element_text(size = 12, color = "purple"),
    panel.background = element_rect(fill = "lightgray"),
    panel.grid.major = element_line(color = "white"),
    panel.grid.minor = element_line(color = "lightgray")
  )
}

# Apply the reusable custom theme function
p + theme_custom()

Using Predefined Themes for Economic Data

For consistency and professionalism, economists often use predefined themes. Here’s an example of a theme suitable for economic data presentations:

theme_economist <- function() {
  theme_minimal() +
    theme(
      plot.title = element_text(size = 18, face = "bold"),
      plot.subtitle = element_text(size = 14, face = "italic"),
      axis.title = element_text(size = 12),
      axis.text = element_text(size = 10),
      panel.grid.major = element_line(color = "gray90"),
      panel.grid.minor = element_line(color = "gray95"),
      panel.background = element_rect(fill = "white"),
      plot.background = element_rect(fill = "white", color = NA),
      legend.background = element_rect(fill = "white"),
      legend.key = element_rect(fill = "white")
    )
}

# Apply the economist theme
p + theme_economist()

Combining Themes with Other ggplot2 Elements

You can combine themes with other ggplot2 elements to create more complex and informative plots:

p + 
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  theme_custom() +
  theme(legend.position = "bottom")
`geom_smooth()` using formula = 'y ~ x'

Practical Examples for Economists

Example 1: GDP Growth Rate

library(ggplot2)
library(lubridate)

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
gdp_data <- data.frame(
  year = 2010:2020,
  gdp_growth = c(2.5, 1.8, 2.2, 1.6, 2.9, 2.4, 1.6, 2.2, 3.0, 2.3, -3.5)
)

# Convert year to Date
gdp_data$year <- as.Date(as.character(gdp_data$year), format="%Y")

gdp_plot <- ggplot(gdp_data, aes(x = year, y = gdp_growth)) +
  geom_line(color = "blue", size = 1) +
  geom_point(size = 2) +
  scale_x_date( ) +
  labs(title = "GDP Growth Rate",
       subtitle = "Yearly Percentage Change",
       x = "",
       y = "GDP Growth (%)") +
  theme_economist()

gdp_plot

Example 2: Unemployment Rate

unemployment_data <- data.frame(
  year = 2010:2020,
  unemployment_rate = c(9.8, 9.3, 8.1, 7.4, 6.2, 5.3, 4.9, 4.4, 4.0, 3.7, 8.1)
)


unemployment_data$year <- as.Date(as.character(gdp_data$year), format="%Y")

unemployment_plot <- ggplot(unemployment_data, aes(x = year, y = unemployment_rate)) +
  geom_line(color = "red", size = 1) +
  geom_point(size = 2) +
  scale_x_date()+
  labs(title = "Unemployment Rate",
       subtitle = "Yearly Percentage",
       x = "",
       y = "Unemployment Rate (%)") +
  theme_economist()

unemployment_plot

Conclusion

Themes in ggplot2 provide a powerful way to customize the appearance of your plots. By understanding how to modify existing themes and create your own, you can ensure that your plots are both visually appealing and professionally styled.

Feel free to explore the various elements that can be customized within themes, and experiment with different styles to find what works best for your data and audience.