[GIS] Alternative North Arrow using R GIStools package

cartographynorth-arrowr

I'm really digging the R package GISTools for making some basic maps. The default north arrow, created with north.arrow looks ok, but I'm wondering if it would be possible to use a different north arrow. It seems like no other arrow is available in the package, so how could I create my own north arrow? The documentation for the package states

Draws a north arrow on a map. The arrow itself is drawn using polygon and any extra parameters are passed to this call.

So I would need to create the arrow as a polygon? How could I implement this? And would it be possible to modify the source code of north.arrow to offer several different arrows?

Best Answer

I found this paper from the Journal of Statistical Software (https://www.jstatsoft.org/article/view/v019c01/v19c01.pdf)

It provides the following function:

    northarrow <- function(loc,size,bearing=0,cols,cex=1,...) {
  # checking arguments
  if(missing(loc)) stop("loc is missing")
  if(missing(size)) stop("size is missing")
  # default colors are white and black
  if(missing(cols)) cols <- rep(c("white","black"),8)
  # calculating coordinates of polygons
  radii <- rep(size/c(1,4,2,4),4)
  x <- radii[(0:15)+1]*cos((0:15)*pi/8+bearing)+loc[1]
  y <- radii[(0:15)+1]*sin((0:15)*pi/8+bearing)+loc[2]
  # drawing polygons
  for (i in 1:15) {
    x1 <- c(x[i],x[i+1],loc[1])
    y1 <- c(y[i],y[i+1],loc[2])
    polygon(x1,y1,col=cols[i])
  }
  # drawing the last polygon
  polygon(c(x[16],x[1],loc[1]),c(y[16],y[1],loc[2]),col=cols[16])
  # drawing letters
  b <- c("E","N","W","S")
  for (i in 0:3) text((size+par("cxy")[1])*cos(bearing+i*pi/2)+loc[1],
                      (size+par("cxy")[2])*sin(bearing+i*pi/2)+loc[2],b[i+1],
                      cex=cex)
}

I got it to work doing this: library(GISTools) data(newhaven) plot(blocks) xy = c(530000,160000)#use locator() to get the x,y values for arrow placement northarrow(loc = xy, size = 10000)#finding the correct size value is a guessing game

You can fiddle with the polygon commands, enter image description herebut this one looks pretty nice. --Also there is another arrow available in the prettymapr package. Cheers, Lewis

Related Question