簡體   English   中英

R Highcharter 熱圖 x 軸類別

[英]R Highcharter heatmap x-axis categories

我需要在熱圖的 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 的示例,在 Shinymaterial Shiny 應用程序中出現問題:

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)

對於 2019 年 2 月 1 日對 NA 的第二次嘗試,或許可以明確添加工具提示格式化程序:

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

編輯:上面的這個解決方案引發了一個 javascript 錯誤(見評論)。

備選方案 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM