在ggplot中绘制函数 - 参数列表

时间:2017-02-21 17:24:44

标签: r ggplot2

功能绘图的简单示例:

p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
p + stat_function(fun = function(x) x^2 + 1*2)

enter image description here

是否可以在ggplot中的绘图代码中添加参数列表? 像这样的东西?

fun1 <- function(x) x^2 + a*b
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
p + stat_function(fun = fun1, par=list(a=1, b=2)) + xlim(-5,5)

1 个答案:

答案 0 :(得分:5)

喜欢这个吗?

fun1 <- function(x,a,b) a*x^2 + b
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
p + 
  stat_function(fun = fun1, args=list(a=1, b=2)) + 
  stat_function(fun = fun1, args=list(a=2, b=1), col='red') + 
  xlim(-5,5)

enter image description here