How to add legend to the chart?

base-r

#1

Following code was found online, it is for bullet graph generation, want to add legend to define the horizontal bar (performance measure) and vertical bar (comparative measure), how to do that? Thank you!

# Prepare palete and function
bulletgraph.palette <- function(n,colored){
  if(colored){ 
    if(n==6) cols = c("pink2","#FFFF33","#00FF4DFF","#00A600FF","lightblue","blue")
    #if(n>5) cols = hsv((n:1 + n*0.3)/(n*1.3),.9,.8+(1:n/(n*5)))
  }
  return(cols)
}

bulletgraph <- function(x,ref,limits, name=NULL, subname="", width=0.4, col=NULL, 
                        palette=NULL,colored=T){
  if(length(limits)<3){
    stop("limits must be a vector with at least three elements")
  }
  if(length(x)!=1){
    stop(paste("x must be a scalar",name))
  }
  if(is.null(name)) name = sys.call()[[2]]
  if(is.null(palette)){
    palette = bulletgraph.palette(length(limits)-1,colored)
  }
  if(is.null(col)){
    if(colored) col="steelblue3"
    else col = "black"
  }
  n = length(limits)
  ranges = matrix(tail(limits,-1)-c(0,head(tail(limits,-1),n-2)),n-1)
  barplot(ranges,col=palette,border=NA,horiz=T,
          xlim=c(min(limits),max(limits)),xpd=F)
  segments(ref,.3,ref,1.1,lwd=3)
  barplot(x[1],width= width,names.arg=name,cex.names=1,
          space=((1-width)/2+0.2)/width,
          add=T,horiz=T,border=NA,col=col,las=1,xpd=F)
  mtext(subname,side=2,line=1,at=0.4,cex=0.6,adj=1,las=2)
  if(limits[1]!=0){
    warning("Bars should be drawn on a zero-based scale: using a jagged base to remark such non conformance.")
    jit = (limits[4] - limits[1])/100
    x = c(rep(c(limits[1],limits[1]+jit),6),0)
    y = c(2:13/10,1.2)
    polygon(x,y,col="white",border="white")
  }
}

# Generate bullet graph'
bulletgraph(x=67.2, ref=80.02, limits=c(0,65.63,68.93,73.39,77.36,81.6,100),
            name="Group-1", subname="(Percentage)")