Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Annotate points of a geom_boxplot, that fulfill specified conditions?

Say I have a boxplot that I created per ggplot(). And this boxplot has points above the upper whisker and below the lower whisker. If I desire to comment only a subset of those points, for example, only points, that correspond to variable values 50 and above or 5 and below. How would I do that?

EDIT

For clarification: Instead of commenting and point out, that specific
points are above or below a specified threshold, I meant commenting each point individually, like labelling the points that are above and below the threshold with their respective value. So if a value like 70 is above the upper threshold of 50, I’d like the point to be annotated directly next to it with "70".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would overplot some points in a new geom_point layer using a distinct color by passing the appropriate subset of the data, then add text labels with the same subset.

set.seed(1)
df <- data.frame(x = 'Data', y = rnorm(1000, 26, 7))

library(ggplot2)

ggplot(df, aes(x, y)) + 
  geom_boxplot() +
  ylim(c(0, 60)) +
  geom_point(data = subset(df, y > 50 | y < 5), color = 'red') +
  geom_text(data = subset(df, y > 50 | y < 5), aes(label = round(y, 2)),
            nudge_x = 0.08)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading