【发布时间】: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