简体   繁体   English

如何在 R 中定义 function 的图形上 plot 几条曲线?

[英]How to plot several curves on one graph with defined function in R?

I need to plot a couple of curves on one graph.我需要在一张图上 plot 几条曲线。 I've got trajectories of Brownian simulation which I got from the function:我从 function 获得了布朗模拟的轨迹:

brownian <- function(T,N){
  alpha=0
  sigma=1
  
  delta_t=T/N
  
  t=seq(0,T,by=delta_t)
  #x=c(0,alpha*delta_t+sigma*sqrt(delta_t)*rnorm(N,mean=0,sd=1))
  x=c(0,alpha*delta_t+sqrt(delta_t)*rnorm(N,mean=0,sd=1))
  Xt=cumsum(x)
  plot(t,Xt,type='l',col = rep(1:3, each = 10),xlab="t=[0,T]",ylab = "B(t,ω)")
}

For example for brownian(1,1000) I get:例如对于 brownian(1,1000) 我得到:

And for brownian(10,1000) I get:对于 brownian(10,1000) 我得到:

As you can see I get black graphs.如您所见,我得到黑色图表。 I have to plot these trajectories on one graph (every trajectory should have different color).我必须在一张图上 plot 这些轨迹(每个轨迹应该有不同的颜色)。 When it takes several trajectories, it should look like:当它需要几个轨迹时,它应该看起来像:

Do you have any advices how can I plot these curves on one graph and each curve has different color?你有什么建议我怎么能 plot 这些曲线在一张图上,每条曲线都有不同的颜色?

Thanks in advance提前致谢

You could do this pretty easily by modifying the function and using ggplot() to make the graph.您可以通过修改 function 并使用ggplot()制作图表来轻松完成此操作。 The function below takes ntimes as an argument which specifies the number of times you want to do the simulation.下面的 function 将ntimes作为参数,指定您想要进行模拟的次数。 It then uses ggplot() to make the graph.然后它使用ggplot()来制作图表。 You could adjust the internals of the function to have it produce a different looking plot if you like.如果您愿意,您可以调整 function 的内部结构,让它产生不同的外观 plot。

brownian <- function(T,N, ntimes){
  if((length(N) != length(T)) & length(N) != 1){
    stop("N has to be either length of T or 1\n")
  }
  alpha=0
  sigma=1
  if(length(N) == 1 & length(T) > 1)N <- rep(N, length(T))
  dat <- NULL
  for(i in 1:ntimes){
    delta_t=T/N  
    t=seq(0,T,by=delta_t)
    #x=c(0,alpha*delta_t+sigma*sqrt(delta_t)*rnorm(N,mean=0,sd=1))
    x=c(0,alpha*delta_t+sqrt(delta_t)*rnorm(N,mean=0,sd=1))
    Xt=cumsum(x)
    dat <- rbind(dat, data.frame(xt=Xt, t=t, n=i))
  }
  require(ggplot2)
  ggplot(dat, aes(x=t, y=xt, colour=as.factor(n))) + 
    geom_line(show.legend=FALSE) + 
    labs(x="t=[0,T]",y = "B(t,ω)", colour="T") + 
    theme_classic()
}
brownian(10,1000, 5)

在此处输入图像描述

Here is a base R solution with matplot .这是带有matplot的基本 R 解决方案。 It is ideal for this type of plot, since it computes the x and y axis ranges and plots all lines in one call only.它非常适合这种类型的 plot,因为它仅在一次调用中计算 x 和 y 轴范围并绘制所有线。 It uses DaveArmstrong's idea of adding an extra argument ntimes .它使用了 DaveArmstrong 的想法,即添加一个额外的参数ntimes This argument is also used for the color scheme.此参数也用于配色方案。

brownian <- function(T, N, ntimes){
  alpha <- 0
  sigma <- 1
  delta_t <- T/N
  t <- seq(0, T, by = delta_t)
  #x=c(0,alpha*delta_t+sigma*sqrt(delta_t)*rnorm(N,mean=0,sd=1))
  Xt <- replicate(ntimes,
                  cumsum(c(0, alpha*delta_t+sqrt(delta_t)*rnorm(N, mean = 0, sd = 1)))
  )
  matplot(t, Xt, 
          type = "l", lty = 1, 
          col = seq_len(ntimes),
          xlab = "t=[0,T]", ylab = "B(t,ω)")
}

set.seed(2020)
brownian(1, 1000, 5)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM