这是我的数据框:
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 ) {
curve(DF$X1[i]*x+DF$X2[i]*x^2+DF$X3[i]*x^3+DF$X4[i]*x^4, xlim=c(xmin,xmax), ylim=c(ymin,ymax), add=TRUE)
}
编辑:通过使用ggplot:
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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句