Customizing ggplot2 Plots
Learn to customize plots in ggplot2, including adjusting fonts, legends, colors, and using colorbrewer.
We'll cover the following...
The ggplot2
package offers a great deal of customization in two ways. Firstly, we can leverage a significant library of geom_functions
to create differently styled plots. Everything from histograms to scatter plots to contour plots and even geographic maps. In fact, there are too many to cover them all here. As we need different plot types, it’s best to check the help files for ggplot2
. However, in addition to specific plot types, many customizations are available. We’ll focus on the common elements we can use to customize the look and feel of our plots. Let’s check out an example.
#load tidyverse librarieslibrary(ggplot2)library(purrr)library(tibble)library(dplyr, warn.conflicts = FALSE)library(tidyr)library(stringr)library(readr)library(forcats)#Use the iris datasetVAR_IrisData <- as_tibble(iris)#a histogram of sepal lengthsVAR_IrisData %>% ggplot(mapping = aes(x = Sepal.Length)) +geom_histogram(binwidth = 0.5)
- Lines 15–16: Produce a histogram of
Sepal.Length
, withbinwidth
of0.5
on the x-axis. Leave all other plot settings as default.
Even in such few lines of code, we can produce a reasonably visually appealing graph—and we have yet to apply any customization. In data science, these base settings are often good enough for exploratory purposes. However, we often need to fine-tune and customize our graphs to meet a specific format when we need to showcase results. The customizations discussed here will allow us to carry out that customization.
Labeling and legends
The ggplot2
package conveniently combines a plot’s labeling and legend elements into the function labs
. In the example below, we set up a scatter plot of Petal.Width
vs. Petal.Length
, with Sepal.Length
determining the data point size. We also set the most common titling and labeling parameters.
#load tidyverse librarieslibrary(ggplot2)library(purrr)library(tibble)library(dplyr, warn.conflicts = FALSE)library(tidyr)library(stringr)library(readr)library(forcats)#Use the iris datasetVAR_IrisData <- as_tibble(iris)#a scatter plot based on the iris datasetVAR_IrisData %>% ggplot(mapping = aes(x = Petal.Length, y = Petal.Width, size = Sepal.Length)) +geom_point() +labs(title = "Scatter Plot",subtitle = "Relationship Between Petal Length, Petal Width and Sepal Length",caption = "Data from the iris data set",x = "Petal Length",y = "Petal Width") +scale_fill_discrete(name = "Sepal Length") +theme(legend.position = "bottom")
-
Lines 15–23: Create a scatter plot, customizing the chart title, axes titles, subtitle, and caption. In the scatter plot, set the x data as
Petal.Length
, y data asPetal.Width
, and data point size asSepal.Length
. -
Lines 17–21: ...