• 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 Add Circles Around Specific Data Points in R

datavizpyr · June 7, 2021 ·

In this tutorial, we will learn how to add circles/triangles around a select data points in a scatter plot made with ggplot2 in R. Adding circles/triangles around data points is yet another way to add annotation to data points to highlight them to attract attention to the region of the plot.

Let us load tidyverse and palmer penguins data package. We will also set the ggplot2’s theme to theme_bw().

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))
penguins %>% 
  head()
## # A tibble: 6 x 8
##   species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex  
##   <fct>   <fct>           <dbl>         <dbl>            <int>       <int> <fct>
## 1 Adelie  Torge…           39.1          18.7              181        3750 male 
## 2 Adelie  Torge…           39.5          17.4              186        3800 fema…
## 3 Adelie  Torge…           40.3          18                195        3250 fema…
## 4 Adelie  Torge…           NA            NA                 NA          NA <NA> 
## 5 Adelie  Torge…           36.7          19.3              193        3450 fema…
## 6 Adelie  Torge…           39.3          20.6              190        3650 male 
## # … with 1 more variable: year <int>

Simple Scatter Plot with ggplot2

First, let us make a simple scatter plot with geom_point() in ggplot2.

penguins %>%
  ggplot(aes(x=bill_length_mm, flipper_length_mm))+
  geom_point()
Scatter Plot in R
Scatter Plot in R

We can see some data points are outliers in the above scatter plot and let us add circles around the points to highlight.

Add Circles Around Select Data Points

To add circles around select data points, we will use another layer of genom_point() with subset of data for adding circles. Instead of simple points, we will use pch value for hollow circle shaped points. We made the circle bigger with size and specified a color with colour argument.

penguins %>%
  ggplot(aes(x=bill_length_mm, flipper_length_mm))+
  geom_point()+
  geom_point(data=penguins %>% filter(bill_length_mm > 56),
             pch=21,
             size=4,
             colour="purple") 
ggsave("Add_circles_around_select_data_points_ggplot2.png")

Now we have highlighted a few extreme data points with purple circles.

How to Add Circles around Select Points in ggplot2
How to Add Circles around Select Points in ggplot2

Add Triangles Around Select Data Points

By using different value for pch we can choose to add different shape. Here, we use pch= 24 to add triangles around select data points.

penguins %>%
  ggplot(aes(x = bill_length_mm, y = flipper_length_mm)) +
  geom_point() +
  geom_point(data=penguins %>%
             filter(bill_length_mm > 55),
             pch = 24,
             size=4, 
             colour = "red") 
How to Add Triangles around Select Points in ggplot2
How to Add Circles around Select Points in ggplot2

Add Circles and Traingles Around Data Points

We can also add multiple shapes to different set of data points using additional geom_point() layers. In this example, we added both circles and triangles around select data points. Here we also filled triangles with colors.

penguins %>%
  ggplot(aes(x = bill_length_mm, y = flipper_length_mm))+
  geom_point()+
  geom_point(data = penguins %>% 
               filter(bill_length_mm > 55),
             pch=21, 
             size=4,
             colour="red") +
    geom_point(data=penguins %>% 
                 filter(bill_length_mm < 35),
             pch=24, 
             fill="lightblue", 
             alpha=0.5,
             size=4, colour="blue") 
How to Add Circles and Triangles around Select Points in ggplot2
How to Add Circles and Triangles around Select Points in ggplot2

Related posts:

Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? 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

Filed Under: add circles around data points, ggplot2, R Tagged With: ggplot2, R

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