df2<-data.frame(id=c("a","f","f","b","b","c","c","c","d","d","",""),
var=c(12,20,15,18,10,30,5,8,5,5,3,5))
give.n <- function(x){
return(c(y = mean(x), label = length(x)))
}
ggplot(data=subset(df2, id != ""), aes(x = reorder(id, -var), y = var)) +
geom_boxplot()+
stat_summary(fun.data = give.n, geom = "text",
position = position_jitter(height=1, width = 0))+
theme(axis.text.x = element_text(angle = 90, hjust = 1, size=11, vjust = -.005))+
ggtitle("Title")+
xlab("")+
ylab("value")
我有上面的情节,但是我想把计数放在箱线图中的中线以上,这样它们就更明显了。以这种方式使用position_jitter并不总是阻止计数与中间条重叠。有什么建议? *编辑提供df2
答案 0 :(得分:2)
您需要在返回的y =
值中定义调整。例如,让它返回中位数+ 0.1。您必须手动调整0.1数据。
give.n <- function(x){
return(c(y = median(x) + 0.1, label = length(x)))
}
ggplot(iris, aes(Species, Sepal.Width)) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text")
或者,如果您希望它在中间和上铰链之间准确居中,您可以像这样计算该位置:
give.n <- function(x){
return(c(y = mean(fivenum(x)[3:4]), label = length(x)))
}