Quantcast

Unable to specify order of a factor

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Unable to specify order of a factor

Justin Montemarano
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Sarah Goslee
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 <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Justin Montemarano
In reply to this post by Justin Montemarano
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Sarah Goslee
Ah, you're missing something crucial:

> levels(total.density)
[1] "8"  "16" "32"

is giving you the *labels* of the factor, as *strings*, and what you
get if you use order() on them has nothing to do with the order of the
factor levels, and everything to do with the string sort order for
your locale.

> str(levels(total.density))
 chr [1:3] "8" "16" "32"

The factor levels themselves are in the order you specified.

> 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:50 AM, Justin Montemarano <[hidden email]> wrote:

> 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
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Justin Montemarano
In reply to this post by Justin Montemarano
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Ista Zahn
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Justin Montemarano
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.
-
Justin Montemarano
Graduate Student
Kent State University - Biological Sciences

http://www.montegraphia.com


On Wed, Mar 21, 2012 at 11:42 AM, Ista Zahn <[hidden email]> 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 <[hidden email]>
> 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]]
> >
> > ______________________________________________
> > [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Ista Zahn
On Wed, Mar 21, 2012 at 12:00 PM, Justin Montemarano <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
>> 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]]
>> >
>> > ______________________________________________
>> > [hidden email] 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.
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Michael Weylandt
You'll also want to use dput() to send us an exact encoding of your
data when making that reproducible example: there might be something
subtle at play here that print methods won't show.

Michael

On Wed, Mar 21, 2012 at 12:28 PM, Ista Zahn <[hidden email]> wrote:

> On Wed, Mar 21, 2012 at 12:00 PM, Justin Montemarano <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
>>> 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]]
>>> >
>>> > ______________________________________________
>>> > [hidden email] 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.
>>
>>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

Justin Montemarano
Hi all:

I've got it... it appears that total.density was also defined in two
separate data frames (se.predict.data and dc.predict.data) with levels
order 16, 32, 8. Using relevel(), I moved 8 to the first position and it's
solved the plotting problem.

Ista's 'minimal' reproducible code request prompted me to discover my
error; thanks all.

-
Justin Montemarano
Graduate Student
Kent State University - Biological Sciences

http://www.montegraphia.com


On Wed, Mar 21, 2012 at 12:42 PM, R. Michael Weylandt <
[hidden email]> wrote:

> You'll also want to use dput() to send us an exact encoding of your
> data when making that reproducible example: there might be something
> subtle at play here that print methods won't show.
>
> Michael
>
> On Wed, Mar 21, 2012 at 12:28 PM, Ista Zahn <[hidden email]> wrote:
> > On Wed, Mar 21, 2012 at 12:00 PM, Justin Montemarano <[hidden email]>
> 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 <[hidden email]> 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 <
> [hidden email]>
> >>> 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]]
> >>> >
> >>> > ______________________________________________
> >>> > [hidden email] 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.
> >>
> >>
> >
> > ______________________________________________
> > [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Unable to specify order of a factor

David Winsemius

On Mar 21, 2012, at 1:07 PM, Justin Montemarano wrote:

> Hi all:
>
> I've got it... it appears that total.density was also defined in two
> separate data frames (se.predict.data and dc.predict.data) with levels
> order 16, 32, 8. Using relevel(), I moved 8 to the first position  
> and it's
> solved the plotting problem.
>
> Ista's 'minimal' reproducible code request prompted me to discover my
> error; thanks all.

I've had the experience in the last few years that almost all of my  
questions to Rhelp have needed to be peacefully euthanized after being  
subjected to the rack of hammering into a "reproducible" condition.

--
David.

>
> -
> Justin Montemarano
> Graduate Student
> Kent State University - Biological Sciences
>
> http://www.montegraphia.com
>
>
> On Wed, Mar 21, 2012 at 12:42 PM, R. Michael Weylandt <
> [hidden email]> wrote:
>
>> You'll also want to use dput() to send us an exact encoding of your
>> data when making that reproducible example: there might be something
>> subtle at play here that print methods won't show.
>>
>> Michael
>>
>> On Wed, Mar 21, 2012 at 12:28 PM, Ista Zahn <[hidden email]>  
>> wrote:
>>> On Wed, Mar 21, 2012 at 12:00 PM, Justin Montemarano <[hidden email]
>>> >
>> 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 <[hidden email]>  
>>>> 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 <
>> [hidden email]>
>>>>> 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]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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.
>>>>
>>>>
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Loading...