library(ggplot2)
data(mpg)
<- ggplot(mpg, aes(x = displ, y = hwy)) +
p 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
+ theme_gray() p
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:
+ theme_classic() p
+theme_minimal() p
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:
<- theme(
custom_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
+ custom_theme p
Creating a Reusable Custom Theme Function
To make your customizations reusable, you can create a theme function:
<- function() {
theme_custom 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
+ theme_custom() p
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:
<- function() {
theme_economist 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
+ theme_economist() p
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
<- data.frame(
gdp_data 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
$year <- as.Date(as.character(gdp_data$year), format="%Y")
gdp_data
<- ggplot(gdp_data, aes(x = year, y = gdp_growth)) +
gdp_plot 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
<- data.frame(
unemployment_data 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)
)
$year <- as.Date(as.character(gdp_data$year), format="%Y")
unemployment_data
<- ggplot(unemployment_data, aes(x = year, y = unemployment_rate)) +
unemployment_plot 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.