
Image by Editor | Midjourney
Customizing ggplot2 themes in R helps you create visuals that match your brand. You can change things like colors, fonts, and layout to make your plots look consistent. ggplot2 offers many options to adjust the style of your graphs. This lets you create professional and unique visuals that fit your brand.
In this guide, we’ll show you how to make these changes using ggplot2.
Introduction to ggplot2 Themes
When using ggplot2 in R, themes help improve the look of your plots. They control non-data elements like background, grid lines, fonts, and text. By default, ggplot2 uses the theme_gray() theme for a clean, simple look. However, you can apply other pre-built themes to customize your plot, such as:
- theme_bw(): A black-and-white theme with white background
- theme_minimal(): A minimalistic theme with no gridlines
- theme_classic(): A classic theme with axes and no grid lines
- theme_void(): A completely blank theme, useful for maps or overlays
Customizing Themes for Brand Consistency
Here are some ways that you can customize themes in ggplot2 to better align with your brand, or to create a new branding experience of your own.
Changing Background Color
Customizing the background color in ggplot2 helps match your plots to your brand’s style. You can change the plot background or the panel background using the theme() function.
- Plot background: Changes the entire plot’s background
- Panel background: Adjusts the area where the data is plotted
# Custom theme with brand colors
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme(
panel.background = element_rect(fill = "#f5f5f5"), # light gray background
plot.background = element_rect(fill = "#ffffff"), # white plot background
panel.grid.major = element_line(color = "#e1e1e1") # light gridlines
)

Adjusting Axis Text and Titles
Customizing axis text and titles in ggplot2 makes your plots easier to read and helps match your brand. Use the theme() function with element_text() to change fonts, sizes, and colors.
- Axis titles: Customize with axis.title.x and axis.title.y
- Axis text: Adjust tick labels using axis.text.x and axis.text.y
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme(
axis.title = element_text(family = "Arial", face = "bold", size = 14, color = "#333333"),
axis.text = element_text(family = "Arial", size = 12, color = "#666666"),
plot.title = element_text(family = "Arial", size = 16, face = "bold", color = "#1a73e8")
) +
labs(title = "Weight vs Miles per Gallon")

Customizing Gridlines
Customizing gridlines in ggplot2 helps highlight the data and makes your plot look better. Use the theme() function with element_line() to change the style of gridlines.
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme(
panel.grid.major = element_line(color = "#D0D0D0", size = 0.5), # subtle gridlines
panel.grid.minor = element_blank() # remove minor gridlines
)

Adjusting Legends
Customizing legends in ggplot2 makes your plot clearer and matches your brand. Use the theme() function to change the legend’s position, text, and background.
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point() +
theme(
legend.position = "top", # Position at the top
legend.background = element_rect(fill = "#f0f0f0", color = "#e0e0e0"),
legend.title = element_text(face = "bold", color = "#333333"),
legend.text = element_text(size = 12, color = "#666666")
)

Creating a Custom ggplot2 Theme
Creating a custom theme in ggplot2 helps keep your plots consistent. You can create a reusable theme using the theme() function and save it as a variable.
library(ggplot2)
# Custom Theme Function
theme_brand <- function() {
theme_minimal() +
theme(
text = element_text(family = "Arial", color = "#004080"),
plot.title = element_text(size = 18, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 14, hjust = 0.5, margin = margin(b = 10)),
axis.title = element_text(size = 12, face = "bold"),
axis.text = element_text(size = 10),
panel.grid.major = element_line(color = "#80C0E0", size = 0.5),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "#FFFFFF"),
plot.background = element_rect(fill = "#FFFFFF", color = NA)
)
}
# Example Plot
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "#004080") +
ggtitle("Fuel Efficiency by Engine Size") +
theme_brand()

Exporting Plots
After customizing your plot, you can save it in formats like PNG, PDF, or SVG. The ggsave() function lets you specify the size, resolution, and format for your output file. Make sure to use high resolution for publishing purposes.
ggsave("custom_plot.png", dpi = 300)
Conclusion
Customizing ggplot2 themes in R helps you create plots that reflect your brand’s style. By adjusting elements like background color, fonts, gridlines, and legends, you can make your visuals consistent and professional.
With simple tweaks, you can create attractive and clear plots that align with your brand’s identity.
