0

I am trying to build a grouped boxplot with all species from the same genus (variable 'gen') colored the same way and plotted next to each others AND with species (variable 'spe') organized by increasing medians of my variable 'du' among each genus.

As I did not yet manage to do this, I used the variable (gen_spe : genus + species) in the code of my boxplot so that R organized them by genus (due to alphabetical order). However, I think that using "gen_spe" instead of the variable "spe" prevents me to organize by increasing medians too.

I add the boxplot obtained so that you understand better. Here, there are 4 colors for the 4 genera studied and species from the same genus are in the same color. I also would like to rearrange them so that e.g. in the red color, the smaller value of the Y variable is the first one and the larger value the last one (for each color separately) enter image description here

And my code:

    ggplot(df, aes(gen_spe, du)) +
  theme_classic(base_size = 10) +
  geom_boxplot(
    aes(color = gen,
        color = after_scale(
          colorspace::desaturate(color, .2)
        )),
    size = 0.75
  ) +
  ggsci::scale_color_npg(
    guide = "none"
  ) +
  guides(
    color = "legend"
  )

Thank you in advance !

Marine
  • 21
  • 1
  • 7
  • you could try something like : aes( x=reorder(gen_spe, -du), y=du) – Dr. Flow Oct 20 '21 at 13:44
  • 1
    You could improve your chances of finding help here by adding a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Adding a MRE makes it much easier for others to find and test an answer to your question. That way you can help others to help you! P.S. Here is [a good overview on how to ask a good question](https://stackoverflow.com/help/how-to-ask) – dario Oct 20 '21 at 13:53
  • Thank you for your answer Dr. Flow. If I do this: `aes(x=reorder( **gen_spe** , du, na.rm=T) or aes(x=reorder( **spe** , du, na.rm=T)`, I get an ordered grouped boxplot with increasing mean values. But this way, I lose the fact that species from the same genus are next to each other. I need: 1- Species from the same genus to be next to each others 2- Inside each 'group' of boxplots from the same genus (and same color), they should be organized by increasing mean values. – Marine Oct 20 '21 at 13:57

0 Answers0