rr-highchartergauge

Create a gauge chart with Highcharter in R


I am trying to create a gauge plot as the following image:

enter image description here

The df is:

df <- structure(list(Genero = c("Hombre", "Mujer"), Media = c(46.7, 
53.3)), row.names = c(NA, -2L), class = c("etable", "data.frame"
))

And the code that I've been trying is:

stops <- list_parse2(df)

highchart() %>%
     hc_chart(type = 'solidgauge') %>%
     hc_pane(
          startAngle = -90,
          endAngle = 90,
          background = list(
               outerRadius = '100%',
               innerRadius = '60%',
               shape = 'arc'
          )
     ) %>%
     hc_yAxis(
          stops = stops,
          lineWidth = 0,
          minorTickWidth = 0,
          tickAmount = 2,
          min = 0,
          max = 100,
          labels = list(y = 25) 
     ) %>%
     hc_add_series(
          data = df$Media,
          color = '#EB6909'
     ) %>%
     hc_exporting(enabled = TRUE) %>%
     hc_plotOptions(series = list(animation = FALSE))

Solution

  • I think this is what you are looking for:

    df <- df |> 
      dplyr::mutate(color = c('#FFA500', '#A9A9A9'))
    
    pie <- df |> 
      hchart(type = "pie",
             hcaes(x = Genero, y = Media, color = color)) |> 
      hc_plotOptions(
        pie = list(
          innerSize = '70%', 
          startAngle = -90, 
          endAngle = 90, 
          dataLabels = list(
            enabled = TRUE,
            format = '<b>{point.name}</b>: {point.percentage:.1f} %'
          )
        )
      )  
    
    pie
    

    The idea is to improvise a gauge plot out of a donut chart. First, I created a new column color in the data frame with which I colored the gauge plot. Next, in the pie list in hc_plotOptions(), I set the innerSize argument to 70% to create the hole in the donut chart, then the startAngle and endAngle were set to -90 and 90 respectively to effectively turn the chart into a gauge plot.

    enter image description here

    I hope this works for you!