Order bars of one group only in a grouped barplot

1,341 views
Skip to first unread message

Mohammad Nasir Shalizi

unread,
Jun 20, 2019, 7:28:31 AM6/20/19
to ggp...@googlegroups.com
Hello,

I am trying to order (highest to lowest value) bars of only one group in a grouped bar plot. I am not interested in order of of the second group. But I need to order bars of group 1. Attached codes and figure is the example I am trying to work this out. In this example I want to order bars for x-axis label "Clone" only. My codes are;
ggplot(f402, aes(x=factor(Propagule), y=BV_Sep, fill=reorder(Genotype, BV_Sep))) + 
theme_bw() +
  geom_bar(aes(fill=Genotype), stat = "identity", position = "dodge")

My sample data are;
   Genotype  Clone Family Propagule   BV_Sep    
   Genotype1 540209 Fam402  Seedling  13.43000
   Genotype2 540219 Fam402  Seedling  13.87870
   Genotype3 540238 Fam402  Seedling  12.60450
   Genotype2 540219 Fam402     Clone  29.07208
   Genotype1 540209 Fam402     Clone  28.99002
   Genotype3 540238 Fam402     Clone  28.31975

Any suggestions would be appreciated.

Thank you,
Nasir

image.png


Brandon Hurr

unread,
Jun 20, 2019, 9:49:53 AM6/20/19
to Mohammad Nasir Shalizi, ggplot2
If you need to order levels of a factor, you need to do that outside of ggplot2.

To get the order level:

library(tidyverse)
fac_order <-
f402 %>%
filter(Propagule == "Clone") %>%
arrange(BV_Sep) %>%
pull(Genotype)

f402 <-
f402 %>%
mutate(Genotype = factor(Genotype, levels = fac_order)

ggplot(f402, aes(x=factor(Propagule), y=BV_Sep, fill=Genotype)) + 
theme_bw() +
geom_bar(stat = "identity", position = "dodge")

I think that should work. I haven't checked it though.

B

--
--
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+u...@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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/CAEdR%3D_naa80OUjrA8qJ_6624uh3q4k7ydhtgsPhDQHFEsyEevQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages