R Highcharter x轴日期发布

问题描述

我有一个包含月末数据的数据框。我正在使用堆积式柱形图,但x轴日期标签有问题。例如,它在三月列下显示四月。

library(highcharter)
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30','2020-05-31','2020-06-30')),Value = c(1,2,3,4,5,6),Country = c('US','Mexico','US','Canada','Canada'))
hchart(df,"column",hcaes(Date,Value,group = Country)) %>%
   hc_plotOptions(column = list(stacking = "normal"))

我试图通过添加hc_xAxis(type = 'datetime',labels = list(format = '{value:%m-%Y}'))来指定类型和标签,但这无济于事。工具提示显示正确的日期。

enter image description here

解决方法

一种简单而有价值的解决方案是将日期转换为字符。

library(highcharter)
 
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30','2020-05-31','2020-06-30')),Value = c(1,2,3,4,5,6),Country = c('US','Mexico','US','Canada','Canada'))
 
df$Date <- as.character(df$Date)
 
hchart(df,"column",hcaes(Date,Value,group = Country)) %>%
   hc_plotOptions(column = list(stacking = "normal")) %>%
   hc_xAxis(type="category",categories=unique(df$Date))

enter image description here