考虑以下代码:
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)
如您所见,没有观察到图例,如果被观察到,则它们是线条的另一种颜色。
如何在不使用ggplot2的情况下使图例表示颜色?以及如何将其与我自己创建的图例一起使用?
答案 0 :(得分:2)
该代码难以置信,难以理解,并且无法以通常在R中绘制的方式来完成。这是一个整洁的解决方案,可将您带到同一个地方。
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()
答案 1 :(得分:1)
只需在Get-ChildItem -File -Recurse |
Where-Object { $PSItem.CreationTime.Day -eq 9 }
中添加col
自变量
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)