Search code examples
rggplot2plotanomaly-detection

Draw circles around points belonging to a factor level in ggplot


A previous post describes how to draw red circles around points which exceed a given value in ggplot. I would like to do the same for anomaly detection results, but instead have the circles drawn around points belonging to a given factor level.

How could I change this code to allow circles to be drawn around a given factor level?

ggplot(mtcars, aes(wt, mpg)) + 
  geom_point() +
  geom_point(data=mtcars[mtcars$mpg>30,],
             pch=21, fill=NA, size=4, colour="red", stroke=1) +
  theme_bw()

Solution

  • Let's suppose that the "factor level" you are interested in is the value 10.4 for mtcars$mpg. mtcars$mpg is a numerical vector, so you first have to convert it into a factor.

    mtcars$mpg <- as.factor(mtcars$mpg)
    

    Then you can use the same code you used previously for values greater than a limit, except that this time the condition is to belong to the factor level 10.4:

    ggplot(mtcars, aes(wt, mpg)) + 
    geom_point() +
      geom_point(data=mtcars[mtcars$mpg %in% 10.4, ],
                 pch=21, fill=NA, size=4, colour="red", stroke=1) +
      theme_bw()
    

    Note that the conversion of mtcars$mpg to factor is not necessary and that the code will run on the numerical vector in the same way. I converted it since your question was about "factor level".

    Note also that if you are not dealing with factor levels but simply with values matching a certain number, you can use:

    ggplot(mtcars, aes(wt, mpg)) + 
      geom_point() +
      geom_point(data=mtcars[mtcars$mpg == 10.4, ],
                 pch=21, fill=NA, size=4, colour="red", stroke=1) +
      theme_bw()
    

    since you are now only testing for equality and not for appartenance.