1

I am plotting boxplot with facet_wrap in ggplot2, using free scales.

Wondering how can I specify the order of each boxplot according to the table? Or to specify the order of each boxplot in each facet?

Most of the solution I have discovered worked for non-facet graph or graph with fixed scales. And the order that I wanted is not according to alphabetical order or scales.

rfall2:

Genes   rfdist  Category
18S vs ITS  0.75    18S-ITS
18S vs ITS  0.916667    18S-ITS
18S vs ITS  0.666667    18S-ITS
18S vs ITS  0.666667    18S-ITS
18S vs ITS  0.75    18S-ITS
18S vs ITS  0.833333    18S-ITS
18S vs 18S-ITS  0.666667    18S-ITS
18S vs 18S-ITS  0.583333    18S-ITS
18S vs 18S-ITS  0.5 18S-ITS
18S vs 18S-ITS  0.833333    18S-ITS
18S vs 18S-ITS  0.75    18S-ITS
18S vs 18S-ITS  0.75    18S-ITS
18S vs 18S-ITS  0.416667    18S-ITS
18S vs 18S-ITS-rbcL 0.833333    18S-ITS
18S vs 18S-ITS-rbcL 0.583333    18S-ITS
18S vs 18S-ITS-rbcL 0.833333    18S-ITS
18S vs 18S-ITS-rbcL 0.75    18S-ITS
18S vs 18S-ITS-rbcL 0.583333    18S-ITS
18S vs 18S-ITS-rbcL 0.833333    18S-ITS
18S vs 18S-ITS-rbcL 0.75    18S-ITS
18S vs 18S-ITS-rbcL 0.75    18S-ITS
18S vs rbcL 0.916667    18S-rbcL
18S vs rbcL 0.916667    18S-rbcL
18S vs rbcL 0.916667    18S-rbcL
18S vs rbcL 0.916667    18S-rbcL
18S vs rbcL 0.916667    18S-rbcL
18S vs rbcL 0.916667    18S-rbcL
18S vs 18S-rbcL 0.666667    18S-rbcL
18S vs 18S-rbcL 0.833333    18S-rbcL
18S vs 18S-rbcL 0.75    18S-rbcL
18S vs 18S-rbcL 0.75    18S-rbcL
18S vs 18S-rbcL 0.75    18S-rbcL

The R script I was using:

> ggplot(rfall2, aes(x=Genes, y=rfdist)) +
+ geom_boxplot() +
+ scale_y_continuous(name ="Normalized Robinson-Foulds Distance", limits=c(0,1))+
+ scale_x_discrete(name="Gene Trees Comparison") +
+ facet_wrap(vars(Category), scales="free", ncol=1) +
+ theme_bw()

rfall2

I wanted to reorder for eg. on the first category "18S-ITS":

18S vs ITS, 18S vs 18S-ITS, 18S vs 18S-ITS-rbcL, ..., which I have arranged them in my table.

Any idea?

web
  • 105
  • 9
  • Did you tried `rfall2$Genes <- factor(rfall2$Genes, unique(rfall2$Genes))` and then running the plot code? – Ronak Shah Jan 04 '21 at 09:25
  • @RonakShah just tried it, it worked for the first category! But how can I do it for all the categories? – web Jan 04 '21 at 09:31
  • In general the idea is `rfall2$Genes <- factor(rfall2$Genes, required_order)`. So if you have values in `Gene` as `a` to `e` and you want the order to be `a`, `c`, `b` for 1st category and `e` and `d` for second you could do `rfall2$Genes <- factor(rfall2$Genes, c('a', 'c', 'b', 'e', 'd'))` – Ronak Shah Jan 04 '21 at 09:43

0 Answers0