Home > Back-end >  ggplot2 reorder my boxplot by 80th percentile
ggplot2 reorder my boxplot by 80th percentile

Time:11-13

I want to reorder my boxplots in order of their 80th percentile values.

my plot looks like this:

enter image description here

my code structure is along the lines of:

ggplot(data, aes(x=reorder(y, x, median), y))  
  geom_boxplot(fill="deepskyblue")  
  stat_boxplot(geom ='errorbar', width=0.3)   
  theme_bw() 
  scale_y_continuous(trans="log10", n.breaks = 6)

currently ive ordered them by median. I have two questions:

  1. it looks like it orders the boxplots by the median up til about 1/3rd of the plot, then goes back to random ordering. Why would this be?

  2. how can i easily order it by the 80th percentile? i tried subbing in quantile(0.8, y) for median but get an error.

i unfortunately can't share the data structure/variables as its confidential.

Thanks.

CodePudding user response:

  1. The problem of not having the graph in order may be due to NAs, try filtering them previously:

    data <- data %>% filter(!is.na(y))

  2. try FUN = quantile, prob = 0.80, in the reorder function you will end up with:

     ggplot(data, aes(x=reorder(y, x, FUN = quantile, prob=0.80), y))  
     geom_boxplot(fill="deepskyblue")  
     stat_boxplot(geom ='errorbar', width=0.3)   
     theme_bw() 
     scale_y_continuous(trans="log10", n.breaks = 6)
    
  • Related