2017-02-21 2 views
1

Простой пример функции рисования:Draw функции в 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

Смежные вопросы