Change xAxis format in highcharts using R

I´m trying to change the xAxis format. I have two data tables with values per month.

These are my tables:

data <- structure(list(DATA = c("2022/09", "2022/10", "2022/11"), AUM = c(31057073.67, 
                                                                  32045391.81, 32690375.86)), row.names = c(NA, -3L), class = c("data.table", 
                                                                                                                                "data.frame"))

data2 <- structure(list(DATA = c("2022/09", "2022/10", "2022/11"), CUM_SUM = c(1047515038.76, 
                                                                      978350213.95, 879488195.72)), row.names = c(NA, -3L), class = c("data.table", 
                                                                                                                                      "data.frame"))

This is my graph:

highchart() %>%
    hc_add_series(name = "AUM", id = "aum_line", data = data, hcaes(x = DATA, y = AUM), type = 'line') %>%
    hc_add_series(name = 'PL_CUM', id = 'pl_cum_line', data = data2, hcaes(x = DATA, y = CUM_SUM), type = 'line') %>%
    hc_plotOptions(column = list(dataLabels = list(enabled = F),
                                 enableMouseTracking = T)) %>%
    hc_chart(zoomType = 'xy') %>%
    hc_exporting(enabled = TRUE)

This is my plot:

My xAxis should be year and month.

Upvotes: 0

Views: 128

Answers (2)

magdalena
magdalena

Reputation: 3703

If you want to add your dates as a string, you should pass it as data.names and change xAxis.type to category.

Demo: https://jsfiddle.net/BlackLabel/x8fm4njt/

In the case of dates, the more relevant xAxis type is datetime. In that case, x should be given as a timestamp.

Demo: https://jsfiddle.net/BlackLabel/bts82m6u/

API Reference: https://api.highcharts.com/highcharts/series.line.data https://api.highcharts.com/highcharts/xAxis.type

Upvotes: 1

cgvoller
cgvoller

Reputation: 879

I think this is what you're looking for? Your date data is still a string so I converted to date before adding an additional argument to handle the formatting

data$DATA <- as.Date(paste(data$DATA, "/01", sep=""))
data2$DATA <- as.Date(paste(data2$DATA, "/01", sep=""))

Gives:

> data$DATA
[1] "2022-09-01" "2022-10-01" "2022-11-01"
> data2$DATA
[1] "2022-09-01" "2022-10-01" "2022-11-01"

Then adding hc_xAxis(dateTimeLabelFormats = list(day = '%m %Y'), type = "datetime")

  highchart() %>%
      hc_add_series(
        name = "AUM",
        id = "aum_line",
        data = data,
        hcaes(x = DATA, y = AUM),
        type = 'line'
      ) %>%
      hc_add_series(
        name = 'PL_CUM',
        id = 'pl_cum_line',
        data = data2,
        hcaes(x = DATA, y = CUM_SUM),
        type = 'line'
      ) %>%
      hc_plotOptions(column = list(
        dataLabels = list(enabled = F),
        enableMouseTracking = T
      )) %>%
      hc_chart(zoomType = 'xy') %>%
      hc_exporting(enabled = TRUE) %>%
      hc_xAxis(dateTimeLabelFormats = list(day = '%m  %Y'), type = "datetime")

Plot:

editi

Upvotes: 1

Related Questions