【问题标题】:Add_Trace issue in PlotlyPlotly 中的 Add_Trace 问题
【发布时间】:2021-10-09 02:08:45
【问题描述】:

如何在add_trace 函数中增加绿线 (lcl) 和红线 (lsl) 的长度,使它们延伸到绘图的末尾(而不是中途停止)?

这是我的图表代码:

      output$p <- renderPlotly({
plot1 %>%

                add_trace(y = lcl(),type = "scatter", mode = "lines", line = list(color = 'lightgreen', width = 2, dash = 'solid'), inherit = FALSE, name = paste("LCL =", lcl()[1])) %>%
     
                add_trace(y = lsl(),type = "scatter", mode = "lines", line = list(color = 'red', width = 2, dash = 'solid'), inherit = FALSE, name = paste("LSL =", lsl()[1])) %>%
              %>%
                layout(
                    title = paste(" cell culture", input$select),
                    yaxis = list(  showline = TRUE,
                                   mirror = "ticks",
                                   linecolor = toRGB("grey"),
                                   linewidth = 1.85
                    ),
                    xaxis = list(  showline = TRUE,
                                   mirror = "ticks",
                                   linecolor = toRGB("grey"),
                                   linewidth = 1.85,
                                   tickangle=70))
        })

编辑:为简单起见,我去掉了实际绘图(散点图)的代码,该图很好,不需要任何调整。

【问题讨论】:

  • 看来您在另一个question 中使用了我下面的答案。请务必接受并支持您认为有帮助的答案。

标签: r plotly r-plotly


【解决方案1】:

我不会使用add_trace 来创建跨越整个图形的水平线,而是将水平线作为形状添加到布局中:

layout(shapes = list(
  list(
    type = "line",
    x0 = 0,
    x1 = 1,
    xref = "paper",
    y0 = 75,
    y1 = 75,
    line = list(color = 'lightgreen', width = 2)
  ),
  list(
    type = "line",
    x0 = 0,
    x1 = 1,
    xref = "paper",
    y0 = 100,
    y1 = 100,
    line = list(color = 'red', width = 2)
  )
))

使用xref = "paper",因此该线横跨整个图形。

在纸张坐标中的定位不是以绝对像素的方式进行的,而是以相对于坐标系的方式进行的(1,1) 在 (layout.width-layout.margin.r, layout.height-layout.margin.t)...

https://plotly.com/python/figure-structure/#positioning-with-paper-container-coordinates-or-axis-domain-coordinates

根据您的要求更改 y 值。

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2018-04-25
    • 2021-12-02
    • 2021-08-02
    • 2021-01-29
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多