r - How to order multiple plots through facet_wrap in ggplot2 -
i have dataset following one, , have 1 million rows this:
orderid prodid priceperitem date category_eng 3010419 2 62420 18.90 2014-10-09 roll toliet paper
i plotting plot of these products scatterplots using priceperitem
y-axis , date
x-axis. have ordered these rows based on these products' coefficient of variation of prices throughout time. have summarized these results in dataset 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 have used following code scatterplots of many products @ same time:
ggplot(roll50.last, aes(x=date, y=priceperitem)) + geom_point() + facet_wrap(~prodid)
but want order plots based on these products' cv have summarized in data.frame. wondering if there way can allow me specify want order panel plots order of value in dataframe.
below small sample data. basically, idea different products' price cv = s.d./mean. , want plot these scatterplot of these products in order of cv highest 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)
you can turn prodid
factor , set order of factor categories based on size of coefficient of variation. example, let's assume data frame cv values called cv
. then, order prodid
values of cv
:
roll50.last$prodid = factor(roll50.last$prodid, levels = cv$prodid[order(cv$cv, decreasing=true)])
now, when plot data, prodid
facets ordered decreasing size of cv
.
Comments
Post a Comment