汇总统计对ggplot上方条形图酒吧注释

编程入门 行业动态 更新时间:2024-10-09 20:21:10
本文介绍了汇总统计对ggplot上方条形图酒吧注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建一个函数,数据帧,x和y的变量和一组变量作为参数它由组变量x变量和小面的水平输出一个条形图。我想地方重新presents y的平均栏上方的文本标签。我的工作的功能,直到我我的功能如下:

I created a function that takes dataframe, x and y variables and a group variable as arguments it outputs a barchart by levels of the x variable and facet by the group variable. I want to place a text label above the bar that represents the average of y. My function worked until I My function is as follows:

XTABAR<- function(DS,xcat,yvar,group,formet=percent,color1=orange,color2=blue,...){ library(ggplot2) library(dplyr) library(scales) localenv<-environment() gg<-data.frame(DS,x=DS[,xcat],y=DS[,yvar],z=DS[,group] ) gg = transform(summarise(group_by(gg, x), sumvar= mean(y))) G<-ggplot(gg,aes(x=factor(x),y=y, fill=factor(x)))+stat_summary(fun.y=mean,geom="bar")+facet_wrap(~z)+scale_y_continuous(labels = formet)+xlab(xcat)+ylab(yvar)+scale_fill_manual(values=c(color1,color2)) #H<-G+geom_text(data=gg,aes(label=sumvar,x=factor(x),y=y), position = position_dodge(width = 0.8), vjust=-.6) H<-G+stat_summary(fun.y = mean, geom="text", aes(label=sumvars), vjust = 0) #G+scale_fill_manual(values=c("orange","blue"))+ylab("Boo")+xlab("Foo")+scale_y_continuous(labels = met) print(H) } # The arguments are dataframe (DS); x variable (xcat) y variable (yvar); grouping variable (group) y scale format (formet); and colors for bars

我得到的错误是:

The error I get is the following:

Error in layout_base(data, vars, drop = drop) : At least one layer must contain all variables used for facetting

因此​​,谁能帮助我的code或理解,这样我可以学习如何获取栏上总结统计逻辑?谢谢

So can anyone help me with the code or understand the logic so that I can learn how to get summary stats above the bar? Thank you

推荐答案

只有一个数据集的一个例子:

An example with just one dataset:

# create a dataset set.seed(123) df <- data.frame(xcol=sample(1:3, 100, replace=TRUE), ycol = rnorm(100, 5, 2), catg=letters[1:5]) # summarising the data require(plyr) df2 <- ddply(df, .(xcol, catg), summarise, ave=mean(ycol)) # creating a plot ggplot(df2, aes(x=factor(xcol),y=ave, fill=factor(xcol))) + geom_bar(stat="identity") + geom_text(aes(label=round(ave,2)), vjust = -0.5) + scale_y_continuous(limits=c(0,7), expand = c(0, 0)) + guides(fill=FALSE) + facet_wrap(~catg) + theme_bw()

这会导致:

which results in:

更多推荐

汇总统计对ggplot上方条形图酒吧注释

本文发布于:2023-11-28 20:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   酒吧   条形图   ggplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!