1

ggplot2 の curve() 関数で add=TRUE 引数を複製する方法がわかりませんか?

このプロットがあるとします

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

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

10 個の +stat_function() を手動で追加することなく、ggplot2 でこれを行うにはどうすればよいですか?

ありがとう

4

1 に答える 1

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

ここに画像の説明を入力

于 2013-08-28T03:32:09.953 に答える