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:
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).
newbox <- function(values) {
values <- na.omit(values)
data.frame(
ymin = mean(values) - sd(values),
lower = mean(values) - sd(values),
middle = mean(values),
upper = mean(values) + sd(values),
ymax = mean(values) + sd(values),
width = 0.75
)
}
ggplot(iris, aes(Species, Sepal.Length)) +
stat_summary(fun.data = newbox, geom = "boxplot", fatten = NA)

Like that?
If you want it with no fill, you could use built-in functions like:
ggplot(iris, aes(Species, Sepal.Length)) +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1),
geom = "crossbar", fatten = NA)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With