Search code examples
r-highcharter

How can I set the yAxis limits within highchart() plot?


I would like to balance the appearance of my plot by setting each side my y axis 100%. I have tried to apply some solutions from Highcharts, but I am struggling with converting the syntax to R.

library(highcharter)
categories = c('2015', '2016', '2017', '2018', '2019')
hc <-highchart()%>%
  hc_chart(type= 'bar')%>%
  hc_title(text= 'My Assessment')%>%
  hc_subtitle(text= 'Mathematics')%>%
  hc_legend(enabled = TRUE) %>%
  hc_xAxis(
    list(categories = categories)) %>%
  hc_tooltip(
    shared = FALSE,
    formatter = JS("function () {
                   return this.point.category + '<br/>' +
                   Highcharts.numberFormat(Math.abs(this.point.y), 1);}"))%>%
    hc_yAxis(title = list(
                  text= 'Percent of Students in Performance Levels'),
           labels = list(
                  formatter = JS("function () {
                      return Math.abs(this.value) + '%';
                                    }")))%>%
  hc_plotOptions(series = list(stacking= 'normal'))%>%
  hc_series(
    list(name = 'Not Yet Met',
         data = c(-3, -4, -5, -6,-4), 
         legendIndex = 1),
    list(name = 'Partially Met',
         data = c(-12, -13, -14, -15, -20),
         legendIndex = 2), 
    list(name = 'Approached',
         data= c(-10, -11, -12, -13, -15),
         legendIndex = 3),
    list(name= 'Exceeds',
         data= c(11, 10, 10, 11, 20),
         legendIndex = 5),
    list(name= 'Meets',
         data= c(64, 62, 60, 55, 41),
         legendIndex = 4)
)
hc   

I expect to see the plot regenerated with 100% on the positive and negative sides of the y axis. The plot is auto generating 40% as the limit on the negative side and 80% on the positive side.


Solution

  • You need to add min and max arguments inside hc_yAxis:

    library(highcharter)
    categories = c('2015', '2016', '2017', '2018', '2019')
    hc <-highchart()%>%
      hc_chart(type= 'bar')%>%
      hc_title(text= 'My Assessment')%>%
      hc_subtitle(text= 'Mathematics')%>%
      hc_legend(enabled = TRUE) %>%
      hc_xAxis(
        list(categories = categories)) %>%
      hc_tooltip(
        shared = FALSE,
        formatter = JS("function () {
                       return this.point.category + '<br/>' +
                       Highcharts.numberFormat(Math.abs(this.point.y), 1);}"))%>%
      hc_yAxis(title = list(
        text= 'Percent of Students in Performance Levels'),
        labels = list(
          formatter = JS("function () {
                          return Math.abs(this.value) + '%';
                                        }")),
        ##### here add min and max
        min=-100,max=100
        )%>%
      hc_plotOptions(series = list(stacking= 'normal'))%>%
      hc_series(
        list(name = 'Not Yet Met',
             data = c(-3, -4, -5, -6,-4), 
             legendIndex = 1),
        list(name = 'Partially Met',
             data = c(-12, -13, -14, -15, -20),
             legendIndex = 2), 
        list(name = 'Approached',
             data= c(-10, -11, -12, -13, -15),
             legendIndex = 3),
        list(name= 'Exceeds',
             data= c(11, 10, 10, 11, 20),
             legendIndex = 5),
        list(name= 'Meets',
             data= c(64, 62, 60, 55, 41),
             legendIndex = 4)
      )
    hc   
    

    newplot