[R] Unable to specify order of a factor

Ista Zahn istazahn at gmail.com
Wed Mar 21 17:28:08 CET 2012


On Wed, Mar 21, 2012 at 12:00 PM, Justin Montemarano <jmontema at kent.edu> wrote:
> Ista:
>
> Your attached code did work for me; moreover, the facets were presented in
> the desired order with facet_wrap() and facet_grid(), which is what I'm
> using because I have a second factor used in facet_grid().
>
> Still, my plots with total.density as a facet are coming out in 16, 32, 8,
> and I'm not seeing why.  Below is my plot code -
>
>> ggplot(ag.tab[ag.tab$plant.sp == 'EC',], aes(x = days.out, y =
>> per.remain)) + facet_grid(total.density ~ prop.ec) +
>>     #add point and error bar data
>>     theme_set(theme_bw()) +
>>     geom_point() + geom_errorbar(aes(ymin = per.remain - se, ymax =
>> per.remain + se), width = 3) +
>>     #add predicted model data
>>     geom_line(data = se.predict.data[se.predict.data$plant.sp == 'EC',],
>> aes(x = x.values, y = predicted.values), colour = c('red')) +
>>     geom_line(data = dc.predict.data[dc.predict.data$plant.sp == 'EC',],
>> aes(x = x.values, y = predicted.values), colour = c('blue'), linetype =
>> c('dashed')) +
>>
>>     xlab('Day') + ylab('Percent Mass Remaining') + opts(panel.grid.major =
>> theme_blank(), panel.grid.minor = theme_blank())
>
> Is there anything odd about it that might be producing the odd ordering
> problem?  FYI, avoiding subsetting ag.tab doesn't do the trick.

I don't know. Please create a minimal example that isolates the
problem. You can start with

levels(ag.tab$total.density)

ggplot(ag.tab[ag.tab$plant.sp == 'EC',], aes(x = days.out, y = per.remain)) +
    facet_grid(total.density ~ prop.ec) +
    geom_point()

Best,
Ista

> -
> Justin Montemarano
> Graduate Student
> Kent State University - Biological Sciences
>
> http://www.montegraphia.com
>
>
> On Wed, Mar 21, 2012 at 11:42 AM, Ista Zahn <istazahn at gmail.com> wrote:
>>
>> Hi Justin,
>>
>> this gives the correct order (8, 16, 32) on my machine:
>>
>> 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)
>>
>> order(levels(total.density))
>>
>> dat <- data.frame(td = total.density, v1 = rnorm(1:length(total.density)))
>>
>> ggplot(dat, aes(x = v1)) +
>>  geom_density() +
>>  facet_wrap(~td)
>>
>> Does it work for you? If yes, then you need to tell us what you're
>> doing that is different from this example. If no, please give use the
>> output of sessionInfo().
>>
>> best,
>> Ista
>>
>> On Wed, Mar 21, 2012 at 11:16 AM, Justin Montemarano <jmontema at kent.edu>
>> wrote:
>> > 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]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list