# Gauge Chart in R

How to create a Gauge Chart in R with Plotly

New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

### Basic Gauge

A radial gauge chart has a circular arc, which displays a single value to estimate progress toward a goal. The bar shows the target value, and the shading represents the progress toward that goal. Gauge charts, known as speedometer charts as well. This chart type is usually used to illustrate key business indicators.

library(plotly)

fig <- plot_ly(
domain = list(x = c(0, 1), y = c(0, 1)),
value = 270,
title = list(text = "Speed"),
type = "indicator",
mode = "gauge+number")
fig <- fig %>%
layout(margin = list(l=20,r=30))

fig


### Add Steps, Threshold, and Delta

The following examples include "steps" attribute shown as shading inside the radial arc, "delta" which is the difference of the value and goal (reference - value), and "threshold" to determine boundaries that visually alert you if the value cross a defined threshold.

library(plotly)

fig <- plot_ly(
domain = list(x = c(0, 1), y = c(0, 1)),
value = 450,
title = list(text = "Speed"),
type = "indicator",
mode = "gauge+number+delta",
delta = list(reference = 380),
gauge = list(
axis =list(range = list(NULL, 500)),
steps = list(
list(range = c(0, 250), color = "lightgray"),
list(range = c(250, 400), color = "gray")),
threshold = list(
line = list(color = "red", width = 4),
thickness = 0.75,
value = 490)))
fig <- fig %>%
layout(margin = list(l=20,r=30))

fig


### Custom Gauge Chart

library(plotly)

fig <- plot_ly(
type = "indicator",
mode = "gauge+number+delta",
value = 420,
title = list(text = "Speed", font = list(size = 24)),
delta = list(reference = 400, increasing = list(color = "RebeccaPurple")),
gauge = list(
axis = list(range = list(NULL, 500), tickwidth = 1, tickcolor = "darkblue"),
bar = list(color = "darkblue"),
bgcolor = "white",
borderwidth = 2,
bordercolor = "gray",
steps = list(
list(range = c(0, 250), color = "cyan"),
list(range = c(250, 400), color = "royalblue")),
threshold = list(
line = list(color = "red", width = 4),
thickness = 0.75,
value = 490)))
fig <- fig %>%
layout(
margin = list(l=20,r=30),
paper_bgcolor = "lavender",
font = list(color = "darkblue", family = "Arial"))

fig


# Reference

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new() app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
) 