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