Reordering bars by multiple variables

20 views
Skip to first unread message

jyr...@gmail.com

unread,
Dec 17, 2017, 8:41:46 PM12/17/17
to ggplot2
Say, I want to rearrange the bars in my plot by a variable, I can do something like

y = reorder(name, variable1)

But how would I go about using two variables for reordering? To first order by variable1, then by variable2?

jyr...@gmail.com

unread,
Dec 17, 2017, 8:53:22 PM12/17/17
to ggplot2
Turns out this does the job:

y = reorder(reorder(name, variable2), variable1)

Not the prettiest looking solution but it works.

Roman Luštrik

unread,
Dec 18, 2017, 3:47:39 AM12/18/17
to jyr...@gmail.com, ggplot2
You need to order factor levels.

Cheers,
Roman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Kevin Stierhoff - NOAA Federal

unread,
Dec 19, 2017, 11:51:25 PM12/19/17
to Roman Luštrik, jyr...@gmail.com, ggplot2
You should check out the new tidyverse package forcats::fct_reorder(). I think that will give you a more elegant solution.

Kevin Stierhoff
Research Fisheries Biologist
NOAA Fisheries-SWFSC
Fisheries Resources Division
110 McAllister Way
Santa Cruz, CA 95060
Reply all
Reply to author
Forward
0 new messages