GGPLOT2 - 译注(标签)与位置geom_boxplot =闪避 [英] ggplot2 - annotate (labelling) geom_boxplot with position = dodge
本文介绍了GGPLOT2 - 译注(标签)与位置geom_boxplot =闪避的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能够使用时,填充美学(或避开在x轴上的样品)中加入标签(在这种情况下,样本尺寸)接近箱线图
它工作正常的最常用的数据集(mtcars)和示例
库(GGPLOT2)fun_length< - 功能(X){
回报(data.frame(Y =位数(X),标签= paste0(N =,长度(X))))
}ggplot(mtcars,AES(因子(缸),MPG))+
geom_boxplot()+
stat_summary(AES(X =系数(CYL))
fun.data = fun_length,GEOM =TEXT,
vjust = + 1,大小= 4)
库(plyr)
ddply(mtcars。(CYL),总结,标签=长度(MPG)) CYL标签
1 4 11
2 6 7
3 8 14
但我不能添加相同的标签,这个版本,现在显示每个VS级别的样本量。
ggplot(mtcars,AES(因子(缸),MPG))+
geom_boxplot(AES(填充系数=(VS)))+
stat_summary(AES(X =系数(CYL))
fun.data = fun_length,GEOM =文本)ddply(mtcars。(CYL,VS),总结,标签=长度(MPG)) CYL VS标签
1 4 0 1
2 4 1 10
3 6 0 3
4 6 1 4
5 8 0 14
任何帮助将受到欢迎。先谢谢了。
解决方案
ggplot(mtcars,AES(因子(缸),MPG))+
geom_boxplot(AES(填充系数=(VS)),位置= position_dodge(0.9))+
stat_summary(AES(X =因子(缸),填充因子=(VS)),位置= position_dodge(0.9),
fun.data = fun_length,GEOM =TEXT,
vjust = + 1,大小= 4)
I'm not being able to add labels (sample size in this case) close to the boxplot when using fill aesthetics (or dodging the samples at x axis).
it works fine for the most used dataset (mtcars) and example
library(ggplot2)
fun_length <- function(x){
return(data.frame(y=median(x),label= paste0("n=", length(x))))
}
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
stat_summary(aes(x = factor(cyl)),
fun.data = fun_length, geom = "text",
vjust = +1, size = 4)
library(plyr)
ddply(mtcars, .(cyl), summarise, label = length(mpg))
cyl label
1 4 11
2 6 7
3 8 14
But I can't add the same labels for this version, now showing sample sizes for each vs level.
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(vs))) +
stat_summary(aes(x=factor(cyl)),
fun.data = fun_length, geom = "text")
ddply(mtcars, .(cyl, vs), summarise, label = length(mpg))
cyl vs label
1 4 0 1
2 4 1 10
3 6 0 3
4 6 1 4
5 8 0 14
Any help will be welcome. Thanks in advance.
解决方案
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(vs)), position=position_dodge(.9)) +
stat_summary(aes(x=factor(cyl), fill = factor(vs)), position=position_dodge(.9),
fun.data = fun_length, geom = "text",
vjust = +1, size = 4)
这篇关于GGPLOT2 - 译注(标签)与位置geom_boxplot =闪避的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文