# File albers.legend # Date Sept. 19, 1993 # March 9, 2005 # format breaks and left align # Oct 31, 2006: R version, add palette argument # # By Dan Carr # change underscore to arrows # # Purpose Function to produce the legend for my albers # hexagon mosaic map of the U.S. # albers.legend = function(percents,breaks,palette,brk.rnd=1){ # Constants to shift the legend_______________________ leg.x.cen = 2.21 #2.18 leg.x.dx = .05 leg.x.fact = 1.6 leg.ltext = leg.x.cen-leg.x.dx-.04 #2.11 leg.rtext = leg.x.cen+leg.x.dx+.04 #2.48 leg.y.min = .30 leg.y.max = 2.08 #2.1 leg.y.dy = .075 # Calculated Values_________________________________ leg.y.r = leg.y.max-leg.y.min if(!missing(percents)){ leg.polyy = leg.y.min + leg.y.r*c(0,percents,100)/100 }else{ leg.polyy = leg.y.min + leg.y.r* (breaks-min(breaks))/(max(breaks)-min(breaks)) } leg.px = leg.x.cen + c(-leg.x.dx,leg.x.dx,leg.x.dx,-leg.x.dx) npolyy = length(leg.polyy) ncells = npolyy-1 # Plot rectangles____________________________________ # Comment: The for loop can be avoided for (i in 1:ncells){ leg.py = c(rep(leg.polyy[i],2),rep(leg.polyy[i+1],2)) polygon(leg.px,leg.py,col=palette[i]) polygon(leg.px,leg.py,density=0,col=9) } # Plot line segments________________________________ nsegs = ncells-1 # x.seg = leg.x.cen + leg.x.fact*c(-leg.x.dx,leg.x.dx) # leg.x.seg = rep(c(x.seg,NA),nsegs) # leg.y.seg = rep(leg.polyy[2:ncells],rep(3,nsegs)) # lines(leg.x.seg,leg.y.seg,col=1) # Plot Percents and Class Breaks_____________________ par(cex=.9) if(!missing(percents)) text(rep(leg.ltext,nsegs), leg.polyy[2:ncells], paste(percents,'%',sep=''),adj=1) text(rep(leg.rtext,nsegs), leg.polyy[2:ncells], format(round(breaks[2:ncells],brk.rnd)),adj=0) # Plot Max and Min ____________________________________ text(leg.x.cen,leg.polyy[npolyy]+leg.y.dy+.02, paste('Max',round(breaks[npolyy],brk.rnd),sep='='), adj=.5) text(leg.x.cen,leg.polyy[1]-leg.y.dy, paste('Min',round(breaks[1],brk.rnd),sep='='), adj=.5) "finished: albers.legend" }