【问题标题】:Can’t display error bars with Plotly and Shiny无法使用 Plotly 和 Shiny 显示误差线
【发布时间】:2021-12-17 23:05:02
【问题描述】:

我正在尝试使用 Shiny 和 plotly 在散点图上显示误差线。这是我的 server.R 文件中的代码:

data = reactiveVal()

observe({
    results <- data.frame() # actually getting the data from here

    # formatting output
    final.results <- cbind(
        "id" = paste(results$a,
                     results$b,
                     results$c,
                     sep = '-'),
        "sigma" = sprintf("%.5g", results$s),
        "c-e" = sprintf("%.3g",results$calc - results$exp)
    )
    
    data(final.results)
})

output$plot <- renderPlotly(
    as.data.frame(data()[,c("id", "c-e", "sigma")]) %>% plot_ly(
        x = ~`c-e`,
        y = ~id,
        height = 800,
        type = 'scatter', 
        mode = 'markers', 
        marker = list(color = "#90AFD9"),
        error_x = list(array = ~sigma, color = "#000000", type = "data")
    )
)

情节还可以,只是没有显示误差线,我的错误是什么?

编辑:澄清data() 函数的来源及其返回值。

【问题讨论】:

  • 请澄清您的问题。距离关闭仅一步之遥。我投票决定暂时开放。
  • 感谢您的投票。我更新了问题以澄清上下文。希望这会做到。

标签: r shiny plotly r-plotly


【解决方案1】:

sprintf() 函数返回一个字符串,而不是一个数字,这就是它不将sigma 值显示为错误栏的原因。如果要保留 5 位小数,请改用 round() 函数:

"sigma" = round(results$s, digits = 5)

【讨论】:

  • 我明白了,但效果并不好。此外,“c-e”字段也是sprintf 的结果,并且点在图表上正确显示。
  • 好吧,我的错,问题是即使“c-e”也没有被视为数值(虽然这一轮并没有修复它)。我必须将 layout.xaxis.type 参数设置为线性才能使其工作:layout(xaxis = list(type = "linear"))
猜你喜欢
  • 2017-09-18
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 2021-11-24
  • 2021-12-03
  • 2021-10-30
  • 1970-01-01
相关资源
最近更新 更多