Home > OS >  Add labels to classification points boundaries in R
Add labels to classification points boundaries in R

Time:09-17

enter image description hereI am working on a project in which I have to put labels on my chart for the boundaries(encircles) around some scatter points.[![enter image description here][2]][2]

As it is shown in the picture, I want to put GrainSize labels on the three encircled boundaries inside triangle. I am using geom_encircle() command from ggalt package with ggplot2.

For example: The plot above has 3 categories, Setosa, Versicolor and Verginica I want these labels to be placed on classification boundaries as well, Like the eclips of setosa should be labeled as setosa and similarly 2 other categories. I found ggforce package usefule but that is limited for eclips or circle shapes only, is there any way that I can put label on these three encircles(Setosa, Versicolor and Verginica)

CodePudding user response:

You could try by creating a separate label dataframe and position the grouping labels as you wish; have gone for a simple solution here.

library(ggplot2)
library(ggalt)
library(dplyr)

labs <- 
  iris %>% 
  group_by(Species) %>% 
  filter(Sepal.Length == max(Sepal.Length))
  

ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) 
  geom_point() 
  geom_encircle(expand = 0.01) 
  geom_text(data = labs, 
            aes(Sepal.Width, Sepal.Length, label = Species),
            nudge_y = 0.15) 
  theme(legend.position = "none")

Created on 2021-09-14 by the reprex package (v2.0.0)

  • Related