-2

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())
stefan
  • 90,330
  • 6
  • 25
  • 51
  • start [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so you can make better posts that will get traction. – Eric Aug 07 '22 at 12:52
  • the error tells you all you need to know. `box.stats_weight()` expects an argument `y` as defined in the first code block. when you call `box.stats_weight()` in the second code snippit, you do not provide a value for this argument, and as their is no default value, unlike `upper_limit`, an error is generated – henry groves Aug 07 '22 at 12:53
  • Hard to say without your data, but try changing `fun.data = box.stats_weight()` to `fun.data = box.stats_weight` — ie, remove the parens. You can also try `fun.data = "box.stats_weight"`. The idea is the `fun.data` expects the *name* of a function, which it will then evaluate on your data… whereas by adding parens, you’re calling it immediately without the input it expects. – zephryl Aug 07 '22 at 13:19
  • I tried without ```()```, with ```""```, with ```box.stats_weight(y)``` and ```box.stats_weight(mean_weight)```. Now I get the error: ```Error during wrapup: incorrect number of dimensions```. When I just call ```box.stats_weight(mean_weight)``` it works correct. – Eike Rohde Aug 07 '22 at 13:54

1 Answers1

0

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 ), i.e. 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()

stefan
  • 90,330
  • 6
  • 25
  • 51