Skip to content

Instantly share code, notes, and snippets.

@z3tt
Created July 23, 2025 15:30
Show Gist options
  • Save z3tt/8723e62bb247f76c76c75cc9ea196448 to your computer and use it in GitHub Desktop.
Save z3tt/8723e62bb247f76c76c75cc9ea196448 to your computer and use it in GitHub Desktop.
A basic gauge chart made with ggplot2 + coord_radial()
library(ggplot2)
value <- 7.3
ggplot() +
# Add gap to bottom
coord_radial(
start = 4/3 * pi, # 240°
end = 2/3 * pi, # 120°
expand = FALSE
) +
# Draw areas
annotate(
geom = "rect",
xmin = c(-Inf, 3, 5), xmax = c(3, 5, Inf),
ymin = .6, ymax = 1,
fill = c("#FF6D5C", "#DFAD43", "#45B4A7")
) +
# Draw ticks
annotate(
geom = "segment",
x = 0:10, xend = 0:10,
y = .6, yend = 1,
linewidth = 1, color = "white"
) +
# Draw needle
annotate(
geom = "pointrange",
x = value, y = 0,
ymin = 0, ymax = 1.05,
size = 2, linewidth = 1.3, lineend = "round"
) +
# This part adds the tick labels (and removes everything else)
scale_x_continuous(breaks = 0:10) +
theme_void() +
theme(axis.text.x = element_text(size = 14))
@BB1464
Copy link

BB1464 commented Aug 8, 2025

This is great, @z3tt. Please, can you share the code snippet on how you added the animation?

@z3tt
Copy link
Author

z3tt commented Aug 11, 2025

@BB1464 For the animation, I mapped over a range of values and glued the images together.
For that, you'd need to wrap it into a function that takes the value and saves the plot first:

draw_gauge <- function(value) {
  [plotting code here]
  
  ggsave(paste0("gauge-", value, ".png")
}

I have put them together manually with an in-browser gif generator but one could use the magick package as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment