0

I have a dataframe in R, I am plotting line charts within using the code below. I want to create function to do the same thing that takes some parameters.

plot(df[,1], df[,2], xlab="Date", ylab="Asset Value", type='l', col=1)
# now add the others
for(j in 2:ncol(Returns)) {
    lines(date, Returns[,j], col=j)
}
legend('topleft', names(Returns), col=1:3, lty=rep(1,3))

I am able to plot the chart with it. But, now I want to plot the same chart with a function in R.

The function should take 3 arguments: df[,1] gives the dates, df[,x] and x which is a number.

Shivam
  • 113
  • 1
  • 10
  • You say `returns` is an input, and then you say `returns` is another data frame. But you also seem to have a data frame called `Stocks`. I also don't understand what you don't know how to do. I suggest **(1)** you provide a small sample input, including `data`, `returns` and the common initial value; and **(2)** you clarify what you don't know how to do. It seems your code is already parameterized, if you wrap it in `foo = function(date, Returns) {}` it looks pretty good. – Gregor Thomas Oct 12 '17 at 19:47
  • Though I have no idea what to do with "the common initial value for the assets" since you don't seem to use that in your code at all or describe what meaning it has for the plot. – Gregor Thomas Oct 12 '17 at 19:48
  • If you need help in providing sample data, there is an excellent [tutorial here](https://stackoverflow.com/q/5963269/903061). Make it copy/pastable, either by simulating data or sharing a `dput()` of your data. – Gregor Thomas Oct 12 '17 at 19:49

0 Answers0