Open In App

Autoplot Methods in R

Last Updated : 23 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn about how to add fill aesthetic to a ggplot2 autoplot function in R programming language.

ggplot2 is a popular R package for creating elegant and customizable data visualizations. The autoplot() function in ggplot2 is a convenient way to generate various types of plots based on the type of data object passed to it. The fill aesthetic is a common feature used in ggplot2 to color different groups in a plot. In this article, we will learn how to add or override fill aesthetic to a ggplot2 autoplot() function.

Prerequisites Concepts:

  • ggplot2: An R package for data visualization and graphics.
  • autoplot: A function in ggplot2 for automatically generating different types of plots based on the data object passed to it.
  • fill aesthetic: A feature in ggplot2 used to color different groups in a plot based on a categorical variable.
  • scale_fill_manual: A function in ggplot2 used to manually specify the fill colors for different groups in a plot.

Steps to add fill aesthetic to a ggplot2 autoplot function in R:

To add or override fill aesthetic to a ggplot2 autoplot() function, we need to follow these steps:

Step 1: Install required packages by executing the below commands in R.

install.packages("ggplot2")
install.packages("ggfortify")

Step 2: Load the ggplot2 and ggfortify libraries in a program.

library(ggplot2)
library(ggfortify)

Step 3: Here, we are loading the iris dataset which is available in the ggplot2 package.

data("iris")

Step 4: Here, we use the autoplot() function to generate a scatterplot of the iris dataset with the k-means clustering algorithm applied to the first four columns. We are also setting the fill aesthetic to the Species variable to color the points based on their species.

autoplot(kmeans(iris[, 1:4], centers = 3), data = iris, frame = TRUE, fill = iris$Species)

Step 5: Override fill aesthetic using scale_fill_manual. By explicitly setting the fill colors in this instance using the scale fill manual function, we are overriding the fill aesthetic. The fill colours are set to red, blue, and green for each of the three species.

autoplot(kmeans(iris[, 1:4], centers = 3), data = iris, frame = TRUE) +
 scale_fill_manual(values = c("red", "blue", "green"))

Example 1: Adding fill aesthetic to a scatter plot

In this example, the k-means clustering technique will be used in columns 1, 3, 4, 5, and 6 of the mtcars dataset to create a scatterplot. According to their cylinder values, the points should be colored red for 4 cylinders, blue for 6 cylinders, and green for 8 cylinders. The colors for each cylinder value are individually specified using the scale fill manual function.

R




# Load required library
library(ggplot2)
library(ggfortify)
 
# Load mtcars dataset
data("mtcars")
 
# Assign colors to each cylinder value
cylinder_colors <- c("4" = "red",
                     "6" = "blue",
                     "8" = "green")
 
# Create the plot with fill aesthetic and manual scale
autoplot(kmeans(mtcars[, c(1, 3, 4, 5, 6)],
                centers = 3), data = mtcars,
         frame = TRUE, fill = mtcars$cyl) +
  scale_fill_manual(values = cylinder_colors)


Output:

 

Example 2: Adding fill aesthetic to a time series plot

In this example, AirPassengers dataset is used, which contains monthly passenger numbers on international airline flights. The default autoplot() function creates a time series plot with blue color and solid line type. We have added a fill aesthetic to highlight the trend in passenger numbers over time.

R




# Load required libraries
library(ggplot2)
library(ggfortify)
 
# load data
data("AirPassengers")
 
# create autoplot with default fill aesthetic
autoplot(AirPassengers, ts.colour = "blue",
         ts.linetype = "solid") +
  ggtitle("Default Autoplot of AirPassengers Dataset") +
  ylab("Passengers") + xlab("Year")
 
# add fill aesthetic to autoplot
autoplot(AirPassengers, ts.colour = "blue",
         ts.linetype = "solid", fill = "trend") +
  ggtitle("Autoplot with Trend Fill Aesthetic") +
  ylab("Passengers") + xlab("Year")


Output:

 

Example 3: Adding fill aesthetic to a seasonal plot

In this example, nottem dataset is used, which contains monthly average air temperatures in degrees Fahrenheit from 1920 to 1950. The default autoplot() function creates a seasonal plot with blue color and solid line type. We have added a fill aesthetic to highlight the different seasons in the temperature data.

R




# load data
data("nottem")
 
# create autoplot with default fill aesthetic
autoplot(nottem, ts.colour = "blue", ts.linetype = "solid") +
  ggtitle("Default Autoplot of nottem Dataset") +
  ylab("Temperature") + xlab("Month")
 
# add fill aesthetic to autoplot
autoplot(nottem, ts.colour = "blue", ts.linetype = "solid", fill = "season") +
  ggtitle("Autoplot with Season Fill Aesthetic") +
  ylab("Temperature") + xlab("Month")


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments