rhighchartsr-highcharter

Highcharter can't remove y axis labels


Trying to remove the yAxis labels, ideally to move them to be placed at the end of the bars where values normally would be as I need a clean left side of chart.

From highcharts references have tried the following to no avail:

hc_yAxis(dataLabels = list(enabled = FALSE), 
hc_yAxis(Labels = list(enabled = FALSE)
hc_yAxis(dataLabels = list(enabled = TRUE, align = 'right')

and from here How to remove "Values" from R Highcharter

hc_yAxis(showLabel = FALSE)

though I think that method may be deprecated if yAxis_multiples is now deprecated.

I could painstakingly annotate the labels but I still have the issue of not being able to remove the yAxis labels.

test <- tibble::tribble(
  ~year,    ~Type, ~total,
  2020,    "low",     45,
  2020, "medium",     52,
  2020,   "high",     98,
  2021,    "low",     60,
  2021, "medium",     83,
  2021,   "high",     80,
  2022,    "low",     64,
  2022, "medium",     34,
  2022,   "high",     20,
  2023,    "low",     62,
  2023, "medium",     47,
  2023,   "high",     58)

test <- mutate(test, Type = fct(Type), year = as.character(year))

highchart() %>% 
  hc_plotOptions(series = list(pointWidth = 15)) %>% 
  hc_xAxis(categories = test$Type, labels = list(style = list(
    color = '#344a5e',fontFamily = "Segoe UI",fontWeight = 600))) %>% 
  hc_yAxis(dataLabels = list(enabled = FALSE)) %>%
  hc_add_series(dataSorting = list(enabled = TRUE),
                data = test, type = 'bar', hcaes(x = Type, y = total, group = year),
                dataLabels = list(enabled = FALSE, style = list(color ='#344a5e',
                                                                fontFamily = "Segoe UI",
                                                                fontWeight = 400))) %>% 
  hc_title(text = "Year on Year Comparison") %>%
  hc_subtitle(text = "") %>%
  hc_caption(text = "ref data") %>%
  hc_tooltip(sort = TRUE, table = TRUE) %>% 
  hc_size(height = 600) %>% 
  hc_add_theme(thm_bar)
    

Any guidance appreciated.


Solution

  • In your example you should use the xAxis function instead. You can use Labels = list(enabled = FALSE)) to remove the labels like this:

    library(dplyr)
    library(highcharter)
    library(forcats)
    test <- mutate(test, Type = fct(Type), year = as.character(year))
    
    highchart() %>% 
      hc_plotOptions(series = list(pointWidth = 15)) %>% 
      hc_xAxis(categories = test$Type, labels = list(style = list(
        color = '#344a5e',fontFamily = "Segoe UI",fontWeight = 600),
        enabled = FALSE)) %>%
      #hc_yAxis(dataLabels = list(enabled = FALSE)) %>%
      hc_add_series(dataSorting = list(enabled = TRUE),
                    data = test, type = 'bar', hcaes(x = Type, y = total, group = year),
                    dataLabels = list(enabled = FALSE, style = list(color ='#344a5e',
                                                                    fontFamily = "Segoe UI",
                                                                    fontWeight = 400))) %>% 
      hc_title(text = "Year on Year Comparison") %>%
      hc_subtitle(text = "") %>%
      hc_caption(text = "ref data") %>%
      hc_tooltip(sort = TRUE, table = TRUE) %>% 
      hc_size(height = 600) 
    

    enter image description here

    Created on 2024-03-25 with reprex v2.0.2