[R-sig-Geo] Problem plotting state borders

José Luis Rotundo rotundo.jose at gmail.com
Fri May 22 18:16:36 CEST 2015


Dear List,

I want to plot a US county level map with counties colored according a
categorical variable. I was successful for the most part, but when
trying to add state level borders, to make the map look better, the
borders do not follow the geographical limits.

Here is the link to the map I ended up with: http://imgur.com/UqcpVBW

If you want to reproduce the what I am doing, the data is here:
http://www.filedropper.com/data_4

Below is the code I am using.

Thanks a lot in advance for any help to solve this problem.

José L. Rotundo
CONICET
Facultad de Ciencias Agrarias
Univ. Nacional de Rosario
Zavalla, Santa Fe
Argentina

#############################################################
# Pick the colors for the categorical values
colors = c("#CC0000", "#FF9999", "#E0E0E0", "#99FF99", "#00CC00")

#Creates a new variable with the categories.
data$colorBuckets <- as.numeric(cut(data$Slope, c(-0.30, -0.20, -0.10, 0.10,
                                                    0.20, 0.30)))

# Put a 3 (equivalent to slope 0) when there is NA's
data$colorBuckets[is.na(data$colorBuckets)] <- 3

# Matches the values of FIPS with the color categories
colorsmatched <- data$colorBuckets

library(mapproj)
# Map colored counties according the categories
map("county", col = colors[colorsmatched], fill = TRUE, resolution = 0,
    lty = 0, projection = "polyconic")

# Add border around each county
map("county", col = "gray", fill = FALSE, add = TRUE, lty = 1, lwd = 0.2,
    projection = "polyconic")

# Add border to each state (THIS IS THE PROBLEMATIC STEP)
map("state", col = "black", fill = FALSE, add = TRUE, lty = 1, lwd = 0.4,
           projection = "polyconic")

################################################################



More information about the R-sig-Geo mailing list