Got Plot

Gauge

Gauge


 
# function to create a circle 
circle <- function(center=c(0,0), radius=1, npoints=100) 
{ 
  r = radius 
  tt = seq(0, 2*pi, length=npoints) 
  xx = center[1] + r * cos(tt) 
  yy = center[1] + r * sin(tt) 
  return(data.frame(x = xx, y = yy)) 
} 
 
# external circle 
border_cir = circle(c(0,0), radius=1, npoints = 100) 
 
# gray border circle 
external_cir = circle(c(0,0), radius=0.97, npoints = 100) 
 
# major tick marks 
ticks <- function(center=c(0,0), from=0, to=2*pi, radius=0.9, npoints=5) 
{ 
  r = radius 
  tt = seq(from, to, length=npoints) 
  xx = center[1] + r * cos(tt) 
  yy = center[1] + r * sin(tt) 
  return(data.frame(x = xx, y = yy)) 
} 
 
# major ticks 
major_ticks_out = ticks(c(0,0), from=5*pi/4, to=-pi/4, radius=0.9, 5) 
major_ticks_in = ticks(c(0,0), from=5*pi/4, to=-pi/4, radius=0.75, 5) 
 
# minor ticks 
tix1_out = ticks(c(0,0), from=5*pi/4, to=5*pi/4-3*pi/8, radius=0.9, 6) 
tix2_out = ticks(c(0,0), from=7*pi/8, to=7*pi/8-3*pi/8, radius=0.9, 6) 
tix3_out = ticks(c(0,0), from=4*pi/8, to=4*pi/8-3*pi/8, radius=0.9, 6) 
tix4_out = ticks(c(0,0), from=pi/8, to=pi/8-3*pi/8, radius=0.9, 6) 
tix1_in = ticks(c(0,0), from=5*pi/4, to=5*pi/4-3*pi/8, radius=0.85, 6) 
tix2_in = ticks(c(0,0), from=7*pi/8, to=7*pi/8-3*pi/8, radius=0.85, 6) 
tix3_in = ticks(c(0,0), from=4*pi/8, to=4*pi/8-3*pi/8, radius=0.85, 6) 
tix4_in = ticks(c(0,0), from=pi/8, to=pi/8-3*pi/8, radius=0.85, 6) 
 
# slices (yellow and red) 
slice2xy <- function(t, rad)  
{ 
  t2p = -1 * t * pi + 10*pi/8 
  list(x = rad * cos(t2p), y = rad * sin(t2p)) 
} 
 
# yellow slice 
yellowFrom = 75 
yellowTo = 90 
yel_ini = (yellowFrom/100) * (12/8) 
yel_fin = (yellowTo/100) * (12/8) 
Syel = slice2xy(seq.int(yel_ini, yel_fin, length.out = 30), rad=0.9) 
 
# red slice 
redFrom = 90 
redTo = 100 
red_ini = (redFrom/100) * (12/8) 
red_fin = (redTo/100) * (12/8) 
 
 
# ======================================================== 
# Plot 
# ======================================================== 
# open plot 
plot(border_cir$x, border_cir$y, type="n", asp=1,  
     xlim=c(-1.05,1.05), ylim=c(-1.05,1.05)) 
# yellow slice 
polygon(c(Syel$x, 0), c(Syel$y, 0),   
        border = "orange", col = "orange", lty = NULL) 
# red slice 
polygon(c(Sred$x, 0), c(Sred$y, 0),   
        border = "tomato2", col = "tomato2", lty = NULL) 
# add white central cirle to hide slices 
points(0, 0, col="white", pch=19, cex=30) 
# add gray border 
lines(external_cir$x, external_cir$y, col="gray85", lwd=20) 
# add external border 
lines(border_cir$x, border_cir$y, col="gray20", lwd=2) 
# add minor ticks 
arrows(x0=tix1_out$x, y0=tix1_out$y, x1=tix1_in$x, y1=tix1_in$y, 
       length=0, lwd=2.5, col="gray55") 
arrows(x0=tix2_out$x, y0=tix2_out$y, x1=tix2_in$x, y1=tix2_in$y, 
        length=0, lwd=2.5, col="gray55") 
arrows(x0=tix3_out$x, y0=tix3_out$y, x1=tix3_in$x, y1=tix3_in$y, 
       length=0, lwd=2.5, col="gray55") 
arrows(x0=tix4_out$x, y0=tix4_out$y, x1=tix4_in$x, y1=tix4_in$y, 
       length=0, lwd=2.5, col="gray55") 
# add major ticks 
arrows(x0=major_ticks_out$x, y0=major_ticks_out$y, 
       x1=major_ticks_in$x, y1=major_ticks_in$y, length=0, lwd=4) 
# add value 
value = 60 
text(0, -0.65, value, cex=4) 
# prepare needle for value 
val = (value/100) * (12/8) 
v = -1 * val * pi + 10*pi/8 
# value coordinates for arrow 
val_x = 0.7 * cos(v) 
val_y = 0.7 * sin(v) 
# add needle 
arrows(0, 0, val_x, val_y, col="#f38171", lwd=7) 
# add central point 
points(0, 0, col="#2e9ef3", pch=19, cex=5) 
 
# value coordinates for arrow 
# prepare needle for value 
v0 = -1 * 0 * pi + 10*pi/8 
z0x = 0.65 * cos(v0) 
z0y = 0.65 * sin(v0) 
v100 = -1 * 12/8 * pi + 10*pi/8 
z100x = 0.65 * cos(v100) 
z100y = 0.65 * sin(v100) 
text(z0x, z0y, labels="0", col="gray50") 
text(z100x, z100y, labels="100", col="gray50")