Last Updated
Viewed 4,893 Times

I've created a side-by-side boxplot using ggplot2.

p <- ggplot(mtcars, aes(x=factor(cyl), y=mpg))
p + geom_boxplot(aes(fill=factor(cyl)))

I want to annotate with min, max, 1st quartile, median and 3rd quartile in the plot. I know geom_text() can do so and may be fivenum() is useful. But I cannot figure out how exactly I can do!. These values should be displayed in my plot.

I am trying to add labels to geom_boxplot for extreme values with dplyr and am getting an inconsistency either with ggplot or dplyr. what am i doing wrong?

#toy exmaple

#subset with extreme values


I am trying to put annotation in a faceted boxplot using the method described in the top answer of this question: Annotating text on individual facet in ggplot2

And formatting the text using methods picked up here:

I can't seem to figure out why geom_text() will not insert the line break in my code where I have place a \n. Here is a simplified version: <- data.frame(Main = rep("Ratio", 100),
                     CAT = c('A','B','C','D'),
                     value = rnorm(100, mean = 1.5, sd = 1.5))
p.text <- data.frame(Main = "Ratio",
                     CAT = 'B',
                     value = 7,
                     lab = "Text")
p <-  ggplot(data =, aes(x = CAT, y = value, fill = CAT)) +
      geom_boxplot() +
      scale_y_continuous(breaks = c(0:6), limits = c(0,8)) +
      facet_wrap(~ Main, scales = 'fixed', nrow = 1, ncol = 1) +
      geom_text(data = p.text, hjust = 0, parse = TRUE,
                label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \n bold(MDD): n.s.)")

enter image description here

Among some other nonsense, I have tried:

label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \n bold(MDD): n.s.)"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \n, bold(MDD): n.s.)"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \"\n\", bold(MDD): n.s.)"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\", \"\n bold(MDD): n.s.\")"
label = "paste(bold(CMV): f ^ 2, \" = 0.04, p = 0.003\"\n, bold(MDD): n.s.)"

...but nothing has worked.

In case it is not obvious, what I want is the CMV results on one line and MDD results on another line, while keeping the bold fonts and superscripted "2". My final graph will consist of one graph with one facet and one with three facets stuck together using grid.arrange(), but my example is just the one graph.


I am trying to add the labels for extreme values (outliers or not) on geom_box plots. I found this question, which is almost exactly similar to mine [ extreme value labels ggplot2 in geom_boxplot ] The answer provided by yonicd almost works for me:


df_bound=left_join(df_bound, df, by=c("id","val"))

ggplot(df,aes(x=id, y=val, fill=id, label=var)) + geom_boxplot() +
geom_point(aes(group=id), data=df_bound)+
geom_text(aes(group=id), data=df_bound, hjust=-1, size=4)

It seems obvious that I would just need to replace the [ $out] in


to have the extreme values instead of the outliers. If I use


the labels for the outliers don't appear. How could if fix that?

Similar Question 4 (2 solutions) : ggplot2 boxplot

Similar Question 5 (1 solutions) : Boxplot with ggplot2

Similar Question 6 (1 solutions) : Creating a boxplot, ggplot2

Similar Question 7 (1 solutions) : ggplot2 width of boxplot

Similar Question 8 (1 solutions) : Boxplot of table using ggplot2

Similar Question 9 (1 solutions) : Sorting a ggplot2 boxplot [duplicate]