Labelling of median, upper and lower whiskers in Boxplots

Hi,
I have a data of students and the marks obtained by them. I want the median and the upper/lower whisker to be labelled on the boxplot. When I give geom_text, I get a weird labelling. How can I resolve this?

library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
data<-tibble::tribble(
         ~student, ~marks,
        "level-1",    25L,
        "level-1",    45L,
        "level-1",    13L,
        "level-1",    48L,
        "level-1",    38L,
        "level-1",    43L,
        "level-1",    43L,
        "level-1",    50L,
        "level-1",    20L,
        "level-1",    38L,
        "level-1",    49L
        )


ggplot(data,aes(student,marks))+
  geom_boxplot(fill="orange")+
  geom_text(aes(label=median(marks)))


Created on 2022-08-16 by the reprex package (v2.0.1)

The line geom_text(aes(label=median(marks))) is inheriting the x and y from data, so the calculated median is printed for each (x,y) in the tibble.

One way to handle this is to create a second tibble that provides only those values in the boxplot you want to label. Using the quantile() function, we can create the following:

data_labels = tibble(student = 'level-1',
                     values = quantile(data$marks)
                     )

which produces...

image

Now, replacing this in your ggplot chain and specifying data = data_labels in geom_text(),

ggplot(data,aes(student,marks))+
  geom_boxplot(fill="orange")+
  geom_text(data = data_labels, aes(student, values, label = values))

we end up with...

Thanks for the help..

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.