'Computation failed for stat_summary, 'what' must be a character string or a function
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 instat_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.
Solution 1:[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:
- In the call to
stat_summary
, use the fully qualified namebase::mean
, rather than baremean
. - In the call to
stat_summary
, usematch.fun(mean)
instead of baremean
: this will tell R that you want to use a function. - Remove or rename the variable.
Solution 2:[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:[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")
Solution 4:[4]
same problem here.
For me the trick was also that I needed strings as parameters. Example:
expBar + stat_summary(fun.y = "sum", geom = "bar", fill = "white", colour = "black")
instead of
expBar + stat_summary(fun.y = sum, geom = "bar", fill = "white", colour = "black")
made it work.
Hope that helps,
rikojir
Solution 5:[5]
Some of my students experienced that error because there were superfluous variables in the dataframe (that had not been dropped before/during the use of the reshape
function). Try creating a temporary dataframe where variables that will not be used are dropped.
df_temp <- df[c("needed1", "needed2", "needed3")]
You may also drop variables using -
before them.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Kerem T |
Solution 4 | rikojir |
Solution 5 | Martin Gal |