How to properly sort facet boxplots by median?

后端 未结 2 436
天命终不由人
天命终不由人 2021-01-19 14:26

I\'m using the \'diamonds\' dataset that comes with R. When trying to sort the \'color\' factor with respect to their price median it won\'t work.

This is what I got

2条回答
  •  迷失自我
    2021-01-19 15:29

    Here is a relatively simple way of achieving the requested arrangement using two helper function available here

    reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
      new_x <- paste(x, within, sep = sep)
      stats::reorder(new_x, by, FUN = fun)
    }
    
    
    scale_x_reordered <- function(..., sep = "___") {
      reg <- paste0(sep, ".+$")
      ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
    }
    
    library(tidyverse)
    data(diamonds)
    
    p <- ggplot(diamonds, aes(x = reorder_within(color, price, cut, median), y = price)) + 
      geom_boxplot(width = 5) + 
      scale_x_reordered()+
      facet_wrap(~cut,  scales = "free_x")
    

    using ylim(0, 5500) will remove a big part of the data resulting in different box plots which will interfere with any formerly defined order. If you wish to limit an axis without doing so it is better to use:

    p + coord_cartesian(ylim = c(0, 5500))
    

    this results in:

    If you really intend to remove a big part of data and keep the arrangement, filter the data prior the plot:

    diamonds %>%
      filter(price < 5500) %>%
      ggplot(aes(x = reorder_within(color, price, cut, median), y = price)) + 
      geom_boxplot(width = 5) + 
      scale_x_reordered()+
      facet_wrap(~cut,  scales = "free_x")
    

提交回复
热议问题