• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to Annotate a Specific Cluster or Group in ggplot2

datavizpyr · June 2, 2021 ·

In this tutorial, we will learn how to annotate or highlight a specific cluster/group in R using ggplot2. We can use R package ggforce to annotate a select group as a circle or ellipse on a scatter plot. In this example, we will use geom_mark_ellipse() function to highlight a cluster on scatterplot. To annotate specific cluster as circle, we can use geom_mark_circle() function from ggforce.

Let us get started by loading the package and data needed to make the plot.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

Scatter plot with multiple clusters

First, let make a scatterplot using ggplot2’s geom_point() after removing any missing data.

penguins <- penguins %>%
            drop_na()
# simple scatter plot colored by variable
penguins %>%
  ggplot(aes(x = bill_length_mm,
             y = flipper_length_mm))+
  geom_point(aes(color = species))
ggsave("scatterplot_with_ggplot2.png")
ggplot2 Scatterplot
Scatterplot with ggplot2

How to Annotate a Specific Cluster or Group using geom_mark_ellipse

Let us annotate specific cluster of interest using geom_mark_ellipse() function in ggforce. We will start with making the scatter plot as above. To annotate specific cluster, we will subset the data that we want to highlight and use the data to geom_mark_ellipse() function. We also color and label the group using aes() within geom_mark_ellipse() function.

penguins %>%
  ggplot(aes(x = bill_length_mm,
             y = flipper_length_mm))+
  geom_point(aes(color = species))+
  geom_mark_ellipse(data = penguins %>% 
                    filter(species == "Gentoo"),
                    aes(color = species,
                        label = species),
                    expand = unit(0.5, "mm"))+
  theme(legend.position = "none")
ggsave("annotate_a_select_cluster_with_ellipse_colored_group_ggforce.png")

In this example, we have annotated one group with ellipse, but also have colored other groups in the plot.

Annotate a Select Group with Ellipse using ggforce
Annotate a Select Group with Ellipse using ggforce

How to Annotate and Add Color to a Specific Cluster

Sometimes we might want the group of interest to standout by coloring only that group. We can do that by adding two geom_point() layers. One with all the data points, but no color. And the second geom_point() layer with data containing the group of interest, but with aes() function to color the group.

penguins %>%
  ggplot(aes(x = bill_length_mm,
             y = flipper_length_mm))+
  geom_point()+
  geom_mark_ellipse(data = penguins %>% 
                    filter(species=="Gentoo"),
                    aes(color = species,
                        label = species),
                    expand = unit(0.5, "mm"))+
  geom_point(data = penguins %>% 
                    filter(species == "Gentoo"),
             aes(color = species))+
  theme(legend.position = "none")
ggsave("annotate_select_cluster_highlighted_with_ellipse_ggforce.png")
Annotate and Color a Select Group with Ellipse using ggforce
Annotate and Color a Select Group with Ellipse using ggforce

You might also be interested in How To Annotate Clusters with Circle/Ellipse by a Variable in R

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Make Axis Title Bold Font with element_text()How to Make Axis Title Bold Font with ggplot2

Filed Under: ggplot2, R Tagged With: ggplot add annotation

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version