Lesson 5
Customizing Themes and Saving Plots
Customizing Themes and Saving Plots

Welcome back! In the last lesson, you created bar plots and histograms to summarize both categorical and continuous data. This time, you’ll take your visualizations to the next level by learning how to customize themes and save your plots. Customizing themes not only makes your plots more attractive but also highlights the important aspects of your data.

What You'll Learn

In this unit, you’ll learn how to enhance your plots with custom themes and save them to files. We’ll start with a scatter plot of the iris dataset to review what you already know. Then, you’ll discover how you can add personalization to your plots by changing the text size, and font, and even save your work for future use.

By the end of this lesson, you'll be able to generate a scatter plot like the one shown below:

Here's a sneak peek at the code you'll be working with:

R
1# Load the built-in iris dataset 2data(iris) 3 4# Create the scatter plot and customize the theme 5p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 6 geom_point() + 7 theme_light() + 8 labs(title = "Sepal Length vs Sepal Width", 9 x = "Sepal Length", 10 y = "Sepal Width") + 11 theme( 12 plot.title = element_text(size = 14, face = "bold"), 13 axis.title.x = element_text(size = 12, face = "italic"), 14 axis.title.y = element_text(size = 12, face = "bold.italic") 15 ) 16 17# Save the customized plot to a file 18ggsave("scatter_plot.png", plot = p, width = 7, height = 7)
Step-by-Step Breakdown
Load built-in dataset

Let's start by loading the iris dataset.

R
1data(iris)
Creating the Scatter Plot

Let's start creating the scatter plot by using the iris dataset. The ggplot function initializes the plot and aes sets the aesthetic mappings, defining the x and y axes and the color to differentiate species. We add geom_point() to plot the data points.

R
1p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 2 geom_point()
Applying a Light Theme

Next, we apply the theme_light() function to give our plot a clean and minimalist look. labs is used to add labels to the plot, including the title and axis labels.

R
1p <- p + theme_light() + 2 labs(title = "Sepal Length vs Sepal Width", 3 x = "Sepal Length", 4 y = "Sepal Width")
Customizing the Theme

To make the plot more personalized, we can further customize the theme. Using the theme function, we can modify various elements of the plot. For instance, plot.title can be customized to change the text size and font face, and similarly, axis.title.x and axis.title.y can be adjusted to modify the axis titles.

The face parameter in element_text can take other values such as "plain", "italic", and "bold.italic" besides "bold".

R
1p <- p + theme( 2 plot.title = element_text(size = 14, face = "bold"), 3 axis.title.x = element_text(size = 12, face = "italic"), 4 axis.title.y = element_text(size = 12, face = "bold.italic") 5)
Saving the Plot

Finally, saving the plot is straightforward with the ggsave function. This function saves the last plot that was displayed or any specified plot to a file. By default, it saves the file in the working directory. You can specify the filename and format.

R
1ggsave("scatter_plot.png", plot = p, width = 7, height = 7)

With this, your customized plot is saved as an image file, with the specified dimensions of the saved image in inches.

The steps outlined will be broken down further during the practice section, helping you to understand each component and enabling you to apply these techniques to your own projects, ensuring they look polished and professional.

Why It Matters

Customizing themes allows you to make your plots visually appealing and tailored to your audience. Whether you're presenting to a team of data scientists, a classroom, or including them in a report, well-designed plots enhance communication and engagement. Saving your plots is equally important — you’ll be able to keep a record of your work, share it easily, and include it in documents or presentations.

Mastering these skills will not only make your visualizations stand out but also ensure they convey the message you intend. Ready to make your plots look professional and save them for future use? Let's dive into the practice section and get started!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.