我需要在热图的x轴上显示所有月份。您可以从最小的示例中看到2月19日缺少Sales数据,但无论如何我希望热图将其作为一个空列包含。
我尝试在Sales值为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)
答案 0 :(得分:1)
对于2019年2月1日不适用的第二次尝试,也许要显式添加工具提示格式化程序:
hc_tooltip(borderWidth = 4,
formatter = JS('function (tooltip)
{return tooltip.defaultFormatter.call(this, tooltip);}
'), shared = TRUE)
编辑:上面的此解决方案引发了JavaScript错误(请参见评论)。
替代1 :如果数据集将月份包括为数字(而不是日期),则热图将正确绘制,缺少2月份的数据。注意month的月份索引为零。
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的类型应为“ datetime”。为了更好地近似宽度,还必须使用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)