2

I'd like to create a function that takes some inputs based on which it returns a plot object similar to the one shown below:

enter image description here

The two halves are mirror images in terms of dial position and colors but the text in the center and the labels are different. I've seen some examples but they don't fully cover what I need:

  1. ggplot Donut chart
  2. Dial Position Gauge Chart Plotly R,
  3. https://www.r-graph-gallery.com/doughnut-plot.html
  4. Hide labels in plotly donut chart r

sample code

Attempting the top half of the plot first using plotly:

plot_func <- function(current_value){
  fig <- plot_ly(
    domain = list(x = c(0, 1), y = c(0, 1)),
    value = current_value,
    title = list(text = "Rating"),
    type = "indicator",
    mode = "number+gauge",
    gauge = list(
      axis =list(range = list(100, 85)),
      bar = list(
        # color = 'white', 
        # line = list(width = 1), 
        thickness = 0
      ),
      steps = list(
        list(range = c(85,90), color = "#b20000", name = 'E'),
        list(range = c(90,92.5), color = "#e09999", name = 'D'),
        list(range = c(92.5, 95), color = "#ffffb2", name = 'C'),
        list(range = c(95, 97.5), color = '#7fbf7f', name = 'B'),
        list(range = c(97.5,100), color = "#008000", name = 'A')),
      threshold = list(
        line = list(color = "red", width = 4),
        thickness = 0.75,
        value = current_value)))

  return(fig)
}

myplot <- plot_func(current_value = 99.8)

This gives an output: enter image description here

I can't figure out a way to:

  1. Have a continuous color range rather than the step change I have right now
  2. change the red bar for current_value to an arrow instead

Any help is appreciated.

Gautam
  • 2,597
  • 1
  • 28
  • 51

1 Answers1

1

Your first issue with making a gradient can be done with using scale_*_gradient, which "creates a two colour gradient (low-high), scale_*_gradient2 creates a diverging colour gradient (low-mid-high), scale_*_gradientn creates a n-colour gradient." Source

With the following example

scale_colour_gradient(
  ...,
  low = "#132B43",
  high = "#56B1F7",
  space = "Lab",
  na.value = "grey50",
  guide = "colourbar",
  aesthetics = "colour"
)

As for the arrow, there is a geom for that!

geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"))

The issue is dealt with in more detail in this relevant question.

mhovd
  • 3,724
  • 2
  • 21
  • 47
  • Thanks, could you include some sample code for how to incorporate this in `ggplot2` - I'm not sure how to add `scale_colour_gradient` to `plotly`. – Gautam Apr 28 '20 at 16:31
  • @Gautam, have you seen the package `flexdashboard`? It lets you create gauges pretty easily. While the package isn't meant for graphing, its outputs can be exported to an image. https://rdrr.io/cran/flexdashboard/man/gauge.html – mhovd Apr 28 '20 at 16:42
  • Not very familiar with `flexdashboard` - seems interesting. However, for now I'm looking for something that can help me export html images or static images - I was planning on using ggplot2 (for the latter) and plotly + orca for the html ones. I need to make some 1000 such gauges.. – Gautam Apr 29 '20 at 20:15
  • If you are doing this for HTML, I highly recommend you make the gauges in CSS, rather than in R. – mhovd Apr 30 '20 at 11:58