Search code examples
rgaugeflexdashboard

R flexdashboard gauge function won't change color


I'm trying to make a gauge with a blue color using the flexdashboard package in R, however I can't seem to change the color of the gauge. It seems that it only comes in three preset colors of success, warning, and danger. My problem is that I can't seem to change success to blue. Here's my code

library(flexdashboard)

gauge(20, 
          min = 0, 
          max = 100,
          symbol = "%",
          sectors = gaugeSectors(success = c(0, 0.4),
                                 warning = c(0.4, 0.6),
                                 danger = c(0.6, 1)
          )                              )

Solution

  • You can modify colors via the colors argument in the gaugeSectors function. As the help states ?gaugeSectors:

    Colors can be standard theme colors ("success", "warning", "danger", "primary", and "info") or any other valid CSS color specifier

    So you need to add a colors argument like this: colors = c("blue", rgb(0, 1, 0), "#CC664D")

    And also you made a mistake while specifying the value ranges in gaugeSectors: your minimum and maximum values are 0 and 100, so you need to provide values within this range:

    success = c(0, 40),
    warning = c(40, 60),
    danger = c(60, 100)
    

    Please note that the symbol "%" doesn't actually convert the value to percentage, it is just a string that is printed after the value.

    gauge(20, 
          min = 0, 
          max = 100,
          symbol = "%",
          sectors = gaugeSectors(success = c(0, 40),
                                 warning = c(40, 60),
                                 danger = c(60, 100),
                                 colors = c("blue", rgb(0, 1, 0), "#CC664D")
          )
    )
    

    Result