Your function must be written to handle length > 1 vectors, because stat_function
will pass a vector of values to your function, rather than applying the function to each x-axis value on the plot individually. If you try e.g. f_1(0:10)
you get the same warning, confirming this function does not handle length > 1 vectors properly. The problem is with z^y
. If z
and y
are both vectors the behavior is to apply ^
to each element of z
and y
having the same index. See for example (1:10)^(1:10)
. Now what happens when you try (1:10)^(1:20)
? The warning is telling you the lengths don't match, so the behavior may be unexpected.
You can vectorize your function by just using Vectorize
, and this should give the plot you expect
p <- ggplot(data = data.frame(z = 0), mapping = aes(z=z))
p <- p + stat_function(fun = Vectorize(f_1))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…