ggpubr: order of non-numeric x-axis items

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

ggpubr: order of non-numeric x-axis items

April Ettington
Hi,

When I use ggpubr with an x-axis utilizing descriptive categories (eg. bar
chart for different colors of car), it sorts all of the labels
alphabetically.  Is there a way to change this so it shows in the order I
want?

Thanks,

April

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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
|

Re: ggpubr: order of non-numeric x-axis items

Patrick (Malone Quantitative)
Try using relevel() to organize the categories in your factor in the
desired order. You may need to use relevel(as.factor()) .

On Sat, Oct 26, 2019 at 6:51 AM April Ettington
<[hidden email]> wrote:

>
> Hi,
>
> When I use ggpubr with an x-axis utilizing descriptive categories (eg. bar
> chart for different colors of car), it sorts all of the labels
> alphabetically.  Is there a way to change this so it shows in the order I
> want?
>
> Thanks,
>
> April
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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
|

Re: ggpubr: order of non-numeric x-axis items

Bert Gunter-2
No. relevel() only changes the order in one specific way. Use `levels<-`()
to reorder in a general way:

> z <- factor(rep(letters[3:1],2))
> z
[1] c b a c b a
Levels: a b c
> z <-relevel(z, ref = "c")
> z
[1] c b a c b a
Levels: c a b
> levels(z)<- c("c","b","a")
> z
[1] c a b c a b
Levels: c b a

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Oct 26, 2019 at 6:09 AM Patrick (Malone Quantitative) <
[hidden email]> wrote:

> Try using relevel() to organize the categories in your factor in the
> desired order. You may need to use relevel(as.factor()) .
>
> On Sat, Oct 26, 2019 at 6:51 AM April Ettington
> <[hidden email]> wrote:
> >
> > Hi,
> >
> > When I use ggpubr with an x-axis utilizing descriptive categories (eg.
> bar
> > chart for different colors of car), it sorts all of the labels
> > alphabetically.  Is there a way to change this so it shows in the order I
> > want?
> >
> > Thanks,
> >
> > April
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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 -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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
|

Re: ggpubr: order of non-numeric x-axis items

Deepayan Sarkar
On Sat, Oct 26, 2019 at 8:22 PM Bert Gunter <[hidden email]> wrote:

>
> No. relevel() only changes the order in one specific way. Use `levels<-`()
> to reorder in a general way:
>
> > z <- factor(rep(letters[3:1],2))
> > z
> [1] c b a c b a
> Levels: a b c
> > z <-relevel(z, ref = "c")
> > z
> [1] c b a c b a
> Levels: c a b
> > levels(z)<- c("c","b","a")
> > z
> [1] c a b c a b
> Levels: c b a

No, that changes the data, not just the order of the levels; "b" and
"a" have been switched.

You need some version of

> factor(z, levels = c("c", "a", "b"))
[1] c b a c b a

See also ?reorder for a useful way of reordering the levels,
especially in plots.

-Deepayan

> Cheers,
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Sat, Oct 26, 2019 at 6:09 AM Patrick (Malone Quantitative) <
> [hidden email]> wrote:
>
> > Try using relevel() to organize the categories in your factor in the
> > desired order. You may need to use relevel(as.factor()) .
> >
> > On Sat, Oct 26, 2019 at 6:51 AM April Ettington
> > <[hidden email]> wrote:
> > >
> > > Hi,
> > >
> > > When I use ggpubr with an x-axis utilizing descriptive categories (eg.
> > bar
> > > chart for different colors of car), it sorts all of the labels
> > > alphabetically.  Is there a way to change this so it shows in the order I
> > > want?
> > >
> > > Thanks,
> > >
> > > April
> > >
> > >         [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > 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 -- To UNSUBSCRIBE and more, see
> > 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 -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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
|

Re: ggpubr: order of non-numeric x-axis items

Patrick (Malone Quantitative)
Thanks, both of you.

On Sat, Oct 26, 2019 at 11:55 AM Deepayan Sarkar
<[hidden email]> wrote:

>
> On Sat, Oct 26, 2019 at 8:22 PM Bert Gunter <[hidden email]> wrote:
> >
> > No. relevel() only changes the order in one specific way. Use `levels<-`()
> > to reorder in a general way:
> >
> > > z <- factor(rep(letters[3:1],2))
> > > z
> > [1] c b a c b a
> > Levels: a b c
> > > z <-relevel(z, ref = "c")
> > > z
> > [1] c b a c b a
> > Levels: c a b
> > > levels(z)<- c("c","b","a")
> > > z
> > [1] c a b c a b
> > Levels: c b a
>
> No, that changes the data, not just the order of the levels; "b" and
> "a" have been switched.
>
> You need some version of
>
> > factor(z, levels = c("c", "a", "b"))
> [1] c b a c b a
>
> See also ?reorder for a useful way of reordering the levels,
> especially in plots.
>
> -Deepayan
>
> > Cheers,
> > Bert
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Sat, Oct 26, 2019 at 6:09 AM Patrick (Malone Quantitative) <
> > [hidden email]> wrote:
> >
> > > Try using relevel() to organize the categories in your factor in the
> > > desired order. You may need to use relevel(as.factor()) .
> > >
> > > On Sat, Oct 26, 2019 at 6:51 AM April Ettington
> > > <[hidden email]> wrote:
> > > >
> > > > Hi,
> > > >
> > > > When I use ggpubr with an x-axis utilizing descriptive categories (eg.
> > > bar
> > > > chart for different colors of car), it sorts all of the labels
> > > > alphabetically.  Is there a way to change this so it shows in the order I
> > > > want?
> > > >
> > > > Thanks,
> > > >
> > > > April
> > > >
> > > >         [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > > 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 -- To UNSUBSCRIBE and more, see
> > > 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 -- To UNSUBSCRIBE and more, see
> > 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 -- To UNSUBSCRIBE and more, see
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
|

Re: ggpubr: order of non-numeric x-axis items

Bert Gunter-2
In reply to this post by Deepayan Sarkar
Yes , thanks Deepayan. Darn! -- I've committed this error in the past,
also, but keep forgetting. Sigh... the aging brain.

-- Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Oct 26, 2019 at 8:55 AM Deepayan Sarkar <[hidden email]>
wrote:

> On Sat, Oct 26, 2019 at 8:22 PM Bert Gunter <[hidden email]>
> wrote:
> >
> > No. relevel() only changes the order in one specific way. Use
> `levels<-`()
> > to reorder in a general way:
> >
> > > z <- factor(rep(letters[3:1],2))
> > > z
> > [1] c b a c b a
> > Levels: a b c
> > > z <-relevel(z, ref = "c")
> > > z
> > [1] c b a c b a
> > Levels: c a b
> > > levels(z)<- c("c","b","a")
> > > z
> > [1] c a b c a b
> > Levels: c b a
>
> No, that changes the data, not just the order of the levels; "b" and
> "a" have been switched.
>
> You need some version of
>
> > factor(z, levels = c("c", "a", "b"))
> [1] c b a c b a
>
> See also ?reorder for a useful way of reordering the levels,
> especially in plots.
>
> -Deepayan
>
> > Cheers,
> > Bert
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along
> and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Sat, Oct 26, 2019 at 6:09 AM Patrick (Malone Quantitative) <
> > [hidden email]> wrote:
> >
> > > Try using relevel() to organize the categories in your factor in the
> > > desired order. You may need to use relevel(as.factor()) .
> > >
> > > On Sat, Oct 26, 2019 at 6:51 AM April Ettington
> > > <[hidden email]> wrote:
> > > >
> > > > Hi,
> > > >
> > > > When I use ggpubr with an x-axis utilizing descriptive categories
> (eg.
> > > bar
> > > > chart for different colors of car), it sorts all of the labels
> > > > alphabetically.  Is there a way to change this so it shows in the
> order I
> > > > want?
> > > >
> > > > Thanks,
> > > >
> > > > April
> > > >
> > > >         [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > > 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 -- To UNSUBSCRIBE and more, see
> > > 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 -- To UNSUBSCRIBE and more, see
> > 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 -- To UNSUBSCRIBE and more, see
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
|

ggpubr: order of non-numeric x-axis items

Bendix Carstensen
In reply to this post by April Ettington
In the Epi package is a function, Relevel  (note the capital R), that does general reordering and combination of factor levels.
Bendix Carstensen
maintainer of Epi
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.