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

原因
geom_bar()繪制的條形圖在單變量情況下,x軸對應(yīng)變量、y軸對應(yīng)變量的個(gè)數(shù)。
但如果是雙變量的話,直接添加變量就會(huì)報(bào)錯(cuò)。
geom_bar()想要在Y軸顯示X軸變量的個(gè)數(shù),而不是我們輸入的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()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容