首页 > 解决方案 > 在悬停模板中添加自定义数据(情节,闪亮,r)

问题描述

我想在闪亮的应用程序中修改 plotly-sankey 图的标签。

例如,我想在悬停信息value<extra>框中显示 1/10。在下面找到一个最小的工作示例。

该手册对以下内容中的其他详细信息进行了说明hovertemplate

可用的变量是作为此链接https://plot.ly/javascript/plotlyjs-events/#event-datahovertemplate中描述的事件数据发出 的变量。此外,每个点可以指定的每个属性(那些 )都是可用的。arrayOk: true

hovertemplate或者,如果可能的话,我很乐意在字符串本身中除以 10 。

(非)工作示例:

rm(list = ls())

if(!require(pacman)) install.packages("pacman")
pacman::p_load(shiny, plotly)

# ui ----------------------------------------------------------------------

ui <- fluidPage(
  fluidRow(
    plotlyOutput("sankey")
  )
)


# server ------------------------------------------------------------------

server <- function(input, output, session) {

  output$sankey <- renderPlotly(

    plot_ly(
      type = "sankey",
      orientation = "h",

      node = list(
        label = c("A1", "A2", "B1", "B2", "C1", "C2"),
        color = c("blue", "blue", "blue", "blue", "blue", "blue"),
        pad = 15,
        thickness = 20,
        line = list(
          color = "black",
          width = 0.5
        )
      ),

      link = list(
        source = c(0,1,0,2,3,3),
        target = c(2,3,3,4,4,5),
        value =  c(8,4,2,8,4,2),

        customInfo = c(8,4,2,8,4,2)/10,

        hovertemplate = "%{value}<extra>%{customInfo}</extra>"

      )
    ) %>% 
      layout(
        title = "Basic Sankey Diagram",
        font = list(
          size = 10
        )
      )

  )

}


shinyApp(ui = ui, server = server)

标签: rshinyplotlyr-plotly

解决方案


推荐阅读