Created
September 29, 2013 23:02
-
-
Save mollietaylor/6757351 to your computer and use it in GitHub Desktop.
Custom Legend in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment