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 themep +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 themep + 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 functionp +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 themep +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
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.