R. GGplot2, geom_boxplot with custom quantiles

2019-08-31 06:05发布

I have a dataset which includes data from 100 simulations of train runs in a network with 4 trains, 6 stations and lateness at arrival for each train at each station. My data looks something like this:

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

Now, I need to create a box plot which looks similar to this:

library(ggplot2)
m <- ggplot(MyData , aes(y = Arrival_Lateness, x = factor(Stations)))
m + geom_boxplot(aes(fill = factor(Train_number)))

https://imagizer.imageshack.us/v2/1144x436q90/19/bnrx.png

But this doesn't work for my data because geom_boxplot uses inter-quartile range for whiskers. I would like to define my own quantiles for boxes and whiskers. I found this post from Stackoverflow which partially solves my problem Changing whisker definition in geom_boxplot . But when I apply the solution (I modified the code by inserting fill = factor(Train_number) in to aes function) I get this:

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

https://imagizer.imageshack.us/v2/1144x436q90/827/m9y0.png

This is clearly not what I want. I need to have boxes for each train side by side like in the first image, and not overlapping like in the second one. How do I do this?

I will appreciate any help!

Thanks in advance!

标签: r ggplot2
1条回答
forever°为你锁心
2楼-- · 2019-08-31 06:47

You are so close: just add position="dodge" to the call to stat_summary(...).

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

ggplot is a fantastic tool, but one of the frustrating things about it is that the defaults are different depending on which function you are using. For geom_boxplot(...) the default position is "dodge", while for stat_summary(...) the default position is "identity".

查看更多
登录 后发表回答