重新排列ggplot2中的列,错误:应使用`aes或`aes _`创建映射

问题描述

我正在尝试对ggplot中的列进行重新排序,以从最小到最大,但始终收到此错误消息

countries<-c("Australia","Austria","Belgium","Canada","Denmark","France","Germany","Italy","Luxembourg","Netherlands","norway","New Zealand","Spain","Sweden","United Kingdom","USA")
cost<-c(1221,711,184,250,6658,51,1118,880,919,2500,1558,2452,103,920,1460,401)
citcost<-data.frame(countries,cost)

citcost %>% ggplot(citcost,aes(x=reorder(countries,cost),y=cost,fill=countries)) + geom_bar(stat="identity") + theme(axis.text.x = element_text(angle = 45,hjust = 1)) + scale_colour_brewer()

错误:应该使用aes() or aes _()`创建映射。

解决方法

问题出在NotifyController的参数上。在这里,第二个参数是ggplot,它期望mapping,而我们已经为数据对象提供了aes,而数据已经通过ggplot(citcost传递了。因此,OP代码中的cicost %>% ggplot调用是

ggplot

应该是

ggplot(data = citcost,mapping = citcost,...)

-输出

enter image description here