0

This is my data frame:

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 are the parameters of a 4th degree polynomial:

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

Thus, each row represents a function. Is it possible to plot each function in the same graph?

Medical physicist
  • 2,510
  • 4
  • 34
  • 51
  • 5
    Yes. Please show what you've already tried to accomplish this. Or at least a reproducible where someone else can tinker with it without having to manually edit data. – Heroka Aug 21 '15 at 12:49
  • Please include the output data that you expect when posting the example. – Pierre L Aug 21 '15 at 13:01
  • Please, the question is simple, the solution is not. This is a reproducible example: DF <- data.frame(X1 = rnorm(10), X2 = rnorm(10), X3 = rnorm(10), X4 = rnorm(10)). I have no clue yet how to solve it, so what I have tried is not useful at all. – Medical physicist Aug 21 '15 at 13:04
  • 2
    @Medicalphysicist edit the question, and even if it's useless it proves your tried something, as SO is not a free code service, question not showing a minimal effort to be solved before asking are badly received usually. – Tensibai Aug 21 '15 at 13:15
  • define "plot a function" please – Ping Jin Aug 21 '15 at 13:15
  • Maybe [this](http://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph) or [this](http://stackoverflow.com/questions/17150183/r-plot-multiple-lines-in-one-graph) are approching your goal ? – Tensibai Aug 21 '15 at 13:19

4 Answers4

0

I would try something like this (although it is not an optimal solution)

f <- function (x, X0, X1, X2, X3, X4){
  return (X0 + X1*x + X2*x^2 + X3*x^3 + X4*x^4)
}

x <- seq(-10,10,length.out = 100)
for (i in c(1:length(DF$X1)){
  plot( f(x, 0, DF$X1[i], DF$X2[i], DF$X3[i], DF$X4[i]) ,t='l')
  par(new = TRUE)
}

Please note that I have defined a function of degree 4 (therefore you need 5 coefficients to express it). Your data suggested that X0 = 0.

I also decided to plot the polynoms between -10 and 10, you can change it if you want...

T.Des
  • 80
  • 8
0

As addition to @T.Des answer I have an alternative approach using dplyr and ggplot2 packages. Run the script step by step to see how it works.

library(dplyr)
library(ggplot2)

# get the parameters as a dataframe
dt = data.frame(x1 = c(2,4,5),
                x2 = c(6,7,2),
                x3 = c(1,2,4),
                x4 = c(2,1,8))

# create plot_id based on the number of plots you want to produce
dt$plot_id = 1:nrow(dt)

# input how many points you want for your plot
Np = 10

data.frame(expand.grid(dt$plot_id, 1:Np)) %>%                     # create all combinations of plot id and points
  select(plot_id = Var1,
         point=Var2) %>%
  inner_join(dt, by="plot_id") %>%                                # join back the parameters
  mutate(y = x1*point + x2*point^2 + x3*point^3 + x4*point^4,     # calculate your output value
         plot_id = as.character(plot_id)) %>%
  ggplot(., aes(point,y,color=plot_id)) +
  geom_line()

Based on my example dataset you should get something like:

enter image description here

AntoniosK
  • 15,991
  • 2
  • 19
  • 32
0

You also could do it like this with pure base R functionality:

set.seed(1)
x  <- seq(-10, 10, length.out = 100)
xx <- rbind(x, x ^ 2, x ^ 3, x ^ 4)
m  <- matrix(rnorm(40), ncol = 4) # your coefficients
matplot(x, t(m %*% xx), type = "l")

So note that there is no constant term in your polynomial. You just need to adapt x to your data and run this code.

enter image description here

thothal
  • 16,690
  • 3
  • 36
  • 71
0

Something like this?

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)
}

enter image description here

EDIT:By using 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))

enter image description here

mira
  • 64
  • 6