I have data comparing sales on Black Friday in the States and Boxing Day in Canada. For each shopping holiday, I have a dataframe with popular items, which I've combined. I also have an approximate percentage of advertising that differs with each holiday. The dataframe looks like this:
+---------+----------+-----------+---------+
| Item | AdBudget | Holiday | AmtSold |
+---------+----------+-----------+---------+
| TV | $1000 | BlackFri | 1800 |
| Laptop | $2300 | BlackFri | 1400 |
| DVDs | $100 | BlackFri | 300 |
| Blender | $200 | BlackFri | 50 |
| TV | $1200 | BoxingDay | 1000 |
| Laptop | $1200 | BoxingDay | 850 |
| Blender | $500 | BoxingDay | 76 |
| DVDs | $400 | BoxingDay | 35 |
+---------+----------+-----------+---------+
The automatic ordering of the Item factor is alphabetical (ie, Blender, DVDs, Laptop, TV). I want the ordering to be (TV, Laptop, Blender, DVDs).
I want to order the ITEM factor by AmtSold on Boxing Day. Can you help me use reorder to do this?
Thank you.
EDIT
A couple of commenters have asked me what the output dataframe will look like. To clarify, I don't know and don't care about whether or not I reorder the dataframe. I'm putting this data into ggplot2 and making a bar graph. Like so:
ggplot(dataframe,aes(x=Item,y=AmtSold))
+geom_bar(stat="identity")
+ facet_grid(Holiday ~ .)
As well illustrated in the answer to this question about ggplot2 bar graphs on factors ggplot2 orders the factor variable based on alphabetical (or perhaps alphanumerical?) ordering.
In my case, that means that my bar graph will have bars in the following order: Blender, DVDS, Laptop, TV
I want the bars in the order of amount sold (highest to lowest) on Boxing day, this order: TV, Laptop, Blender, DVDs
One possibility is do this explicitly, as such
dataframe$Item <- factor(dataframe$Item, levels = c("TV","Laptop","Blender","DVDs"))
But I'd like to do it programatically.