r - annotate boxplot in ggplot2 -
i've created side-by-side boxplot using ggplot2.
p <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) p + geom_boxplot(aes(fill=factor(cyl))) i want annotate min, max, 1st quartile, median , 3rd quartile in plot. know geom_text() can , may fivenum() useful. cannot figure out how can do!. these values should displayed in plot.
the succinct way can think of use stat_summary. i've mapped labels color aesthetic, can, of course, set labels single color if wish:
ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(cyl))) + geom_boxplot(width=0.6) + stat_summary(geom="text", fun.y=quantile, aes(label=sprintf("%1.1f", ..y..), color=factor(cyl)), position=position_nudge(x=0.33), size=3.5) + theme_bw() in code above use quantile summary function label values. ..y.. refers output of quantile function (in general, ..*.. ggplot construction using values calculated within ggplot).

Comments
Post a Comment