1

When using matplot to plot a matrix using:

matplot(t, X[,1:4], col=1:4, lty = 1, xlab="Time", ylab="Stock Value")

my graph comes out as:

enter image description here

How do I reduce the line thickness? I previously used a different method and my graph was fine:

enter image description here

I have tried manupilating lwd but to no avail.

Even tried plot(t, X[1:4097,1]), yet the line being printed is very thick. Something wrong with my R?

EDIT: Here is the code I used to produce the matrix X:

####Inputs mean return, volatility, time period and time step
mu=0.25; sigma=2; T=1; n=2^(12); X0=5;
#############Generating trajectories for stocks
##NOTE: Seed is fixed. Changing seed will produce 
##different trajectories
dt=T/n
t=seq(0,T,by=dt)
set.seed(201)


X <- matrix(nrow = n+1, ncol = 4)

for(i in 1:4){
  X[,i] <- c(X0,mu*dt+sigma*sqrt(dt)*rnorm(n,mean=0,sd=1))
  X[,i] <- cumsum(X[,i])
}


colnames(X) <- paste0("Stock", seq_len(ncol(X)))
  • Can you give us a [minimal, reproducible example of your problem](https://stackoverflow.com/a/5963610/8485403)? – csgroen Oct 09 '19 at 16:10
  • This seems to be a problem with an excessive amount of points. In the second graph you have data that is nicely spread, while in the prior graph each point seems very volatile. Basically the range between each point is very small, and each point seems to vary a great deal. Consider plotting every `n` points instead. For example every 10 points: `pps <- seq(1, nrow(X), by = 10); matplot(t[pps], X[pps, 1:4], col=1:4, lty = 1, xlab="Time", ylab="Stock Value")`. Without a reproducible example this is a possible fix-up. – Oliver Oct 09 '19 at 16:26
  • @csgroen edited the question with my code. – OvermanZarathustra Oct 09 '19 at 16:52
  • @Oliver Both graphs are using the same data, it's just that in my second graph I generated individual vectors of stock values c() instead of a matrix. – OvermanZarathustra Oct 09 '19 at 16:52
  • 2
    you need to add `type = 'l'` plot just the first 10 of `t` and `X` and you will see what is happening – rawr Oct 09 '19 at 17:00
  • @rawr thanks for the response. Actually, adding `type = 'l'` to `plot()` when plotting a single column of values from `X` works just fine. But when I add `lyt = 'l'` to `matplot()` it gives an error. – OvermanZarathustra Oct 09 '19 at 17:25
  • 1
    you should have two options for matplots, lines or points, by using `type = 'l'` or `type = 'p'`, respectively. for line plots, `lty` controls the line type (solid, dashed, dotted, etc), and for points, `pch` controls the plotting character. So you need to use `matplot(..., type = 'l', lty = 1)` or similar – rawr Oct 09 '19 at 17:31
  • @rawr turns out just need to add `type = '1'` to matplot. Plots just fine now. Thank you! – OvermanZarathustra Oct 09 '19 at 17:42

1 Answers1

2

Just needed to add type = "l" to matplot(....). Plots fine now.

matplot(t, X[,1:4], col=1:4, type = "l", xlab="Time", ylab="Stock Value")