使用R highcharter更改x轴的日期格式

y3bcpkx1  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(81)

下面的代码创建了这个图。
请问有人能告诉我如何将x轴日期标签从“2023-06-19”转换为“19-May”格式吗?我试过谷歌搜索,但添加像labels = list(format = '{value:%b %d}')这样的东西似乎没有产生影响,我不知道为什么。

highchart() %>% 
    hc_chart(plotBackgroundColor = "#F5F5F5", type = "column") %>% 
    hc_plotOptions(column = list(dataLabels = list(enabled = FALSE), stacking = "normal")) %>%
    hc_add_series(data = input$`Bank Hol / Audit Cx`, name = "Bank Hol", color = "ivory", pointWidth = 20) %>% 
    hc_add_series(data = input$`Industrial Action Cx`, name = "Strike", color = "#F89880", pointWidth = 20) %>% 
    hc_add_series(data = input$`Non-AOS Capacity Cx`, name = "Type A Cx", color = "#311432", pointWidth = 20) %>% 
    hc_add_series(data = input$`AOS Capacity Cx`, name = "Type B Cx", color = "violet", pointWidth = 20) %>% 
    hc_add_series(data = input$`2023/24 Used`, name = "23/24 Used", color = "limegreen", pointWidth = 20) %>% 
    hc_add_series(data = input$`Capacity Plan`, name = "Plan", type = "line", color = "#980008", lineWidth = 2,
                  marker = list(fillColor = "#FFB6C1", lineWidth = 1, lineColor = NULL, symbol = "circle", radius = 4)) %>% 
    hc_add_series(data = input$`2019/20 Used`, name = "2019/20 Used", type = "line", color = "#818589", lineWidth = 1, dashStyle = "shortdash",
                  marker = list(fillColor = "white", lineWidth = 1, lineColor = NULL, symbol = "circle", radius = 3)) %>% 
    hc_add_series(data = input$`2022/23 Used`, name = "2022/23 Used", type = "line", color = "#205eda", lineWidth = 1, dashStyle = "shortdash",
                  marker = list(fillColor = "#ADD8E6", lineWidth = 1, lineColor = NULL, symbol = "circle", radius = 3)) %>% 
    hc_tooltip(crosshairs = FALSE, backgroundColor = "#F0F0F0", shared = TRUE, table = TRUE, borderWidth = 2) %>% 
    hc_yAxis(title = list(text = "Sessions (4-Hour Units)")) %>% 
  hc_xAxis(categories = as.list(input$Week), title = list(text = "Week Commencing"), type = "datetime", labels = list(rotation = 270))

Dput:

input <- structure(list(Week = structure(c(19450, 19457, 19464, 19471, 
                                  19478, 19485, 19492, 19499, 19506, 19513, 19520, 19527, 19534, 
                                  19541, 19548, 19555, 19562, 19569, 19576, 19583, 19590, 19597, 
                                  19604, 19611, 19618, 19625, 19632, 19639, 19646, 19653, 19660, 
                                  19667, 19674, 19681, 19688, 19695, 19702, 19709, 19716, 19723, 
                                  19730, 19737, 19744, 19751, 19758, 19765, 19772, 19779, 19786, 
                                  19793, 19800, 19807), class = "Date"), `Capacity Plan` = c(59.8, 
                                                                                             52.675, 69, 72, 64.875, 64.875, 68.5, 72, 59.115, 72, 72, 69, 
                                                                                             72, 72, 67.875, 72, 66.24, 66.24, 66.24, 66.24, 66.24, 59.115, 
                                                                                             68, 72, 72, 72, 69, 72, 72, 66.24, 72, 68.5, 72, 72, 72, 72, 
                                                                                             69, 72, 51.99, 59.115, 72, 68.5, 72, 72, 72, 66.24, 69, 72, 72, 
                                                                                             72, 67.875, 65), `2019/20 Used` = c(65.875, 67.5, 70.5, 59.5, 
                                                                                                                                 70.75, 58.625, 70.375, 68, 57.125, 74.625, 73.375, 67, 70.875, 
                                                                                                                                 75.625, 70.375, 68.2625, 66, 73.5, 62.75, 66.625, 63.625, 53.875, 
                                                                                                                                 71.125, 60.25, 69.875, 68.25, 72.375, 72, 71.375, 75.375, 66.25, 
                                                                                                                                 64.75, 68.125, 66.25, 81, 72.75, 72.375, 73, 36.875, 44.75, 68.375, 
                                                                                                                                 71.25, 74.125, 73.25, 70.25, 74.375, 75.5, 73.25, 65, 64.125, 
                                                                                                                                 41.25, 42.5), `2022/23 Used` = c(57.25, 51, 46.25, 59.375, 52.125, 
                                                                                                                                                                  52, 62.75, 59.625, 36.75, 57.75, 59.125, 58.5, 60.25, 52.625, 
                                                                                                                                                                  52.875, 55, 51.375, 56.25, 53.625, 61.5, 52.5, 49, 54.25, 56.875, 
                                                                                                                                                                  49.5, 57.625, 54.875, 61.125, 61, 54.875, 61.125, 53.5, 60.5, 
                                                                                                                                                                  57.875, 64.625, 63.625, 65, 60.625, 38.5, 47.375, 59.125, 50.75, 
                                                                                                                                                                  60.25, 63.25, 50.5, 55.25, 63.875, 59.125, 51.75, 50.375, 62, 
                                                                                                                                                                  55.25), `2023/24 Used` = c(47.875, 34.625, 66.625, 64.375, 56.625, 
                                                                                                                                                                                             59.875, 67.75, 71.4875, 53.125, 77.75, 55.625, NA, NA, NA, NA, 
                                                                                                                                                                                             NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                             NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                             NA, NA, NA, NA, NA), `Industrial Action Cx` = c(NA, 16, NA, NA, 
                                                                                                                                                                                                                                             NA, NA, NA, NA, NA, NA, 20, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                             NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                             NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
                                                                                                                                                                                             ), `AOS Capacity Cx` = c(6, 6, NA, NA, NA, NA, NA, NA, 4, NA, 
                                                                                                                                                                                                                      2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                      NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                      NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), `Non-AOS Capacity Cx` = c(NA, 
                                                                                                                                                                                                                                                                                         NA, 2, 7, 2.7375, 3.75, 3, 1.25, 2, 1, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                         NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                         NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                         NA, NA, NA, NA), `Bank Hol / Audit Cx` = c(10, 9.25, 2.9875, 
                                                                                                                                                                                                                                                                                                                                    NA, 13.25, 12.25, 3, NA, 12.25, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                                                                    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                                                                    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                                                                    NA, NA, NA)), row.names = c(NA, -52L), class = c("tbl_df", "tbl", 
                                                                                                                                                                                                                                                                                                                                                                                     "data.frame"))
jvidinwx

jvidinwx1#

您可以在hc_xAxis中更改categoriesformat,如下所示:

library(highcharter)
library(dplyr)

highchart() %>% 
  hc_chart(plotBackgroundColor = "#F5F5F5", type = "column") %>% 
  hc_plotOptions(column = list(dataLabels = list(enabled = FALSE), stacking = "normal")) %>%
  hc_add_series(data = input$`Bank Hol / Audit Cx`, name = "Bank Hol", color = "ivory", pointWidth = 20) %>% 
  hc_add_series(data = input$`Industrial Action Cx`, name = "Strike", color = "#F89880", pointWidth = 20) %>% 
  hc_add_series(data = input$`Non-AOS Capacity Cx`, name = "Type A Cx", color = "#311432", pointWidth = 20) %>% 
  hc_add_series(data = input$`AOS Capacity Cx`, name = "Type B Cx", color = "violet", pointWidth = 20) %>% 
  hc_add_series(data = input$`2023/24 Used`, name = "23/24 Used", color = "limegreen", pointWidth = 20) %>% 
  hc_add_series(data = input$`Capacity Plan`, name = "Plan", type = "line", color = "#980008", lineWidth = 2,
                marker = list(fillColor = "#FFB6C1", lineWidth = 1, lineColor = NULL, symbol = "circle", radius = 4)) %>% 
  hc_add_series(data = input$`2019/20 Used`, name = "2019/20 Used", type = "line", color = "#818589", lineWidth = 1, dashStyle = "shortdash",
                marker = list(fillColor = "white", lineWidth = 1, lineColor = NULL, symbol = "circle", radius = 3)) %>% 
  hc_add_series(data = input$`2022/23 Used`, name = "2022/23 Used", type = "line", color = "#205eda", lineWidth = 1, dashStyle = "shortdash",
                marker = list(fillColor = "#ADD8E6", lineWidth = 1, lineColor = NULL, symbol = "circle", radius = 3)) %>% 
  hc_tooltip(crosshairs = FALSE, backgroundColor = "#F0F0F0", shared = TRUE, table = TRUE, borderWidth = 2) %>% 
  hc_yAxis(title = list(text = "Sessions (4-Hour Units)")) %>% 
  hc_xAxis(categories = as.list(format(as.Date(input$Week, format = "%y-%m-%d"), "%d-%b")), 
           title = list(text = "Week Commencing"))

x1c 0d1x创建于2023-06-23与reprex v2.0.2

相关问题