【问题标题】:Plotly does not show linesPlotly 不显示线条
【发布时间】:2018-11-26 03:05:06
【问题描述】:

我正在使用 plotly 将数据集可视化为交互式绘图,该绘图可用于仪表板中的闪亮应用程序。我正在使用 plotly 包,因为它似乎非常适合我做这样的事情。我想用线条和标记创建一个图。当尝试使用下面的代码进行跟踪时,我只得到标记,中间没有线条

plot_ly(cube_eduexpgdp, x = ~year) %>% 
  add_trace(y = ~expenditure_gdp, mode = "lines+markers",
            color = ~country_name, line = list(shape = "linear")) %>%
  layout(title = "Government expenditures as percentage of GDP", 
          yaxis = list(title = "Expenditures (%)"), 
          xaxis = list(title = "Year"))

这个tutorial from plotly 建议使用以下代码来完成。当我使用此代码时,我得到一个空画布,当我将鼠标悬停在它上面时,它会在工具提示上显示分组数据点的值。

plot_ly(cube_eduexpgdp, x = ~year, y = ~expenditure_gdp, 
        color = ~country_name) %>%
  add_lines()  `

有没有人可以帮助我?将不胜感激!

【问题讨论】:

  • 你能提供一个可重现的例子吗?

标签: r shiny plotly r-plotly


【解决方案1】:

我遇到了同样的错误,发现它是由尝试绘制分组数据框引起的,所以必须先ungroup()。在这里找到:R - plotly invisible lines

【讨论】:

    【解决方案2】:

    尝试将您的 add_trace 更改为:mode = 'markers'

    我遇到了类似的问题 - 图例正确显示线条+标记,但绘图仅显示标记。故障排除后,从“线条+标记”切换到简单的“标记”实际上使线条出现。这些行也接受了跟踪中提供的 list() 参数。

    【讨论】:

    • 澄清一下:这似乎是 mode = 'markers' 的解决方法。执行此操作时,您将收到一条警告:“已指定线条对象,但线条不在模式中。将线条添加到模式中......”但是,它确实完成了工作。
    【解决方案3】:

    如果没有可重现的示例,我只能在这里猜测,但这可能是由于继承和/或plotly 默认值。下面的代码有帮助吗?

    plot_ly(cube_eduexpgdp, x = ~year, type = 'scatter', mode = 'markers') %>% 
      add_trace(y = cube_eduexpgdp[['expenditure_gdp']], mode = "lines+markers",
                color = cube_eduexpgdp[['country_name']], line = list(shape = "linear"), inherit = FALSE) %>%
      layout(title = "Government expenditures as percentage of GDP", 
              yaxis = list(title = "Expenditures (%)"), 
              xaxis = list(title = "Year"))
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 2019-06-25
      • 2022-07-30
      • 2018-09-28
      相关资源
      最近更新 更多