2017-02-21 73 views
1

函數圖形的簡單的例子:在ggplot繪製功能 - 參數列表

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) 

回答

3

是這樣的?

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

+1

沒錯!謝謝! – Juanchi