How To Annotate Clusters with Circle/Ellipse by a Variable in R ggplot2
Last Updated :
24 Mar, 2022
In this article, we will discuss how to annotate Clusters with Circle/Ellipse by a categorical variable in the R Programming Language using the ggplot2 package.
To add a circle or ellipse around a cluster of data points, we use the geom_mark_circle() and geom_mark_ellipse() function of the ggforce package. This function automatically computes the circle/ellipse radius to draw around the cluster of points by categorical data.
First, we will plot the data in a scatter plot using the geom_point function of the ggplot2 package. We will use the color parameter of the aes() function to color the plot by a categorical variable group.
Syntax:
ggplot(df, aes( x, y ) ) + geom_point( aes( color ))
Arguments:
- df: determines the data frame to be used.
- x and y: determine the x-axis and y-axis variables respectively.
- color: determines the categorical variable for coloring the data point clusters.
Example:
Here, is a basic scatter plot made using the geom_point() function of the ggplot2 package. We have colored the plot by the categorical variable group.
R
library (tidyverse)
theme_set ( theme_bw (16))
xAxis <- rnorm (1000)
yAxis <- rnorm (1000) + xAxis + 10
group <- rep (1, 1000)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
sample_data <- data.frame (xAxis, yAxis, group)
ggplot (sample_data, aes (x = xAxis,
y = yAxis))+
geom_point ( aes (color = as.factor (group)))
|
Output:

Annotate circles around cluster:
To annotate a circle around a cluster of points by the group we use the geom_mark_circle() function of the ggforce package. To use this function we first install & import the ggforce package by using:
install. packages('ggforce')
library(ggforce)
Now, we will annotate the circle around a cluster of data points by using the geom_mark_circle() function.
Syntax:
ggplot(df, aes( x, y ) ) + geom_point( aes( color )) + geom_mark_circle( aes(color) )
Example:
Here, is a basic scatter plot with circles around a cluster of data points colored by a categorical variable group.
R
library (tidyverse)
library (ggforce)
theme_set ( theme_bw (16))
xAxis <- rnorm (500)
yAxis <- rnorm (1000) + xAxis + 10
group <- rep (1, 500)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
sample_data <- data.frame (xAxis, yAxis, group)
ggplot (sample_data, aes (x = xAxis,
y = yAxis))+
geom_point ( aes (color = as.factor (group)))+
geom_mark_circle ( aes (color = as.factor (group)), expand = unit (0.5, "mm" ))+
theme (legend.position = "none" )
|
Output:

Annotate ellipses around cluster:
To annotate an ellipse around a cluster of points by the group we use the geom_mark_ellipse() function of the ggforce package. This function automatically computes the dimensions of the ellipse and overlays it on top of the scatter plot.
Syntax:
ggplot(df, aes( x, y ) ) + geom_point( aes( color )) + geom_mark_ellipse( aes(color) )
Example:
Here, is a basic scatter plot with ellipses around a cluster of data points colored by a categorical variable group.
R
library (tidyverse)
library (ggforce)
theme_set ( theme_bw (16))
xAxis <- rnorm (500)
yAxis <- rnorm (1000) + xAxis + 10
group <- rep (1, 500)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
sample_data <- data.frame (xAxis, yAxis, group)
ggplot (sample_data, aes (x = xAxis,
y = yAxis))+
geom_point ( aes (color = as.factor (group)))+
geom_mark_ellipse ( aes (color = as.factor (group)), expand = unit (0.5, "mm" ))+
theme (legend.position = "none" )
|
Output:

Customizing the aesthetics
We can customize the aesthetics of the geom_mark_* function by using the color, fill, and alpha property of the aes() function.
Syntax:
ggplot(df, aes( x, y ) ) + geom_point( aes( color )) + geom_mark_ellipse( aes(color, fill, alpha) )
where,
- color: determines the color of the boundary of the circles or ellipses.
- fill: determines the background color of the circles or ellipses.
- alpha: determines the transparency of the circles or ellipses.
Example:
In this example, we will plot a scatter plot overlayed by ellipses with a background colored by the group categorical variable.
R
library (tidyverse)
library (ggforce)
theme_set ( theme_bw (16))
xAxis <- rnorm (500)
yAxis <- rnorm (1000) + xAxis + 10
group <- rep (1, 500)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
sample_data <- data.frame (xAxis, yAxis, group)
ggplot (sample_data, aes (x = xAxis,
y = yAxis))+
geom_point ( aes (color = as.factor (group)))+
geom_mark_ellipse ( aes (fill = as.factor (group)), expand = unit (0.5, "mm" ))+
theme (legend.position = "none" )
|
Output:

Similar Reads
How To Annotate a Plot with Circle in R
In this article, we will discuss how to Annotate a Plot with Circle in R Programming Language. We can do by using ggplot2 package Aesthetic mappings can be created to the plot object to determine the relationship between the x and y axis respectively. Additional components can be added to the create
2 min read
How to Annotate a Specific Cluster or Group in ggplot2 in R?
The ggplot method in R Programming Language is used to do graph visualizations using the specified dataframe. It is used to instantiate a ggplot object. Aesthetic mappings can be created to the plot object to determine the relationship between the x and y-axis respectively. Additional components can
4 min read
How to Assign Colors to Categorical Variable in ggplot2 Plot in R ?
In this article, we will see how to assign colors to categorical Variables in the ggplot2 plot in R Programming language. Note: Here we are using a scatter plot, the same can be applied to any other graph. Dataset in use: YearPointsUsers1201130user12201220user23201315user34201435user45201550user5 To
2 min read
How to display a variable with subscript ggplot2 graph in R?
In R, the ggplot2 package is one of the most popular and powerful tools for creating visually appealing and customizable plots. Sometimes, you may need to display mathematical notation or subscripts in your graphs, especially when working with scientific data or equations. This article will guide yo
4 min read
How to annotate a plot in ggplot2 in R ?
In this article, we will discuss how to annotate functions in R Programming Language in ggplot2 and also read the use cases of annotate. What is annotate?An annotate function in R can help the readability of a plot. It allows adding text to a plot or highlighting a specific portion of the curve. Th
4 min read
Multiple Density Plots and Coloring by Variable with ggplot2 in R
In this article, we will discuss how to make multiple density plots with coloring by variable in R Programming Language. To make multiple density plots with coloring by variable in R with ggplot2, we first make a data frame with values and categories. Then we draw the ggplot2 density plot using the
3 min read
How to Create a Scatterplot in R with Multiple Variables?
In this article, we will be looking at the way to create a scatter plot with multiple variables in the R programming language. Using Plot() And Points() Function In Base R: In this approach to create a scatter plot with multiple variables, the user needs to call the plot() function Plot() function:
3 min read
How to Add Vertical Lines By a Variable in Multiple Density Plots with ggplot2 in R
In this article, we will discuss how to add vertical lines by a variable in multiple density plots with ggplot2 package in the R Programming language. To do so first we will create multiple density plots colored by group and then add the line as a separate element. Basic Multiple Density Plot: To ma
3 min read
How to Position Annotate Text in the Blank Area of Facet ggplot in R
Facet plots in ggplot2 are a powerful way to display multiple plots based on different subsets of your data. However, annotating text within these plots can be challenging, especially when you want to place text in specific blank areas. This article will walk you through the process of positioning a
3 min read
How To Show Mean Value in Boxplots with ggplot2?
In this article, we will discuss how to show mean value in Boxplot with ggplot2 using R programming language. Firstly, we will create a basic boxplot using the geom_boxplot() function of the ggplot2 package and then do the needful, so that the difference is apparent. Syntax: ggplot() + geom_boxplot(
2 min read