I'd like to have something similar to geom_boxplot, but that will only have a box, and that I can set the function for the lower and upper parts of the box, for example, showing plus minus 1 SD of the data from the mean. I am not if stat_boxplot
can be used for this purpose or if some other function would fit better.
This can be (almost) done manually on data using stat="identity" and pre-computation, for example:
y <- rnorm(100)
y1 <- mean(y) - sd(y)
y2 <- mean(y) + sd(y)
df1 <- data.frame(y)
df2 <- data.frame(
x = 1,
y0 = y1,
y25 = y1,
y50 = y1, # this is a problem...
y75 = y2,
y100 = y2
)
ggplot(df1, aes(x=1,y=y)) +
geom_boxplot() +
geom_boxplot(data = df2,
mapping = aes(x = 1, y = 1, ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
stat = "identity", alpha = 0.1, fill = "red")
This example has several problems:
- the box is not of the width of the boxplot
- the lower part of the box has a thicker line than the upper part (since I needed to say where "middle" should be)
- The computation needs to happen manually per data (which could be wrapped in a function, but due to the other problems, I didn't get to that yet).
In short, I'd like something like geom_box but couldn't find it from a google search, and I'd be happy for some directions on how to proceed with writing such a customized geom function (I guess this is a start, but some more help would be welcomed).
width
is a computed variable (along with the box and whisker ranges). It appears that the width of the original boxplot is0.75
, so you could add to theaes
of your secondgeom_boxplot
call. An option formiddle
would be to put it at the median, which would then draw it in the same place as the original boxplot. – aosmith