选择plot_ly时动态更改图形标题

时间:2020-09-21 16:59:49

标签: r plot shiny

我的闪亮服务器中包含以下代码:

  output$SimilarityComparison <- renderPlotly({
    df <- newmpresults[place == input$CitySelected & upc_code == input$SKUSelected & category == input$categoryselectedtrends,]
    graph.title <- df$description
    plot_ly(
      #newmpresults[place == input$CitySelected & upc_code == input$SKUSelected & category == input$categoryselectedtrends,],
      df,
      x=~quantity,
      y = ~unit_price,
      text = ~paste("Precio: ", unit_price, '$<br>Cantidad:', quantity, '<br>MP:', type),
      color = ~as.factor(type)) %>%
      add_markers()%>%
      layout(
        title = ~paste('Producto:', graph.title),
        xaxis = list(title ='Cantidad'),
        yaxis = list(title ='Precio')) %>%
      config(displayModeBar = FALSE)
  })

,我希望在UI中更改选择后,图形的标题会更改并显示在图形中(只有layout( title = ~paste('Producto:', graph.title)部分给我带来了问题)。我认为这就像声明我的标题输入一样简单df$description,但它不会在图形中显示。其他一切都很好。有什么建议可以给我吗?

1 个答案:

答案 0 :(得分:0)

如果df$description不是单个条目,则以矢量作为标题。试试这个

graph.title <- unique(df$description)
layout( title = paste('Producto:', graph.title)
相关问题