【问题标题】:ggplot2, geom_boxplot with custom quantiles and outliersggplot2,geom_boxplot 与自定义分位数和异常值
【发布时间】:2014-02-25 19:44:35
【问题描述】:

我有一个数据集,其中包含来自 100 次模拟火车运行的数据,该网络有 4 列火车、6 个车站以及每个车站的每辆火车的到达迟到时间。我的数据如下所示:

MyData <- data.frame(
  Simulation = rep(sort(rep(1:100, 6)), 4),
  Train_number = sort(rep(c(100, 102, 104, 106), 100*6)), 
  Stations = rep(c("ST_1", "ST_2", "ST_3", "ST_4", "ST_5", "ST_6"), 100*4),
  Arrival_Lateness = c(rep(0, 60), rexp(40, 1), rep(0, 60), rexp(40, 2), rep(0, 60), rexp(40, 3), rep(0, 60), rexp(40, 5))
  )

我现在使用自定义分位数为每个火车和车站创建箱线图(感谢 jlhoward):

f <- function(x) {
  r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

ggplot(MyData, aes(factor(Stations), Arrival_Lateness, fill = factor(Train_number))) + 
  stat_summary(fun.data = f, geom="boxplot", position="dodge")

非常漂亮:

我现在缺少的是异常值。我想在每个箱线图的汤姆上绘制每个火车/车站组合的前 5% 的观察值。我尝试的是这个(灵感来自this question):

q <- function(x) {
  subset(x, quantile(x, 0.95) < x)
}

ggplot(MyData, aes(factor(Stations), Arrival_Lateness, fill = factor(Train_number))) + 
  stat_summary(fun.data = f, geom="boxplot", position="dodge") + 
  stat_summary(fun.y = q, geom="point", position="dodge")

我收到一条消息:“ymax 未定义:使用 y 调整位置”,我的图表如下所示:

这显然不是我想要的。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这个?

    ggplot(MyData, aes(factor(Stations), Arrival_Lateness, 
                       fill = factor(Train_number))) + 
      stat_summary(fun.data = f, geom="boxplot", 
                   position=position_dodge(1))+
      stat_summary(aes(color=factor(Train_number)),fun.y = q, geom="point", 
                   position=position_dodge(1))
    

    恕我直言,这更容易解释。

    ggplot(MyData, aes(factor(Train_number), Arrival_Lateness, 
                   fill = factor(Train_number))) + 
      stat_summary(fun.data = f, geom="boxplot",
                   position=position_dodge(1))+
      stat_summary(aes(color=factor(Train_number)),fun.y = q, geom="point", 
                   position=position_dodge(1))+
      facet_grid(.~Stations, scales="free")+
      theme(axis.text.x=element_text(angle=-90,hjust=1,vjust=0.2))+
      labs(x="Train Number")
    

    编辑(回应 OP 的评论)

    ggplot(MyData, aes(factor(Train_number), Arrival_Lateness, 
                       fill = factor(Train_number))) + 
      stat_summary(fun.data = f, geom="boxplot",
                   position=position_dodge(1))+
      stat_summary(aes(color=factor(Train_number)),fun.y = q, geom="point", 
                   position=position_dodge(1))+
      facet_grid(.~Stations, scales="free")+
      theme(axis.text.x=element_blank(), axis.ticks.x=element_blank())+
      scale_fill_discrete("Train")+scale_color_discrete("Train")+
      labs(x="")
    

    要关闭 x 轴文本和刻度线,我们 theme(...=element_blank())。要关闭轴标签,请使用labs(x="")。此外,填充和色阶必须具有相同的名称,否则它们会分开显示。

    【讨论】:

    • 谢谢!第二个例子实际上更容易解释。但是如何从 x 轴中删除火车编号?在我的实际数据中,我有更多的火车和车站,这使得火车号相互重叠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多