错误:ggplot2中的stat_count() [英] Error: stat_count() in ggplot2

查看:328
本文介绍了错误:ggplot2中的stat_count()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的许多程序中,我一直使用ggplot2来渲染图表。我已经将它们加载到shinyapps.io上,它们工作得很好。但是,当我尝试在我的机器上运行该程序时,出现以下错误:

 错误:stat_count()必须不能用于美学。 

以下是示例代码:

  ggplot(hashtg,aes(x = reorder(hashtag,Freq),y = Freq,fill = hashtag))+ geom_bar(stat =identity)+ 
geom_bar (aes(label = Freq),hjust = 1,color =white)

实际的代码有很多条形图的参数,例如标题,主题&注释,但我想他们不会妨碍输出。我使用的是汇总数据,其中代码中的 Freq 是特定术语的频率。当我寻求帮助时,我重复得到了对条形图使用 stat =identity的指示。



任何帮助都将不胜感激。



会话信息如下:

  R版本3.2.0(2015-04-16)
平台:x86_64-apple-darwin13.4.0(64位)
运行于:OS X 10.10.3(Yosemite)

语言环境:
[1] en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

附加软件包:
[1] stats graphics grDevices utils datasets methods base

其他附加软件包:
[1] wordcloud_2.5 RColorBrewer_1.1-2 SnowballC_0.5.1 ggplot2_2.0.0 plyr_1.8.3
[6] chron_2.3-47 RCurl_1.95-4.7 bitops_1.0-6 ROAuth_0.9.6 RJSONIO_1。 3-0
[11] twitteR_1.1.9 base64enc_0.1-3 tm_0.6-2 NLP_0.1-8 stringr_1.0.0
[16] shinydashboard_0.5.1 shinyIncubator_0.2.2 shiny_0.12.2

通过命名空间加载(并且未附加):
[1] Rcpp_0.12.1 ls_3.2.0 digest_0.6.8 bit_1.1-12 jsonlite_0.9.17 gtable_0.1.2
[7] DBI_0.3.1 rstudioapi_0.3.1 curl_0.9.3 parallel_3.2.0 httr_1.0.0 bit64_0.9-5
[13 ] grid_3.2.0 R6_2.1.1 magrittr_1.5 scales_0.3.0 htmltools_0.2.6 colorspace_1.2-6
[19] mime_0.4 xtable_1.7-4 httpuv_1.3.3 labeling_0.3 stringi_0.5-5 munsell_0.4.2
[25] slam_0.1-32 rjson_0.2.15 rstudio_0.98.1103

重申,

解决方案

我发现这个链接( http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html
与此错误的解决方案。


您已经总结了 geom_bar()的列。您需要将 stat = stat_count (默认在 geom_bar())中更改为标识。

  + geom_bar(stat =identity)

或者,您可以使用 geom_col


In many of my programs I have been using ggplot2 to render charts. I have loaded them on shinyapps.io and they are working absolutely fine. However, when I try to run the program on my machine, i am getting the following error:

Error : stat_count() must not be used with a y aesthetic.

The following is the example code:

ggplot(hashtg, aes(x=reorder(hashtag, Freq), y = Freq, fill = hashtag)) + geom_bar(stat="identity") +
                geom_bar(width = 0.4) + xlab("Hashtags Used") + ylab("Number of responses") + 
                geom_text(aes(label=Freq), hjust = 1, colour = "white" )

The actual code has many arguments of bar graph such as title, theme & annotation, but I guess they would not hamper the output. I am using aggregated data where Freq in the code is the frequency of a particular term. When I searched for help, I repeated get instructions to use stat = "identity" for a bar plot.

Any help would be highly appreciated.

The session info is as follows:

R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] wordcloud_2.5        RColorBrewer_1.1-2   SnowballC_0.5.1      ggplot2_2.0.0        plyr_1.8.3          
 [6] chron_2.3-47         RCurl_1.95-4.7       bitops_1.0-6         ROAuth_0.9.6         RJSONIO_1.3-0       
[11] twitteR_1.1.9        base64enc_0.1-3      tm_0.6-2             NLP_0.1-8            stringr_1.0.0       
[16] shinydashboard_0.5.1 shinyIncubator_0.2.2 shiny_0.12.2        

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.1       tools_3.2.0       digest_0.6.8      bit_1.1-12        jsonlite_0.9.17   gtable_0.1.2     
 [7] DBI_0.3.1         rstudioapi_0.3.1  curl_0.9.3        parallel_3.2.0    httr_1.0.0        bit64_0.9-5      
[13] grid_3.2.0        R6_2.1.1          magrittr_1.5      scales_0.3.0      htmltools_0.2.6   colorspace_1.2-6 
[19] mime_0.4          xtable_1.7-4      httpuv_1.3.3      labeling_0.3      stringi_0.5-5     munsell_0.4.2    
[25] slam_0.1-32       rjson_0.2.15      rstudio_0.98.1103

To reiterate, the same code works without a trouble in shinyapps.io.

解决方案

I found this link (http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html) with the solution for this error.

The column you are trying to summarize with geom_bar() is already summarized. You need to change stat=stat_count (default in geom_bar()) to identity.

+ geom_bar(stat="identity")

Or, you can use geom_col instead.

这篇关于错误:ggplot2中的stat_count()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆