# 为R数据帧中的每一行绘制一个函数

``````Variables:
\$ X1 (dbl) 3.742382, 4.185260, 3.869329, 4.468430, 4.287528, 4.422470, 4.23...
\$ X2 (dbl) 7.0613552, 3.1143999, 6.4780125, 0.8486984, 3.4132880, 1.6816965...
\$ X3 (dbl) -2.02416823, 9.10853246, -0.56165113, 16.16834346, 8.02026020, 1...
\$ X4 (dbl) 15.0497971, 5.0139219, 13.8001589, -2.0927945, 6.5455396, -0.790...
``````

Xn是四次多项式的参数：

``````f(x) = X1*x + X2*x^2 + X2*x^3 + X2*x^4
``````

``````DF <- data.frame(X1 = rnorm(10), X2 = rnorm(10), X3 = rnorm(10), X4 = rnorm(10))

# fixed plot region:
xmin<-0
xmax<-10
ymin<- -10
ymax<-10

for (i in 1:10 ) {
}
``````

``````library(ggplot2)
library(reshape)

xmin<-0
xmax<-10
step<-0.01

DF <- data.frame(X1 = rnorm(10), X2 = rnorm(10), X3 = rnorm(10), X4 = rnorm(10))

xx<-seq(xmin,xmax,by=step)
DF2<-data.frame(matrix("", ncol = length(DF\$X1), nrow = length(xx)))
DF2\$xx<-xx
for(i in 1:length(DF\$X1)){
DF2[,i]<-DF\$X1[i]*xx+DF\$X2[i]*xx^2+DF\$X3[i]*xx^3+DF\$X4[i]*xx^4
}

DF3 <- melt(DF2 ,  id.vars = "xx")

ggplot(DF3, aes(xx,value)) + geom_line(aes(colour = variable))
``````

0条评论