Plotly:循环中的add_trace

时间:2015-12-09 18:41:28

标签: r plot ggplot2 plotly

我尝试在每个循环中添加ad_trace广告,但我只得到一个互相叠加的广告图。

mean <- -0.0007200342
sd   <- 0.3403711
N=10
T=1
Delta = T/N

W = c(0,cumsum( sqrt(Delta) * rnorm(N, mean=mean, sd=sd)))
t <- seq(0,T, length=N+1)

p<-plot_ly(y=W, x=t)

for(i in 1:5){

  W <- c(0,cumsum( sqrt(Delta) * rnorm(N, mean=mean, sd=sd)))
  p<-add_trace(p, y=W)

}
print(p)

enter image description here

5 个答案:

答案 0 :(得分:7)

plot_lyadd_trace函数有一个evaluation = FALSE选项,您可以将其更改为TRUE,这样可以解决范围问题。

答案 1 :(得分:5)

evaluate = TRUE中使用add_trace

答案 2 :(得分:2)

讨厌,但有效:

mean <- -0.0007200342
sd   <- 0.3403711
N=10
T=1
Delta = T/N

W = c(0,cumsum( sqrt(Delta) * rnorm(N, mean=mean, sd=sd)))
t <- seq(0,T, length=N+1)


for(i in 1:5){

  W <- c(0,cumsum( sqrt(Delta) * rnorm(N, mean=mean, sd=sd)))

  assign(paste("W_",i,sep=""),W)
  assign(paste("Name_", i, sep=""), paste("Name",i,sep=""))
  if(i==1){
    pString<-"p<-plot_ly(x = t, y = W_1, name='W1')"
  } else {
    pString<-paste(pString, " %>% add_trace(x=t, y =",  eval(paste("W", i, sep="_")),", name=", eval(paste("Name", i, sep="_")), ")", sep="")
  }

}
eval(parse(text=pString))
print(p)

答案 3 :(得分:1)

我这样做:

<!-- generated on Wed Nov 30 12:18:33 2011 by SUMO netconvert Version 0.13.1
<?xml version="1.0" encoding="iso-8859-1"?>

<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.sf.net/xsd/netconvertConfiguration.xsd">

    <input>
        <type-files value="erlangen.edgetypes.xml"/>
        <osm-files value="erlangen.osm"/>
    </input>

    <output>
        <output-file value="erlangen.net.xml"/>
    </output>

    <projection>
        <proj.utm value="true"/>
    </projection>

    <edge_removal>
        <remove-edges.isolated value="true"/>
    </edge_removal>

    <processing>
        <osm.discard-tls value="true"/>
        <no-turnarounds value="false"/>
        <offset.disable-normalization value="true"/>
        <roundabouts.guess value="true"/>
        <junctions.join value="true"/>
    </processing>

</configuration>
-->

答案 4 :(得分:-3)

这里描述: http://www.r-graph-gallery.com/129-use-a-loop-to-add-trace-with-plotly/

将您的绘图保存在变量中,然后执行add_trace:

p <- plotly(...)

p<- add_trace(p, ...)
相关问题