0

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?

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")

enter image description here

user1744318
  • 99
  • 1
  • 7
  • 1
    Can you think of a plausible reason for why it would be ordered the way it was ordered? And do you have reason to believe that the order in the data frame should/does impact the order in the graph? – Dason Dec 21 '14 at 03:33
  • 1
    @Dason Very Socratic way of helping with homework. Kudos :) – zero323 Dec 21 '14 at 03:36
  • 1
    @Dason Looks like it is in alphabetical order. I suppose I should be asking how to get it into order by value. – user1744318 Dec 21 '14 at 03:36
  • @user1744318 Checking what is the class of EVTYPE, and how to order this type should give you an answer. – zero323 Dec 21 '14 at 04:04
  • Shirley, this must have been asked and answered for ggplot2::geom_bar? – IRTFM Dec 21 '14 at 04:08
  • @BondedDust I found this so far and find it incomprehensible: http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph – user1744318 Dec 21 '14 at 04:11
  • Seemed perfectly clear to me. The default for ordering is factor levels. – IRTFM Dec 21 '14 at 04:20
  • @BondedDust Do I have to convert numerical values to factors? – user1744318 Dec 21 '14 at 04:23
  • That's what reorder does. Read its help page. – IRTFM Dec 21 '14 at 04:30
  • @BondedDust This isn't a duplicate question to the one you list. That other question does not have numeric values in the data table. I know they count them or something in their analysis, but as a beginner I need a simpler case than the other question provides. – user1744318 Dec 21 '14 at 04:47
  • @BondedDust Here, make it a duplicate of this: http://stackoverflow.com/questions/23858316/reordering-bars-in-qplot?rq=1 – user1744318 Dec 21 '14 at 04:50
  • We cannot modify a dup vote. I'm pretty sure I can find 10 more on this topic – IRTFM Dec 21 '14 at 05:24

1 Answers1

1

You could use the reorder() function

EventsByDeaths <- transform(EventsByDeaths, EVTYPE = reorder(EVTYPE, -deaths))

Then your original qplot call should work as desired. Hope this helps!

user51855
  • 369
  • 1
  • 6
  • This WORKS! Any idea why transform works but arrange doesn't? – user1744318 Dec 21 '14 at 04:13
  • I think `arrange` changes the order of the rows in the dataframe and `transform` changes the order of the levels of the factor. The order of the rows doesn't matter to `ggplot`. – user51855 Dec 21 '14 at 13:40
  • No. `transform` does not change anything. It only creates an environment where the expression gets evaluated. The "change" is made by the combination of `reorder` (which returns a new factor object from the numeric input, and `<-` which finally commits the "change". – IRTFM Dec 21 '14 at 22:28