【问题标题】:R Highcharter heatmap x-axis categoriesR Highcharter 热图 x 轴类别
【发布时间】:2019-08-21 19:03:39
【问题描述】:

我需要在热图的 x 轴上显示所有月份。您可以从最小示例中看到 2 月 19 日的销售数据缺失,但我希望热图将其包含为空列。

我尝试在销售值为 NA 的数据中包含一个虚拟变量,其中包括缺失的月份,但工具提示不起作用。

library(highcharter)
library(data.table)

# 1. Create data with missing month

heatmap_data <- data.table(Fruit = c('apple', 'banana', 'orange', 'pear'),
                                 Month = c('2019-01-01', '2019-03-01', '2019-04-01', '2019-05-01'),
                                 Sales = c(2, 4, 6, 8))

highcharter::hchart(heatmap_data,
    type = "heatmap",
    highcharter::hcaes(x = Month, y = Fruit, value = Sales))


# 2. Attempt with dummy entry included

heatmap_data <- data.table(Fruit = c('apple', NA, 'banana', 'orange', 'pear'),
                                 Month = c('2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01', '2019-05-01'),
                                 Sales = c(2, NA, 4, 6, 8))

highcharter::hchart(heatmap_data,
    type = "heatmap",
    highcharter::hcaes(x = Month, y = Fruit, value = Sales)) %>%

    highcharter::hc_tooltip(borderWidth = 4)

第二次尝试在 x 轴上显示所有所需的月份,但随后我丢失了工具提示。有没有更好的方法来处理这些缺失的月份?

2019-08-21 更新 在闪亮材料闪亮应用中使用@Ben 的示例:

library(highcharter)
library(data.table)
library(shiny)
library(shinymaterial)

heatmap_data <- data.table(
  Fruit = c('apple', 'banana', 'orange', 'pear'),
  Month = c('2019-01-01', '2019-03-01', '2019-04-01', '2019-05-01'),
  Sales = c(2, 4, 6, 8))

heatmap_data$Month <- datetime_to_timestamp(as.Date(heatmap_data$Month, format = "%Y-%m-%d")) 

ui <- material_page(
    title = "Heatmap Example",
    highchartOutput("heatmap")
)

server <- function(input, output) {

    output$heatmap <- renderHighchart({
        highchart() %>%
            hc_chart(
                type = 'heatmap'
            ) %>%
            hc_add_series(
                data = heatmap_data,
                type = 'heatmap',
                hcaes(x = Month, y = Fruit, value = Sales),
                colsize = 24 * 3600 * 1000 * 30,
                tooltip = list(
                    pointFormat = '{point.x:%b, %Y}, {point.y}: {point.value}'
                ),
                showInLegend = FALSE,
                borderWidth = 4
            ) %>%
            hc_xAxis(
                type = 'datetime'
            ) %>%
            hc_yAxis(
                categories = heatmap_data$Fruit
            )
    })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r r-highcharter


    【解决方案1】:

    对于 2019 年 2 月 1 日第二次尝试使用 NA,也许明确添加工具提示格式化程序:

    hc_tooltip(borderWidth = 4,
               formatter = JS('function (tooltip)
                 {return tooltip.defaultFormatter.call(this, tooltip);}
               '), shared = TRUE)
    

    编辑:上面的这个解决方案引发了一个 javascript 错误(参见 cmets)。

    备选方案 1:如果数据集以数字形式包含月份(而不是日期),则热图将正确绘制 2 月份的缺失数据。注意 month.abb 的月份索引为零。

    library(highcharter)
    library(data.table)
    
    heatmap_data <- data.table(Fruit = c('apple', 'banana', 'orange', 'pear'),
                               Month = c(0, 2, 3, 4),
                               Sales = c(2, 4, 6, 8))
    
    hchart(heatmap_data,
           type = "heatmap",
           hcaes(x = Month, y = Fruit, value = Sales)) %>%
      hc_xAxis(categories = month.abb)
    

    备选方案 2:如果将 Month 变量保留为日期,则可以使用 datetime_to_timestamp 以毫秒为单位进行转换。 xAxis 应为“日期时间”类型。 Colsize 也需要从时间戳匹配时间(以毫秒为单位),以便更好地近似宽度。我希望这可能会有所帮助,并且更接近您的想法。

    编辑:这个解决方案包括闪亮的实现,不会给我任何 js 错误:

    library(highcharter)
    library(data.table)
    library(shiny)
    
    heatmap_data <- data.table(Fruit = c('apple', 'banana', 'orange', 'pear'),
                               Month = c('2019-01-01', '2019-03-01', '2019-04-01', '2019-05-01'),
                               Sales = c(2, 4, 6, 8))
    
    heatmap_data$Month <- datetime_to_timestamp(as.Date(heatmap_data$Month, format = "%Y-%m-%d")) 
    
    ui <- fluidPage(
        titlePanel("Heatmap Example"),
        mainPanel(highchartOutput("heatmap"))
    )
    
    server <- function(input, output) {
    
        output$heatmap <- renderHighchart({
            highchart() %>%
                hc_chart(
                    type = 'heatmap'
                ) %>%
                hc_add_series(
                    data = heatmap_data,
                    type = 'heatmap',
                    hcaes(x = Month, y = Fruit, value = Sales),
                    colsize = 24 * 3600 * 1000 * 30,
                    tooltip = list(
                        pointFormat = '{point.x:%b, %Y}, {point.y}: {point.value}'
                    ),
                    showInLegend = FALSE,
                    borderWidth = 4
                ) %>%
                hc_xAxis(
                    type = 'datetime'
                ) %>%
                hc_yAxis(
                    categories = heatmap_data$Fruit
                )
        })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 这在我的 PC 上本地运行时有所帮助,但是当我部署到 shinyapps.io 时,我收到错误消息“未捕获的类型错误:无法读取未定义的属性 'attr'”导致崩溃。在系列中拥有 NA 似乎对 highcharter 来说是个问题。所以我仍在寻找解决方案@Ben
    • 感谢您的更新,很遗憾听到 - 我想在今天晚些时候再次查看此内容...
    • @Greg 不幸的是到目前为止没有运气 - 虽然工具提示在部署在闪亮的应用程序中时确实有效,但我重现了相同的 js 错误(不仅是部署的应用程序,而且当我在浏览器中打开时也在本地)检查控制台)。该错误是由 NA 本身引起的,而对工具提示没有任何更改。我希望其他人可以发布更好的解决方案。我会继续努力...
    • 感谢 Ben,是的,我在本地也看到了 js 错误,但它们并没有使应用程序崩溃;部署后只有一个问题。我也会继续尝试,因为我确信必须有一种方法来处理一系列缺失的数据点。
    • 如果您使用数字月份 (1, 2, 3) 而不是日期 (2019-01-01...),它似乎可以工作。热图将在第二个月(2 月)留下空白空间。作为替代方案,您可以将 heatmap_data$Month 转换为时间戳: heatmap_data$dateTime
    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多