• 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 encircle a set of points in R with ggplot2

datavizpyr · December 21, 2022 ·

In this post, we will learn how to encircle data points with ggplot2. In R, there are multiple ways to highlight/annotate data points. Here we will use, ggalt, one of the ggplot2 extension packages to encircle data points. ggalt‘s geom_encircle() function can automagically encircle points belonging to multiple groups.

Setting data and packages

First, let us load the packages needed. We can install ggalt from CRAN and we have ggalt version ‘0.4.0’. Let us also load palmer penguins dataset for making scatter plots and highlighting or encircling points on the plot.

library(tidyverse)
library(palmerpenguins)
library(ggalt)
theme_set(theme_bw(16))
packageVersion("ggalt")
# 0.4.0

Let us make a simple scatter plot

penguins %>%
  ggplot(aes(x = flipper_length_mm,
             y = body_mass_g, 
             color = species))+
  geom_point()

Circling data points with geom_encircle()

We can use geom_encircle() as additional layer to our existing ggplot to automagically detect and encircle data points.

penguins %>%
  ggplot(aes(x =flipper_length_mm, 
             y = body_mass_g,
             color = species))+
  geom_point()+
  geom_encircle(s_shape = 1, expand=0)

In this example, geom_encircle() has encircled three groups, i.e. species, and circled around each group’s the data points. If we had not added color by species in our scatter plot, ggalt’s geom_encircle() would encircle all the data points in the plot.

encircle multiple group's data points automatically with ggalt
encircle multiple group’s data points automatically with ggalt

Circling Select Data points with geom_encircle()

We can also select certain data points to encircle. In this example, we are encircling data points corresponding to Gentoo penguins. To do that we use data argument within geom_encircle(), like any other geoms in ggplot and provide the subset of data that we want to. use to encircle data points.

penguins %>%
  ggplot(aes(x=flipper_length_mm, 
             y = body_mass_g,
             color=species))+
  geom_point()+
  geom_encircle(data = filter(penguins, 
                              species==  "Gentoo"),
                s_shape=1, expand=0)
ggsave("encircle_select_points_ggalt_geom_encircle.png")

geom_encircle() has encircled the selected data points, i.e. Gentoo species.

how to encircle select. points using ggalt's geom_circle()
how to encircle select points using ggalt’s geom_circle()

Here is another example of highlighting a select group using geom_encircle() function in ggalt.

penguins |> 
  ggplot (aes(x = bill_depth_mm, y = flipper_length_mm, color = species)) +
  geom_point() +
  geom_encircle(data = penguins |> filter(species=="Gentoo"),
                aes(x = bill_depth_mm, y = flipper_length_mm),
                color = "blue", size = 1.5) 
encircle select group using geom_encircle() function in ggalt
encircle select group using geom_encircle() function in ggalt

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 Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, R Tagged With: ggalt geom_encircle(), ggplot2 extension ggalt

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