使用 x 轴中的日期对象在 highchart 中制作箱线图

Make a boxplot in highchart with a date object in the x axis

我正在尝试在 highchart 中制作一个箱线图,以将其与我已有的另一个图表一起包含在一个闪亮的应用程序中。

问题是,据我所知,箱线图的行为与其他绘图不同,当您将日期映射到 x 轴时,它被视为字符串,这意味着:绘图显示整个日期 ex: "2018-04-01" 不像其他情节中那样 Apr'18

在这里,我对我所做的事情做了一点表示

# Packages
library(tidyverse)
library(lubridate)
library(highcharter)
library(magrittr)
library(plotly)

# Data
stocks <- data.frame(
  time = rep(as.Date('2009-01-01') + month(1:12), times = 10),
  stock_price = rnorm(120, 0, 1)
) 
# line plot
stocks %>%
  group_by(time) %>%
  summarise(mean_price = mean(stock_price)) %>%
  hchart(.,  
         type = "line", 
         hcaes(x = "time", 
               y = "mean_price"))
# Box plot first try
# hchart boxplot
stocks %$% 
  hcboxplot(x = stock_price, time) %>%
  hc_chart(type = "column")

第一次尝试后,我尝试创建一个缩写日期并将其映射到 x 轴,如下所示,但显示的框是按字母顺序而不是按时间顺序排列的

# hchart boxplot
stocks %>%
  mutate(month = month(time, label = T),
         year = str_extract(as.character(year(time)), "..$"),
         time2 = paste(month, year, sep = "'")) %$%
  hcboxplot(x = stock_price, time2) %>%         
  hc_chart(type = "column")

我想要的输出是一个带有 x 轴的图,就像线图或 plotly 的输出一样

stocks %>%
  group_by(time) %>%
  plot_ly(x = ~time, y = ~stock_price, type = "box") 

arrange()fct_inorder()的帮助下,我相信我已经达到了您想要的结果:

stocks %>%
  arrange(time) %>%
  mutate(
    month = month(time, label = T),
    year  = str_extract(as.character(year(time)), "..$"),
    time2 = fct_inorder(paste(month, year, sep = "'"))
  ) %$%
  hcboxplot(x = stock_price, time2) %>%
  hc_chart(type = "column")