已解决:ggplot2:Error: stat_count() can only have an x or y aesthetic [duplicate]

原因
geom_bar()绘制的条形图在单变量情况下,x轴对应变量、y轴对应变量的个数。
但如果是双变量的话,直接添加变量就会报错。
geom_bar()想要在Y轴显示X轴变量的个数,而不是我们输入的Y轴变量。
You need to include stat=identity, which is basically telling ggplot2 you will provide the y-values for the barplot, rather than counting the aggregate number of rows for each x value, which is the default stat=count

library(ggplot2)
png("plot4.png",height = 480,width = 480)
p <- ggplot(total_coal, aes(factor(year), Emissions))
p <- p + geom_bar(stat='identity',fill="red") +
  xlab("year") +
  ylab("total emissions") +
  ggtitle("total emissions of coal combustion in USA every year")
print(p)
dev.off()
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容