原因
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()