我有一个三向交互,我在美学部分创建了一个带有刻面和颜色参数的图形。 我想用重复次数、平均值或 tukey 字母来注释箱线图,但是当我使用颜色参数时我总是失败,因为标签都是居中的。 我怎样才能正确定位标签,使它们不会聚集在中心?
这里是一些示例数据:
location = rep(c("Bank", "Crop"),each=45)
treat = rep(c("field" ,"30","40"),30)
value = c(1:90)
storage = rep(c("fresh", "frozen"),45)
df = data.frame(location, treat, storage,value)
geom_text 的信息
test.dat <- df %>%
group_by(location,treat, storage) %>%
summarise(mean.h20 = round(mean(value,na.rm=TRUE),0),
total = n())
剧情:
ggplot(df, aes(x = storage, y=value, fill=treat))+
geom_boxplot(aes(fill=treat), position=position_dodge(.9))+
stat_summary(fun=mean, geom="point", shape=18, size=3, color="orange")+
labs(x="Condition", y="Value")+
facet_grid(~location)+
geom_text(data = test.dat, aes(y=0, label = mean.h20))
感谢您的帮助!
将
position_dodge
添加到geom_text
以及已经在geom_point
中使用它:
ggplot(df, aes(x = storage, y=value, fill=treat))+
geom_boxplot(aes(fill=treat), position=position_dodge(.9))+
stat_summary(fun=mean, geom="point", shape=18, size=3, color="orange")+
labs(x="Condition", y="Value")+
facet_grid(~location)+
geom_text(data = test.dat, aes(y=0, label = mean.h20),
position = position_dodge(width=0.9))