我正在尝试在R中绘制数学函数。 我试图从以下网站重新创建代码: https://rpubs.com/kaz_yos/ggplot2-stat-function
这是我的代码:
a <- 1;
b <- 1;
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
brem_nosc <- function(x) (1 + x^2 - (2*x/3))*(log(x) - (1/2))
p + layer(geom = "path", # Default. Can be omitted.
stat = "function",
fun = brem_nosc, # Give function
mapping = aes(color = "fun.1") # Give a meaningful name to color
) +
scale_x_continuous(limits = c(-5,5)) +
scale_color_manual(name = "Function", values = c("blue"))
但是我遇到以下错误:
Error in layer(geom = "path", stat = "function", fun = brem_nosc, mapping = aes(color = "fun.1")) :
unused argument (fun = brem_nosc)
我无法弄清楚为什么无法识别该功能。可能是什么问题? 谢谢您阅读
答案 0 :(得分:1)
当层的 'stat' 参数设置为“function”时,该函数必须在分配给 'params' 参数的列表中传递。使用您的示例,您应该输入
p + layer(geom = "path", stat = "function", params = list(fun = brem_nosc),mapping = aes(color = "fun.1"))