简体   繁体   English

修改plotly折线图中的hovertemplate

[英]Modify hovertemplate in plotly line chart

I have the shiny app below in which I want to modify the hovertemplate in a way that it will display Name , Week and lab (value in more aesthetic form).我有下面的hovertemplate应用程序,我想在其中修改悬停模板,使其显示NameWeeklab (更美观的值)。 The issue is that I have already used customdata to display lab and I do not know how could I display Name .问题是我已经使用customdata来显示lab并且我不知道如何显示Name

library(plotly)
library(shiny)
full_data<-data.frame("Name"=c("Q1","Q2","Q3","Q1","Q2","Q3"),"Values"=c(245645,866556,26440,65046,641131,463265),
                      "Week"=c("a","b","c","d","e","f"))
desc <- full_data %>% 
  group_by(Name,Week) %>% 
  summarise(values = sum(Values)) %>%
  mutate(lab = scales::label_number_si(accuracy = 0.1)(values))

shinyApp(
  ui = fluidPage(
    
    plotlyOutput("pl")
  )
  ,
  server = function(input, output, session) {
    output$pl<-renderPlotly({
      plot_ly(desc,
              x = ~Week, 
              y = ~values,
              #text = ~values,
              color = ~Name,
              colors = c("#60ab3d","#6bbabf","#c4d436","#3e5b84","#028c75","red"),
              customdata = mapply(function(x,y) list(x,y), desc$lab, desc$Name, SIMPLIFY = FALSE)) %>%
        add_trace(
          type = 'scatter',
          mode = 'lines+markers',
          hovertemplate = paste(
            "%{x}",
            "%{customdata[0]}", 
            "%{customdata[1]}", 
            "<extra></extra>",
            sep = "\n"),
          hoveron = 'points')
      
    })
  }
)
    

Try this:尝试这个:

customdata = mapply(function(x,y) list(x,y), desc$lab, desc$Name, SIMPLIFY = FALSE)

and

hovertemplate = paste(
  "%{color}",
  "%{x}",
  "%{customdata[0]}", 
  "%{customdata[1]}", 
  "<extra></extra>",
  sep = "\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM