Hi all: I'm attempting to create a faceted plot with ggplot2 and I'm having issues with a factor's order that is used to define the facet_grid(). The factor (named total.density) has three levels - 8, 16, and 32 - and I would like them presented in that order. Running order(levels(total.density)) yields the incorrect order of the facet grid - 2 3 1, corresponding with 16, 32, and 8. I have attempted correcting the order with the following solutions (of course, not run at once): #total.density <- relevel(total.density, '8') #total.density <- as.numeric(levels(total.density)[total.density]) #total.density <- factor(total.density, levels = c('8','16','32')) #total.density <- factor(total.density, levels levels(total.density)[c(3,1,2)]) #library(gregmisc) #total.density <- reorder.factor(total.density, c('8', '16', '32'), order = T) The data are as follows: total.density <- c(8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32) I'm running R 2.14.2 with all packages up-to-date as of 21.3.2012. Any help would be greatly appreciated. - Justin Montemarano Graduate Student Kent State University - Biological Sciences http://www.montegraphia.com [[alternative HTML version deleted]]
Is this what you need?> total.density <-+ c(8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32)> total.density <- factor(total.density, levels=c(8, 16, 32), ordered=TRUE) > str(total.density)Ord.factor w/ 3 levels "8"<"16"<"32": 1 1 1 1 1 1 1 1 1 1 ... On Wed, Mar 21, 2012 at 10:26 AM, Justin Montemarano <jmontema at kent.edu> wrote:> Hi all: > > I'm attempting to create a faceted plot with ggplot2 and I'm having issues > with a factor's order that is used to define the facet_grid(). > > The factor (named total.density) has three levels - 8, 16, and 32 - and I > would like them presented in that order. ?Running > order(levels(total.density)) yields the incorrect order of the facet grid - > 2 3 1, corresponding with 16, 32, and 8. > > I have attempted correcting the order with the following solutions (of > course, not run at once): > > #total.density <- relevel(total.density, '8') > ? ? ? ?#total.density <- as.numeric(levels(total.density)[total.density]) > ? ? ? ?#total.density <- factor(total.density, levels = c('8','16','32')) > ? ? ? ?#total.density <- factor(total.density, levels > levels(total.density)[c(3,1,2)]) > ? ? ? ?#library(gregmisc) > ? ? ? ?#total.density <- reorder.factor(total.density, c('8', '16', '32'), > order = T) > > The data are as follows: > > total.density <- > c(8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32) > > I'm running R 2.14.2 with all packages up-to-date as of 21.3.2012. > > Any help would be greatly appreciated. > > --- Sarah Goslee http://www.functionaldiversity.org
Actually I've try that too, Sarah.... The test is to run order(levels(total.density)), which I need to be 1 2 3, not 2 3 1, and your solution still gives me 2 3 1. I also don't know how to reply to this thread with the previous message below... - Justin Montemarano Graduate Student Kent State University - Biological Sciences http://www.montegraphia.com [[alternative HTML version deleted]]
I think I understand, but I believe my original interest is in the order of levels(total.density), since ggplot appears to be using that to order the facets. Thus, I'm still getting three graphs, ordered (and displayed as) 16 to 32 to 8, rather than the more intuitive, 8 to 16 to 32. I'm sorry if I wasn't clear and/or I've missed your message. - Justin Montemarano Graduate Student Kent State University - Biological Sciences http://www.montegraphia.com [[alternative HTML version deleted]]