2013-08-28 5 views
1

ggplot2의 curve() 함수에서 add = TRUE 인수를 복제하는 방법을 모르겠습니까? 가정하자ggplot2에서 많은 커브 오버레이

나는하지 않고

 testfn<-function(x,a){ 
     sin(x-a) 
    } 

    for(i in 1:10){ 
     curve(testfn(x,i),add=TRUE,xlim=c(-5,5)) 
    } 

가 어떻게 ggplot2에서이 작업을 수행 할이 플롯 수동 10 + stat_function()의 추가해야?

감사

답변

4

사용 stat_function 호출의 목록을 작성 lapply

예를 들어

library(ggplot2) 
# the base plot (with x limits) 
xlim <- c(-5,5) 
baseplot <- ggplot(data.frame(x = xlim), aes(x=x)) 

# the function 
testfn <- function(x,a){sin(x-a)} 
# a list of 10 calls (a = 1,...10) 
list_fn <- lapply(seq_len(10), function(a){ 
    stat_function(fun = testfn, args = list(a=a)) 
}) 
# the plot 
baseplot + list_fn 

enter image description here