R/plot_scaleBar.R

Defines functions scaleBar scalebar npcX npcY

#' scaleBar
#'
#' @param x x
#' 
#' @export
#' @examples
#' data(st)
#' st$plot(axes=F) 
#' scaleBar()
#' 
scaleBar<-function(...){scalebar(...)}
#first fix for the scalebar hang: dont draw it if autodetect of x and y have failed
scalebar<-function(
  #xpos=locX(0),
  #ypos=locY(0),
  xpos=npcX(-.02),
  ypos=npcY(-.02),
  x= {(d<-pretty(par("usr")[1:2], n=10)); d[2]-d[1]},
  y= {(d<-pretty(par("usr")[3:4], n=10)); d[2]-d[1]},
  xUnit="",
  yUnit="",
  xFactor=1, 
  yFactor=1,
  cex=1,
  lwd=2,
  ...) 
{
  
  if (length(x)>0 & length(y)>0){
    if (xUnit=="s" && x<1){
      xUnit="ms"
      xFactor=1e3
    }
    # no clipping
    usr <- par("usr")
    clip(usr[1]-(usr[2]-usr[1]), usr[2]+(usr[2]-usr[1]), usr[3]-(usr[4]-usr[3]), usr[4]+(usr[4]-usr[3]))
    
    #draw scalebar
    if (length(x)>0){
      segments(xpos,ypos, xpos+x,ypos,lwd=lwd)
      if(!x==0){
        text(xpos+(x /2), ypos,paste(xFactor*x ,xUnit), pos=1, cex=cex,...)    
      }
    }
    
    if (length(y)>0){
      segments(xpos,ypos, xpos,ypos+y,lwd=lwd)
      if(!y==0){
        text(xpos, ypos+(y/2),paste(yFactor*y,yUnit), pos=2, cex=cex,...)
      }
    }
    
    #restore normal clipping
    clip(usr[1], usr[2], usr[3], usr[4])
  }
}

npcX<-function(x){   #find xvalue that corresponds to a given fraction of the complete x axis scale
  grconvertX(x,from="npc")
}

npcY<-function(y){   #find yvalue that corresponds to a given fraction of the complete y axis scale
  grconvertY(y,from="npc")
}
tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.