R highcharter x轴日期问题
R highcharter x-axis date issue
我有一个包含月末数据的数据框。我使用的是堆积柱形图,但 x 轴日期标签有问题。例如,它在三月列下显示四月。
library(highcharter)
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30','2020-05-31','2020-05-31','2020-06-30')), Value = c(1,2,3,4,5,6), Country = c('US','Mexico','US','Canada','US','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}'))
来指定类型和标签,但没有帮助。工具提示显示正确的日期。
一个简单而有价值的解决方案是将日期转换为字符。
library(highcharter)
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30',
'2020-05-31','2020-05-31','2020-06-30')),
Value = c(1,2,3,4,5,6),
Country = c('US','Mexico','US','Canada','US','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))
我有一个包含月末数据的数据框。我使用的是堆积柱形图,但 x 轴日期标签有问题。例如,它在三月列下显示四月。
library(highcharter)
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30','2020-05-31','2020-05-31','2020-06-30')), Value = c(1,2,3,4,5,6), Country = c('US','Mexico','US','Canada','US','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}'))
来指定类型和标签,但没有帮助。工具提示显示正确的日期。
一个简单而有价值的解决方案是将日期转换为字符。
library(highcharter)
df = data.frame(Date = as.Date(c('2020-03-31','2020-03-31','2020-04-30',
'2020-05-31','2020-05-31','2020-06-30')),
Value = c(1,2,3,4,5,6),
Country = c('US','Mexico','US','Canada','US','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))