【问题标题】:ggplot2 reorder my boxplot by 80th percentileggplot2 按第 80 个百分位重新排列我的箱线图
【发布时间】:2021-12-25 07:41:43
【问题描述】:

我想按照第 80 个百分位值的顺序重新排列箱线图。

我的情节是这样的:

我的代码结构如下:

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)

目前我按中位数对它们进行排序。我有两个问题:

  1. 看起来它按中值对箱线图进行排序,直到图的 1/3 左右,然后返回到随机排序。为什么会这样?

  2. 如何轻松按第 80 个百分位排序?我尝试在quantile(0.8, y) 中替换median,但出现错误。

很遗憾,我不能将数据结构/变量作为机密共享。

谢谢。

【问题讨论】:

  • 尝试 forcats::fct_reorder(x, y, .fun = quantile, probs = 0.8)

标签: r ggplot2 boxplot percentile


【解决方案1】:
  1. 图表不按顺序的问题可能是NAs,先尝试过滤一下:

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

  2. 尝试FUN = quantile, prob = 0.80,在reorder 函数中你会得到:

     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)
    

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2013-08-28
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    相关资源
    最近更新 更多