简体   繁体   English

R Highcharter:工具提示定制

[英]R Highcharter: tooltip customization

I created a chart using highcharter in a shiny dashboard and I am trying to customize the tooltip. 我在闪亮的仪表板上使用highcharter创建了图表,并且尝试自定义工具提示。 The chart is combined line and scatter plot. 图表为折线图和散点图。 I would like it to do the following: 我希望它执行以下操作:

1) Have a single box for hover information (it currently has one for the line and one for scatter) 1)有一个用于显示悬停信息的框(当前有一个框用于行,一个框用于散点)

2) Be able to use different column of information that is not used in the series x or y values 2)能够使用x或y系列值中未使用的另一列信息

I would like the tooltip to display the following information (whether I hover over the scatter point or line) for each particular x-axis value. 我希望工具提示针对每个特定的x轴值显示以下信息(是否将鼠标悬停在散点图或直线上)。

Overall 总体

Mean: 2 [Mean: data$avghours] 平均值:2 [平均值:data $ avghours]

Dog: 1 [data$animal: data$hours] 狗:1 [数据动物:数据小时]

Below is the example code I've written that demonstrates my problem: 以下是我编写的演示我的问题的示例代码:

library (shiny)
library (shinydashboard)
library (highcharter)


header <- dashboardHeader(title = "Example")

body <- dashboardBody(

  fluidRow(
    box(title = "example", status = "primary", solidHeader = TRUE, 
        highchartOutput("chart")
    )
   )
)

sidebar <- dashboardSidebar()

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {

  date <- c(1,2,3,4,5,6,7,8,9,10)
  hours <- c(1,5,4,1,6,5,7,5,4,3)
  avghours <- c(2,2,2,3,3,3,2,2,2,2)
  animal <- c("dog","cat","cat","cat","cat","cat","cat","cat","dog","dog")
  data <- data.frame(date,hours,avghours,animal)

  output$chart <- renderHighchart({

    highchart() %>%
      hc_add_series(name = "Shipments", data=data$hours, type = "scatter", color = "#2670FF", marker = list(radius = 2), alpha = 0.5) %>%
      hc_add_series(name = "Rolling Mean", data=data$avghours, color = "#FF7900") %>%
      hc_yAxis(min = 0, title = list(text = "Hours")) %>%
      hc_tooltip(crosshairs = TRUE)

  })

}

shinyApp(ui, server)

Firt of all, you need to add all the data instead give only the vector (the vector DON´T have all the information to the tooltip you want). 首先,您需要添加所有数据,而不是仅提供向量(向量不具有所需工具提示的所有信息)。 To do this you need change the data argument using the data.frame with the hcaes helper function in the mapping argument to define which variable use in every axis: 为此,您需要使用data.frame更改data参数,并在mapping参数中使用hcaes helper函数来定义每个轴使用哪个变量:

highchart() %>%
  hc_add_series(data = data, mapping = hcaes(x=date, y=hours), name = "Shipments", type = "scatter", color = "#2670FF", marker = list(radius = 2), alpha = 0.5) %>%
  hc_add_series(data = data, hcaes(date, avghours), name = "Rolling Mean", type = "line", color = "#FF7900") %>%
  hc_yAxis(min = 0, title = list(text = "Hours")) %>%
  hc_tooltip(crosshairs = TRUE)

在此处输入图片说明

Then you can use the tooltip argument in every hc_add_series to define the tooltip in each series: 然后,您可以在每个hc_add_series使用tooltip参数来定义每个系列中的tooltip:

highchart() %>%
  hc_add_series(data = data, hcaes(date, hours), name = "Shipments", type = "scatter",
                tooltip = list(pointFormat = "tooltip with 2 values {point.animal}: {point.hours}")) %>%
  hc_add_series(data = data, hcaes(date, avghours), name = "Rolling Mean", type = "line",
                tooltip = list(pointFormat = "Avg hour text! {point.avghours}")) %>%
  hc_yAxis(min = 0, title = list(text = "Hours")) %>%
  hc_tooltip(crosshairs = TRUE)

在此处输入图片说明

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

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