I want to create some box plots and add the mean, median and n above them. For that I followed someones code. first the function:
box.stats_weight<- function(y, upper_limit = max(mean_weight) * 1.15) {
return(data.frame(
y = 0.95 * upper_limit,
label = paste(
"n =", length(y), "\n",
"mean =", round(mean(y), 2), "\n",
"median =", round(median(y), 2), "\n"
)
))
}
I get no errors running that code. When I try to implement it in my Boxplot it get the Error:
Error during wrapup: argument "y" is missing, with no default
My Boxplot code looks like the following:
weight_box <- ggboxplot(SB01.data, x = "treatment", y = "mean_weight",
add = "jitter", shape = "treatment"+
stat_summary(fun.data = box.stats_weight(), geom = "text", hjust = 0.5, vjust = 0.9) +
theme_classic())
Besides the issue with calling box.stats_weight
which was already mentioned in the comments there is also an issue in your code related to the misplaced closing )
, ie it should be ggboxplot(...) +...
while your did ggboxplot(... +...)
. Also, as you provided no reproducible example I can only guess that mean_weight
(which is used for the upper_limit) is a vector in your global environment. Otherwise this will result an error too. In my code below I pass the upper limit directly when calling box.stats_weight
.
Using mtcars
as example data:
box.stats_weight <- function(y, upper_limit = max(mean_weight) * 1.15) {
data.frame(
y = 0.95 * upper_limit,
label = paste(
"n =", length(y), "\n",
"mean =", round(mean(y), 2), "\n",
"median =", round(median(y), 2), "\n"
)
)
}
library(ggpubr)
#> Loading required package: ggplot2
ggboxplot(mtcars,
x = "cyl", y = "mpg",
add = "jitter", shape = "cyl"
) +
stat_summary(fun.data = ~ box.stats_weight(.x, upper_limit = max(mtcars$mpg) * 1.15), geom = "text", hjust = 0.5, vjust = 0.9) +
theme_classic()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.