0

Consider the following code:

T=1
N=50
X=matrix(rnorm(N*4, mean = 0, sd = 1), 4, N)
t=seq(0,T,by=T/N)
t=head(t,-1)
ymax=max(X); ymin=min(X) #bounds for simulated prices

##Plot
led=c() # For ledgend purposes
ymax=max(X); ymin=min(X) #bounds for simulated prices
plot(t,X[1,], t='l', ylim=c(ymin, ymax), col=1, ylab="Price P(t)", xlab="time t")
led=c(led, paste("GBM", 1))
for(i in 2:4){
    lines(t, X[i,], t='l', ylim=c(ymin, ymax), col=i)
    led=c(led, paste("GBM", i))
}
legend(0, 0, legend=led, lty=1:4, cex=0.8)

The outcome would be enter image description here

As you can see the legend is not observed and if they are observed they are of another colour of that of the lines.

How can I make the legend represent the colour without using the ggplot2? And how to use it with my own created legend?

6
  • What does "your own legend" mean? Commented Aug 25, 2018 at 16:09
  • @jackBrookes I mean that my created variable led Commented Aug 25, 2018 at 16:10
  • Is that a requirement for some reason? ggplot2 has pretty good support for legends. Commented Aug 25, 2018 at 16:21
  • 1
    I'm confused about "without using the ggplot2" since you're not using ggplot. I'm also removing the jupyter-notebook tag since this isn't specific to those notebooks Commented Aug 25, 2018 at 17:38
  • @camille thank you but I do not understand why did you downgrade the question? I do want it to look good on jupyter notebook. As the drawings are not good looking there. Commented Aug 25, 2018 at 18:06

2 Answers 2

2

The code is incredible unreadable and is not done in a way you would normally plot in R. Here's a tidyverse solution that gets you to the same place.

tmax <- 1 # shouldnt use T as a variable name
N <- 50
X <- matrix(rnorm(N * 4, mean = 0, sd = 1), 4, N)
times <- seq(0, tmax, by = tmax / N) # shouldnt use t as a variable name
times <- head(times, -1)
ymax <- max(X)
ymin <- min(X) #bounds for simulated prices

library(tidyverse)

df <- as.data.frame(t(X)) %>% 
  mutate(time = times) %>% 
  gather(GBM, price, -time) %>% 
  mutate(GBM = gsub("V", "GBM ", GBM))

ggplot(df, aes(x = time, y = price, color = GBM)) +
  geom_line()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Just add col argument in legend

T=1
N=50
X=matrix(rnorm(N*4, mean = 0, sd = 1), 4, N)
t=seq(0,T,by=T/N)
t=head(t,-1)
ymax=max(X); ymin=min(X) #bounds for simulated prices

##Plot
led=c() # For ledgend purposes
ymax=max(X); ymin=min(X) #bounds for simulated prices
plot(t,X[1,], t='l', ylim=c(ymin, ymax), col=1, ylab="Price P(t)", xlab="time t")
led=c(led, paste("GBM", 1))
for(i in 2:4){
  lines(t, X[i,], t='l', ylim=c(ymin, ymax), col=i)
  led=c(led, paste("GBM", i))
}
legend(0, 0, legend=led, cex=0.8,col = 1:4, lwd=2.5)

enter image description here

You can also change legend postion:

legend("topleft", legend=led, cex=0.8,col = 1:4,lwd=2.5)

enter image description here

3 Comments

Thank you so much for this but the legend is hiding some of the figure? Is there a neater way to display this? Furthermore, I see a dotted lines which is not the case in the figure. Is there a way to change that?
The dot line is because of the argument "lty", just remove it.
You can change the first argument of legend such as "topleft" "topright" "bottomleft" and so on to change the position of your legend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.