Individual points sharing the same legend box with ggrepel

Viewed 73

When I generate the graph like this one

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species,label=Species)) + 
geom_label_repel()

Each point, individually, has its own labeled. Is there a way to create a graph where we only had 3 text box (in this case, because we have three Species), and all the points corresponding to this species bound with lines?

Thanks!

3 Answers

I like the ggforce::geom_mark_* geoms for this:

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species,label=Species)) +
  geom_point() +
  ggforce::geom_mark_hull()

enter image description here

You can accomplish something similar to what Jon Spring provided by using stat_ellipse and annotate.

library(ggplot2)


ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  stat_ellipse(aes(x = Sepal.Length, y = Sepal.Width, color = Species), level = 0.998) +
  annotate(
    geom = "text", x = 3.9, y = 2, label = "Versicolor", hjust = "left", color = 'dark green'
    ) +
  annotate(
    geom = "text", x = 4.2, y = 4, label = "Setosa", hjust = "left", color = 'red'
    ) +
    annotate(
    geom = "text", x = 7.1, y = 4.1, label = "Virginica", hjust = "left", color = 'blue'
    )

Created on 2021-03-17 by the reprex package (v0.3.0)

Thank Jon and Eric,

However, these two solutions were a bit confusing to show my data, so I preferred the dots bound by lines to a single label.

Thanks, Robert

Related