0

Is there a way to add factor label on top of points in the scatterplot with ggplot?

Take iris dataset, for example (unique rows).

iris_unique <- unique(iris)

Species column is a factor with 3 levels. I can make a scatterplot out of sepal.length vs sepal.width:

ggplot(iris_unique, aes(x=Sepal.Length, y=Sepal.Width, col=Species)) +
  geom_point(size = 3)+
  theme_bw()

and I need a way to place the label above the 3 different regions, indicating which factor level they belong to.

For few levels color sufficies but I am normally handling datasets with cca. 20 factor levels and color is useless to indicate different ones on the scatterplot. If there was a way to add text label to the graph (not to each dot, but one label per factor level hovering roughly above the corresponding dot region), it would still work in conveying the message, I think.

Thank you all in advance!

  • Do you have an idea of the position of these labels (x and y coordinate)? Then you can use `geom_text` to add labels at those positions. – user3640617 Dec 03 '17 at 15:57
  • The code is in another stack overflow post [posted answered here](https://stackoverflow.com/questions/15624656/label-points-in-geom-point) – wissem Dec 03 '17 at 16:46
  • I don't know coordinates. The factor is a result of the clustering algorithm and I would want label to be placed automatically based on the position of dots for that factor level. – Vinko Tosevski Dec 03 '17 at 20:46
  • @wissem - I found that answer before, that's why I specified in my question that I do not want to label each dot (I have thousands) but only the factor level (those I have up to 20 or so). – Vinko Tosevski Dec 03 '17 at 20:47

0 Answers0