Title

R 을 이용한 그림

*Gague chart R-bloggers에 소개된 gauge-chart를 코드내용을 살펴본 후 function하나로 정리하여 사용하기 편하게 만들어봤습니다.

gaugechart.ms<-function(X,Y)
  #X는 1~100사이의 수치,Y는 "" 를 사용하여 나타낼 이름입력.
{
  value=X
  label=Y
  circle <- function(center=c(0,0), radius=1, npoints=100)##radius 반지름의 길이를 정할수있다.
  {
    r = radius
    tt = seq(0, 2*pi, length=npoints)
    ##xx,yy는 cos,sin을 이용한 x,y좌표를 나타낸다.
    xx = center[1] + r * cos(tt)
    yy = center[1] + r * sin(tt)
    return(data.frame(x = xx, y = yy))
  }
  slice2xy <- function(t, rad) 
  {
    t2p = -1 * t * pi + 10*pi/8
    list(x = rad * cos(t2p), y = rad * sin(t2p))
  }
  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))
  }
  # 테두리로 큰원을 그린다.
  border_cir = circle(c(0,0), radius=1, npoints = 100)

  # 원안에 작은 원을 그린다.
  external_cir = circle(c(0,0), radius=0.97, npoints = 100)

  # 노란색(다른색으로 바꿀수있음)의 시작과 끝 범위를 지정한다.
  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)

  #빨간색(다른색으로 바꿀수있음)의 시작과 끝 범위를 지정한다.
  redFrom = 90
  redTo = 100
  red_ini = (redFrom/100) * (12/8)
  red_fin = (redTo/100) * (12/8)
  Sred = slice2xy(seq.int(red_ini, red_fin, length.out = 30), rad=0.9)

  # white slice (this will be used to get the yellow and red bands)
  whiteFrom = 74
  whiteTo = 101
  white_ini = (whiteFrom/100) * (12/8)
  white_fin = (whiteTo/100) * (12/8)
  Swhi = slice2xy(seq.int(white_ini, white_fin, length.out = 30), rad=0.8)

  # 굵은 눈금을 표시할 좌표(각도로계산된)와 눈금의 길이를 정하는 코드이다.
  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)

  #작은 눈금을 표시할 좌표(각도로계산된)와 눈금의 길이를 정하는 코드이다.
  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)

  #0, 100을 입력시키기위한 각도와 좌표값들이다.
  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)

  # indicated value, say 80 (you can choose another number between 0-100)

  # angle of needle pointing to the specified value
  val = (value/100) * (12/8)
  v = -1 * val * pi + 10*pi/8
  # x-y coordinates of needle
  val_x = 0.7 * cos(v)
  val_y = 0.7 * sin(v)

  # open plot
  plot(border_cir$x, border_cir$y, type="n", asp=1, axes=FALSE,
       xlim=c(-1.05,1.05), ylim=c(-1.05,1.05),
       xlab="", ylab="")
  # 다각형을 그리는 코드이다.
  polygon(c(Syel$x, 0), c(Syel$y, 0),
          border = "#FF9900", col = "#FF9900", lty = NULL)
  # red slice
  polygon(c(Sred$x, 0), c(Sred$y, 0),
          border = "#DC3912", col = "#DC3912", lty = NULL)
  # white slice
  polygon(c(Swhi$x, 0), c(Swhi$y, 0),
          border = "white", col = "white", lty = NULL)
  # 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)
  # 각도로 계산된 지정된 좌표에 작은 눈금을 그린다.
  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")
  # 해당좌표에 굵은 눈금을 그린다.
  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
  text(0, -0.65, value, cex=4)
  # add label of variable
  text(0, 0.43, label, cex=3.8)
  # add needle
  arrows(0, 0, val_x, val_y, col="#f38171", lwd=7)
  # add central blue point
  points(0, 0, col="#2e9ef3", pch=19, cex=5)
  # add values 0 and 100
  text(z0x, z0y, labels="0", col="gray50")
  text(z100x, z100y, labels="100", col="gray50")

}

함수 실행 결과 입니다.

gaugechart.ms(80, "project")

plot of chunk unnamed-chunk-2