Reordering columns in ggplot2, Error: Mapping should be created with `aes() or `aes_()`
Reordering columns in ggplot2, Error: Mapping should be created with `aes() or `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_()` 创建映射。
问题出在 ggplot
的参数上。在这里,第二个参数是 mapping
,它期望 aes
,而不是我们为数据对象提供 ggplot(citcost
),而数据已经通过 cicost %>% ggplot
传入。因此,OP 代码中的 ggplot
调用是
ggplot(data = citcost, mapping = citcost, ...)
而是
citcost %>%
ggplot(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()
-输出
我正在尝试对 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_()` 创建映射。
问题出在 ggplot
的参数上。在这里,第二个参数是 mapping
,它期望 aes
,而不是我们为数据对象提供 ggplot(citcost
),而数据已经通过 cicost %>% ggplot
传入。因此,OP 代码中的 ggplot
调用是
ggplot(data = citcost, mapping = citcost, ...)
而是
citcost %>%
ggplot(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()
-输出