2

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.

Community
  • 1
  • 1
CHN
  • 387
  • 1
  • 2
  • 7
  • You have not done your work to ask a good R question. It should include a simple self contained example. – Robert Hijmans Sep 27 '15 at 02:47
  • Thanks for feedback - edits done – CHN Sep 27 '15 at 02:55
  • Can you illustrate what this reordering will do your example data.frame? – under_the_sea_salad Sep 27 '15 at 03:30
  • good question. I'm not concerned about how it will change the dataframe. I'm effectively having this problem: http://stackoverflow.com/questions/3253641/how-to-change-the-order-of-a-discrete-x-scale-in-ggplot – CHN Sep 27 '15 at 03:34
  • I understand the answer, but not well enough to adapt the reordering part to my particular needs – CHN Sep 27 '15 at 03:35
  • Create a data frame that reflects the desired output for the example and add it to your question. – Pierre L Sep 27 '15 at 03:42
  • @PierreLafortune I don't think the desired output will necessarily change my dataframe. Did you check out my answer to bourbaki4481472's question? – CHN Sep 27 '15 at 03:48
  • In future, use the output of `dput(mydata)` to post your sample data. – jlhoward Sep 27 '15 at 04:28
  • You can simply reorder the column inside the data frame. `Df$Item <- factor(Df$Item, levels = c("TV", "Laptop", "Blender", "DVDs")` – Veera Sep 27 '15 at 09:12
  • @jihoward, the dput tip is great, thank you. – CHN Sep 27 '15 at 12:07

1 Answers1

2
lvls <- as.character(df$Item[df$Holiday=="BoxingDay"])[order(df$AmtSold[df$Holiday=="BoxingDay"])]
df$Item <- factor(df$Item,levels=lvls)
library(ggplot2)
ggplot(df,aes(x=Item,y=AmtSold))+
  geom_bar(aes(fill=Holiday), stat="identity") +
  facet_grid(Holiday ~ .)

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thank you for your answer. It's true that it works for the example I've illustrated, however the dataset I'm working with actually has hundreds of items that I've removed for simplicity. It does not make sense to do this manually. I apologize for the confusion. – CHN Sep 27 '15 at 04:02
  • Then what is the order you want?? It's not the order of the rows. – jlhoward Sep 27 '15 at 04:04
  • @jihoward this is GREAT! It's exactly what I'm looking for. Thank you. – CHN Sep 27 '15 at 12:06