我正在提供一个关于MLE的教程,并试图找出如何将静态点分组添加到绘图图中.显然,这个 idea 是,当我滑动正态分布时,你可以看到这些点对应于更低或更高的似然性.然而,我只能在第一帧上显示点.

x <- seq(0, 10, length.out = 1000)

aval <- list()
for (step in 1:6) {
  aval[[step]] <- list(
    visible = FALSE,
    name = paste0('v = ', step),
    x = x,
    y = dnorm(x, step+1)
  )
  
}

aval[3][[1]]$visible = TRUE

steps <- list()
fig <- plot_ly()

for (i in 1:6) {
  fig <-
    add_lines(
      fig,
      x = aval[i][[1]]$x,
      y = aval[i][[1]]$y,
      visible = aval[i][[1]]$visible,
      
      name = aval[i][[1]]$name,
      type = 'scatter',
      mode = 'lines',
      hoverinfo = 'name',
      
      line = list(color = '00CED1'),
      showlegend = FALSE
    )

step <- list(args = list('visible', rep(FALSE, length(aval))),method = 'restyle')
  step$args[[2]][i] = TRUE
  steps[[i]] = step
  
}


fig <- fig %>% add_markers(x = c(4.5,5,5.5), y = c(0,0,0))
# add slider control to plot

fig <- fig %>%
  layout(sliders = list(list(
    active = 0,
    currentvalue = list(prefix = "Frequency: "),
    steps = steps
  )))


fig

推荐答案

你为什么不用普洛特利的animation capabilities

但无论是定义自定义步骤还是使用frame参数,都必须 for each 步骤提供"静态"点:

library(plotly)
library(data.table)

f <- 1:6
x <- seq(0, 10, length.out = 1000)
y <- unlist(lapply(f+1, dnorm, x = x))

DT <- CJ(f, x) # like expand.grid()
DT[, y := y]
DT[, step := paste0("step-", f)]

staticDT <- CJ(f, x = c(4.5,5,5.5), y = 0)
staticDT[, step := paste0("step-", f)]

fig <- plot_ly(
  data = DT,
  x = ~x,
  y = ~y,
  frame = ~step,
  type = 'scatter',
  mode = 'lines',
  showlegend = FALSE,
  color = I('#00CED1')
)

fig <- add_trace(
  fig,
  data = staticDT,
  x = ~x,
  y = ~y,
  frame = ~step,
  type = 'scatter',
  mode = 'markers',
  showlegend = FALSE,
  color = I('red'),
  inherit = FALSE
)

fig <- animation_opts(
  fig, transition = 0, redraw = FALSE
)

fig <- animation_slider(
  fig, currentvalue = list(prefix = "Frequency: ")
)

fig

result

R相关问答推荐

Select R中列未排序的收件箱中的最后一个按顺序编号的列

如何将多个数据帧附加到R中的多个相应的CSV文件中?

我可以截断10字节的扩展数并转换为8字节的double吗?

手工PCA上的载体与输出双图不匹配

geom_raster不适用于x比例中超过2,15的值

geom_Ribbon条件填充创建与数据不匹配的形状(ggplot 2 r)

删除facet_wrap标签之间的水平线

列出用m n个值替换来绘制n个数字的所有方法(i.o.w.:R中大小为n的集合的所有划分为m个不同子集)

如何使用shinyChatR包配置聊天机器人

大规模重新标记haven标签数据

gt()从gt为相同内容的单元格 colored颜色 不同?

使用tidy—select创建一个新的带有mutate的摘要变量

单个轮廓重叠条的单独图例

使用较长的查询提取具有部分匹配的列表中的较短目标,

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

安全地测试文件是否通过R打开

从R中发出的咕噜声中的BUG?

通过初始的shiny 应用更新部署的shiny 应用的数据和参数,其中部署的应用程序显示为URL

按镜像列值自定义行顺序

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误