我使用arrange函数按死亡顺序放置我的数据框,但是当我尝试做前5的条形图时,它们按字母顺序排列。如何按价值使它们按顺序排列?我需要使用ggplot吗?
library(dplyr)
library(ggplot2)
EventsByDeaths <- arrange(SumByEvent, desc(deaths))
> head(EventsByDeaths, 10)
Source: local data frame [10 x 3]
EVTYPE deaths damage
1 TORNADO 4662 2584635.60
2 EXCESSIVE HEAT 1418 53.80
3 HEAT 708 277.00
4 LIGHTNING 569 338956.35
5 FLASH FLOOD 567 759870.68
6 TSTM WIND 474 1090728.50
7 FLOOD 270 358109.37
8 RIP CURRENTS 204 162.00
9 HIGH WIND 197 170981.81
10 HEAT WAVE 172 1269.25
qplot(y=deaths, x=EVTYPE, data=EventsByDeaths[1:5,], geom="bar", stat="identity")
答案 0 :(得分:1)
您可以使用reorder()
功能
EventsByDeaths <- transform(EventsByDeaths, EVTYPE = reorder(EVTYPE, -deaths))
然后您的原始qplot
来电应该可以正常工作。希望这有帮助!