3

I have a dataset like the following one, and I have about 1 million rows like this:

orderid     prodid      priceperitem    date        category_eng    
3010419     2   62420       18.90   2014-10-09      roll toliet paper   

I am currently plotting a plot of these products scatterplots using priceperitem as y-axis and date as x-axis. I have also ordered these rows based on these products' coefficient of variation of their prices throughout time. I have summarized these results in another dataset like the following one:

prodid       mean        count           sd           cv
424657       12.7124    5541.0000       10.239       80.54999886
158726193    23.7751    1231.0000       17.7567      74.68621596

And I have used the following code to get the scatterplots of many products at the same time:

ggplot(Roll50.last, aes(x=date, y=priceperitem)) + geom_point() + facet_wrap(~prodid)

But I want to order the plots based on these products' CV that I have summarized in another data.frame. I am wondering if there is a way that can allow me to specify that I want to order the panel plots by the order of a value in another dataframe.

Below is a small sample data. Basically, the idea is to get the different products' price cv which = s.d./mean. And I want to plot these scatterplot of these products in order of cv from highest to lowest.

#generate reproducible example
id = c(1: 3)
date = seq(as.Date("2011-3-1"), as.Date("2011-6-8"), by="days")

id = rep(c(1,2,3,4),each=25)
set.seed(01)
id = sample(id)

price = rep(c(1:20), each = 5)
price = sample(price)
data = data.frame(date, id, price)
lll
  • 1,049
  • 2
  • 13
  • 39

1 Answers1

0

You can turn prodid into a factor and set the order of the factor categories to be based on the size of the coefficient of variation. For example, let's assume your data frame with the cv values is called cv. Then, to order prodid by the values of cv:

Roll50.last$prodid = factor(Roll50.last$prodid, 
                            levels = cv$prodid[order(cv$cv, decreasing=TRUE)])

Now, when you plot the data, the prodid facets will be ordered by decreasing size of cv.

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • I tried the following two lines of codes, but the ggplot is not giving me a panel of plots but rather only one plot. `Roll50.mid6$prodid = factor(Roll50.mid6$prodid, levels = mid115to120$prodid[order(mid115to120$cv, decreasing=TRUE), ])` `ggplot(Roll50.mid6, aes(x=date, y=priceperitem)) + geom_point() + facet_wrap(~prodid)` Could you tell me what might be the reason causing this? – lll Jun 15 '15 at 00:58
  • Hard to know without seeing a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that would allow me to run your code, reproduce your problem and then (hopefully) solve it. A minimal reproducible example means a subset of your data, code creating the `cv` data frame from that data sample, and code to create the plot (plus any explanatory text needed to understand your issue). – eipi10 Jun 15 '15 at 01:13
  • To post a data sample, just paste into your question (not into a comment) the output of `dput(Roll50.last[1:10,])` or whatever small portion of the data is sufficient to demonstrate your problem. – eipi10 Jun 15 '15 at 01:13
  • added a small sample. Hope this can help illustrate my point. – lll Jun 15 '15 at 02:01
  • I fixed an error (the last comma in my original code shouldn't have been there). Try now. (This one of many reasons to include a reproducible example in your question. If we can't test our code, we can't be sure it will work.) – eipi10 Jun 15 '15 at 15:28
  • I tried the new line of code. it still only gives me one big confusing plot rather than a panel of plots. Could you kindly tell me why – lll Jun 15 '15 at 19:13
  • Your example still isn't reproducible. You've provided sample data, but you still need to supply the code that goes from that sample data to the plot that doesn't look the way you want it to. Then I (or others) can work from that and try to solve your problem. – eipi10 Jun 15 '15 at 19:26