Using qplot + facet_wrap, where the plots are ordered by one variable and labeled by another

I have a dataset as follows:

Date         State     Count   mean
1994-01-05   Alabama   408     581.333
1994-01-06   Alabama   784     581.333
1994-02-08   Alabama   552     581.333
1994-01-05   Arizona   385     759.666
1994-01-06   Arizona   1845    759.666
1994-02-08   Arizona   49      759.666
1994-01-05   Alaska    1067    3224.666
1994-01-06   Alaska    36      3224.666
1994-02-08   Alaska    8571    3224.666

where there are "counts" for an ordered set of dates for each of the fifty states. The mean of the counts for each state is how the states are ordered.

I would like to make a plot with subplots of the 50 states, with the count as the y-axis and the date as the x-axis. I would like the subplots to be arranged by increasing mean counts (top left --> bottom right). However, I would like the title of each subplot to include the State (whether or not its mean value is also included does not matter much).

I tried:

qplot(Date, Count, data =dataset) + facet_wrap(~mean,nrow=7) 

This led to an almost perfect plot. It consists of the fifty subplots, with the correct axes, and in increasing order of their mean counts. However, the title of each subplot is not the state, but the mean of the state.

Any advice would be appreciated!!


At the moment you cannot pass expressions to facet_wrap, so one approach to create a factor column with the levels in the order you wish. The idiomatic R approach is to use reorder

# create factor
dataset$State_lab <- with(dataset, reorder(x=State,X=mean))
# your plot
qplot(Date,Count,data=dataset) + facet_wrap(~State_lab)

Need Your Help

ng-repeat not working with set data structure

angularjs set angularjs-ng-repeat ng-repeat

I'm trying to use ng-repeat with a field that is a Set in javascript I tried use values(), keys(), entries() metho...

How do I create a list of all occurances of a string which ends with a specific file type found in a text file?

python arrays parsing text find

I'm trying to extract all of the links to image files from a text file. All of the image files end in either .jpg or .gif, and are surrounded by quotation marks. I want to find the first occurren...