This question already has an answer here:
这个问题在这里已有答案:
- Order Bars in ggplot2 bar graph 10 answers
- ggplot2条形图中的订单栏10个答案
I use the arrange function to put my data frame in order by deaths, but when I try to do a bargraph of the top 5, they are in alphabetical order. How do I get them into order by value? Do I need to use ggplot?
我使用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")
1 个解决方案
#1
1
You could use the reorder()
function
您可以使用reorder()函数
EventsByDeaths <- transform(EventsByDeaths, EVTYPE = reorder(EVTYPE, -deaths))
Then your original qplot
call should work as desired. Hope this helps!
然后你的原始qplot调用应该按照需要工作。希望这可以帮助!
#1
1
You could use the reorder()
function
您可以使用reorder()函数
EventsByDeaths <- transform(EventsByDeaths, EVTYPE = reorder(EVTYPE, -deaths))
Then your original qplot
call should work as desired. Hope this helps!
然后你的原始qplot调用应该按照需要工作。希望这可以帮助!