r - ggplot2 boxplot stat_summary text placement by group -


in plot below, i'd number of observations (40 in case) overlayed on top of each boxplot. code below doesn't work when there's fill aesthetic. text need adjusted horizontally (1 left, 1 center, 1 right in case) overlay corresponding boxplots.

dt <- data.table(     x = factor(rep(1:2, each=120))     , f = rep(letters[1:3], 40)     , y = c(rnorm(120, 1:3), rnorm(120, 1:3*2)) ) table(dt$x, dt$f)  +--------------+ |       b  c | +--------------+ |   1 40 40 40 | |   2 40 40 40 | +--------------+  frequencyannotation <- function(x) {    c(y = (quantile(x, .75, names = f) + median(x))/2, label=length(x)) } ggplot(dt, aes(x=x, y=y, fill=f)) +     geom_boxplot() +     stat_summary(fun.data = frequencyannotation, geom='text') 

enter image description here

as boxplots dodged when use argument fill=, have add position_dodge() stat_summary() call.

ggplot(dt, aes(x=x, y=y, fill=f)) +       geom_boxplot() +       stat_summary(fun.data = frequencyannotation, geom='text',                     position = position_dodge(width = 0.75))  

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -