如何正确地按照中位数排列facet boxplots? [英] How to properly sort facet boxplots by median?

查看:211
本文介绍了如何正确地按照中位数排列facet boxplots?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R自带的'钻石'数据集。当试图根据价格中位数对颜色因素进行排序时,它不起作用。





  ggplot(diamonds,aes(x = reorder(color,-price,FUN =中值),y =价格))+ 
geom_boxplot()+
facet_wrap(〜cut)+
ylim(0,5500)
pre>

它给了我(根本没有排序):



有什么我做错了或失踪?

解决方案

这是一种使用两个辅助函数实现请求排列的相对简单的方法



使用 ylim(0,5500)将删除大部分数据,从而产生不同的箱型图,这将干扰任何以前定义的订单。如果你想限制一个轴而不这样做,最好使用:

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

结果如下:





如果您真的打算删除大部分数据并保持排列,请在数据图之前过滤数据:

 钻石%>%
过滤器(价格< 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)


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:

ggplot(diamonds, aes(x = reorder(color, -price, FUN=median), y = price)) + 
  geom_boxplot() + 
  facet_wrap(~cut) + 
  ylim(0, 5500)

And it gives me that (not sorted at all):

Is there something I'm doing wrong or missing?

解决方案

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")

这篇关于如何正确地按照中位数排列facet boxplots?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆