highcharter hc_axis无法正常工作

时间:2016-10-25 16:26:40

标签: r highcharts

跟进此post我无法使用下面的代码以正确的格式获取x轴标签。

hchart(visits, "column", x = as.Date(VisitDate), y = freq, group = Clinic) %>% 
   hc_xAxis(categories = allDates$VisitDate, title = list(text = 'Deadline'), type = 'datetime', dateTimeLabelFormats = list(month = "%b", year = "%y")) %>%
   hc_plotOptions(column = list(
     dataLabels = list(enabled = FALSE),
     stacking = "normal",
     enableMouseTracking = TRUE)
   ) 

下面生成的图表标签全部搞砸了。

enter image description here

也可以指定1个月,15天等的间隔。

1 个答案:

答案 0 :(得分:0)

enter image description here

hchart(visits, "column", hcaes(x = VisitDate, y = freq, group = Clinic)) %>% 
hc_xAxis(categories = allDates$VisitDate, title = list(text = 'Deadline'), type = 'datetime', dateTimeLabelFormats = list(month = "%b", year = "%y")) %>%
hc_plotOptions(column = list(
  dataLabels = list(enabled = FALSE),
  stacking = "normal",
  enableMouseTracking = TRUE)
) 

您需要在hchart函数内添加hcaes。这里有一些不错的示例Highcharter page

Highchart还希望日期作为时间戳以您想要的方式工作,请检查此答案plotband.

希望对您有所帮助。祝你有美好的一天

edit1:

使用此代码,您可以添加所需日期的选择范围,但是我认为这种数据和图表不适用

hchart(visits, "column", hcaes(x = VisitDate, y = freq, group = Clinic))%>% 
hc_xAxis(categories = allDates$VisitDate, title = list(text = 'Deadline'), 
type = 'datetime', dateTimeLabelFormats = list(month = "%b", year = "%y")) 
 %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
 ) %>%
hc_rangeSelector(enabled = TRUE, buttons = list(
list(type = "all", text = "All"),
list(type = "month", count = 1, text = "1m"),
list(type = "day", count = 15, text = "15d"),
list(type = "day", count = 1, text = "1d")

), selected = 3)

ex