public
Created

Custom Legend in R

  • Download Gist
map-new-legend.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
library(OIdata)
library(RColorBrewer)
library(classInt)
 
# load state data from OIdata package:
data(state)
 
# set constants:
nclr <- 8 # number of bins
min <- 0 # theoretical minimum
max <- 100 # theoretical maximum
breaks <- (max - min) / nclr
 
# set up colors:
plotclr <- brewer.pal(nclr, "Oranges")
plotvar <- state$coal
class <- classIntervals(plotvar,
nclr,
style = "fixed",
fixedBreaks = seq(min, max, breaks))
colcode <- findColours(class,
plotclr)
NAColor <- "gray80"
plotclr <- c(plotclr, NAColor)
 
# map data:
map("state", # base
col = NAColor,
fill = TRUE,
lty = 0)
map("state", # data
col = colcode,
fill = TRUE,
lty = 0,
add = TRUE)
map("state", # border
col = "gray",
lwd = 1.4,
lty = 1,
add = TRUE)
 
# set legend text:
legendText <- c()
for(i in seq(min, max - (max - min) / nclr, (max - min) / nclr)) {
if (i == max(seq(min, max - (max - min) / nclr, (max - min) / nclr))) {
legendText <- c(legendText, paste(round(i,3), "\u2264 n \u2264", round(i + (max - min) / nclr,3)))
if (!is.na(NAColor)) legendText <- c(legendText, "NA")
} else
legendText <- c(legendText, paste(round(i,3), "\u2264 n <", round(i + (max - min) / nclr,3)))
}
 
legend("bottomleft", # position
legend = legendText,
title = "Percent",
fill = plotclr,
cex = 0.56,
bty = "n") # border
map-standard-legend.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
library(OIdata)
library(RColorBrewer)
library(classInt)
 
# load state data from OIdata package:
data(state)
 
# set constants:
nclr <- 8 # number of bins
min <- 0 # theoretical minimum
max <- 100 # theoretical maximum
breaks <- (max - min) / nclr
 
# set up colors:
plotclr <- brewer.pal(nclr, "Oranges")
plotvar <- state$coal
class <- classIntervals(plotvar,
nclr,
style = "fixed",
fixedBreaks = seq(min, max, breaks))
colcode <- findColours(class,
plotclr)
 
# map data:
map("state", # base
col = "gray80",
fill = TRUE,
lty = 0)
map("state", # data
col = colcode,
fill = TRUE,
lty = 0,
add = TRUE)
map("state", # border
col = "gray",
lwd = 1.4,
lty = 1,
add = TRUE)
 
legend("bottomleft", # position
legend = names(attr(colcode, "table")),
title = "Percent",
fill = attr(colcode, "palette"),
cex = 0.56,
bty = "n") # border
sessionInfo.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-pc-linux-gnu (64-bit)
 
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
 
attached base packages:
[1] stats graphics grDevices utils datasets methods base
 
other attached packages:
[1] classInt_0.1-21 RColorBrewer_1.0-5 OIdata_1.0 maps_2.3-6
[5] RCurl_1.95-4.1 bitops_1.0-6
 
loaded via a namespace (and not attached):
[1] class_7.3-9 e1071_1.6-1 tools_3.0.1

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.