Reputation: 101
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.
Upvotes: 7
Views: 18548
Reputation: 81
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.
Upvotes: 0
Reputation: 687
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)
Upvotes: 4
Reputation: 545518
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:
stat_summary
, use the fully qualified name base::mean
, rather than bare mean
.stat_summary
, use match.fun(mean)
instead of bare mean
: this will tell R that you want to use a function.Upvotes: 11
Reputation: 115
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
Upvotes: 0
Reputation: 258
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")
Upvotes: 1