Computation failed for stat_summary, 'what' must be a character string or a function

11,386

Solution 1

The reason is that you have a variable called mean in your workspace. So when you call stat_summary …

stat_summary(aes(group = 1), geom = "line", fun.y = mean, size = 1, color = "black")

… R thinks that you’re referring to that variable, rather than the mean function from the {base} package.

R is usually able to disambiguate between functions and other variables, even if they have the same name. However, in this case the disambiguation isn’t working because you’re not calling mean directly but passing it as an argument. The solution is to manually disambiguate the function from the variable by doing either of these things:

  1. In the call to stat_summary, use the fully qualified name base::mean, rather than bare mean.
  2. In the call to stat_summary, use match.fun(mean) instead of bare mean: this will tell R that you want to use a function.
  3. Remove or rename the variable.

Solution 2

Similar problem for geom_smooth(method=lm, se=FALSE, fullrange=TRUE), I got exactly the same error message. Because I have lm in my global environment.
Just fixed the problem by changing lm to "lm":
geom_smooth(method="lm", se=FALSE, fullrange=TRUE)

Solution 3

I got the same error when trying to plot summary stats for categories for a given continuous variable. The problem for mine was:

ggplot(data = diamonds) +
   geom_pointrange(mapping = aes(x = cut, y = depth),
                 stat = "summary",
                 fun.ymax = max,
                 fun.ymin = min,
                 fun.y = median)

Functions are not called here as objects. After trying string form this worked for me:

ggplot(data = diamonds) +
      geom_pointrange(mapping = aes(x = cut, y = depth),
                 stat = "summary",
                 fun.ymax = "max",
                 fun.ymin = "min",
                 fun.y = "median")
Share:
11,386
Admin
Author by

Admin

Updated on July 21, 2022

Comments

  • Admin
    Admin over 1 year

    I am trying to following the script/example on ChickWeight plotting raw data in "Independent Group T intervals test", but keeps running into the following error for stat_summary function

    Code to reproduce here:

    library(datasets) 
    data(ChickWeight)
    
    library(ggplot2) 
    g <- ggplot(ChickWeight, aes(x = Time, y = weight, 
                             colour = Diet, group = Chick))
    g <- g + geom_line()
    
    g <- g + stat_summary(aes(group = 1), geom = "line", fun.y = mean, size = 1, color = "black")
    
    g <- g + facet_grid(. ~ Diet)
    

    Error message:
    "Computation failed in stat_summary():
    'what' must be a character string or a function"

    The error message is not very intuitive, I do not even see "what" as a param in the documentation of stat_summary, I did some research and check for others' answers but so far no concrete answer or solution to this problem.

  • Salix
    Salix over 4 years
    konrad answer's is more precise but this trick works just as well.