如何在对数据进行子集化时保留“geom_boxplot”中因子的排序?

时间:2016-01-18 20:57:47

标签: r plot ggplot2 boxplot

在我的箱线图中,我需要修改仅在x轴上绘制的因子的一个级别的晶须定义,并使用this answer中给出的方法,其中涉及将数据分成两个单独的帧,分别绘制它们。这具有消除因子水平的排序的不希望的副作用;它们现在是默认的(按字母顺序排列)。

如何保留原始数据框中的级别排序?

示例:

library(ggplot2)

data(diamonds)

# This function extends the whiskers to the full range of the data, rather than
# the default IQR-based range
no.outliers = function(x) {
  setNames(quantile(x, c(0.00, 0.25, 0.50, 0.75, 1.00)), 
           c("ymin", "lower", "middle", "upper", "ymax"))
}

ggplot(diamonds, aes(x=cut, y=price)) +
  geom_boxplot(data=subset(diamonds, cut != 'Ideal')) +
  stat_summary(data=subset(diamonds, cut == 'Ideal'), geom="boxplot",
               fun.data=no.outliers)

0 个答案:

没有答案