Choropleth mapping problem in R
EDIT: I figured out the source of my problem. I only have counting information for the counties for which I have data, which is less than the number of counties in the area I'm drawing on.
It is clear that the problematic lines of code are here:
mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]
Does anyone have any advice on how to generate a vector of the appropriate length that will fit # of the counties in NY, NJ, CT and PA from the map library? I want to combine account data that I have and include zeros for counties I do not have information for.
I am trying to follow the tutorial described here: http://www.thisisthegreenroom.com/2009/choropleths-in-r/
The following code executes, but it either doesn't match my dataset with maps_counties data as expected, or it doesn't build it in the order I would expect. For example, the resulting regions for a larger area of New York City do not show density, while the random counties in PA show the highest density.
General format of my datasheet:
county state count
fairfield connecticut 17
hartford connecticut 6
litchfield connecticut 3
new haven connecticut 12
...
...
westchester new york 70
yates new york 1
luzerne pennsylvania 1
Note that this data is ordered by state and then by circumference and includes data for CT, NJ, NY, and PA.
First I read in my dataset:
library(maps)
library(RColorBrewer)
d <- read.table("gissum.txt", sep="\t", header=TRUE)
#Concatenate state and county info to match maps library
d$stcon <- paste(d$state, d$county, sep=",")
#Color bins
colors = brewer.pal(5, "PuBu")
d$colorBuckets <- as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))))
Here is my corresponding
mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]
Drawing:
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = colors[d$colorBuckets[na.omit(match(mapnames ,d$stcon))]]
,fill = TRUE
,resolution = 0
,lty = 0
,lwd= 0.5
)
map("state"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
,lty=1
,lwd=2
)
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
, lty=1
, lwd=.5
)
title(main="Respondent Home ZIP Codes by County")
I'm sure I'm missing something of the basic re: order in which map functions are grouped, but I can't figure out how. Thanks for the help. Please let me know if you need more information.
a source to share
Here is a possible solution to your problem by combining your data with data from selection state maps. Is this what you were looking for?
library(maps);
library(RColorBrewer);
# Create Dummy Data Frame to Play With
d = rbind(c('fairfield','connecticut',17),c('westchester','new york',70), c('luzerne','pennsylvania',1));
d = data.frame(d);
names(d) = c("county", "state", "count");
d$count = as.numeric(as.character(d$count));
d$stcon = paste(d$state, d$county, sep=",");
# Extract mapnames for States
mapnames2 = map("county",c("new york","new jersey", "connecticut", "pennsylvania"),plot=FALSE)[4]$names;
mapnames2 = data.frame(mapnames2);
names(mapnames2) = "stcon";
# Merge with d
d = merge(mapnames2, d, all = T);
d$count[is.na(d$count)] = 0;
# Color bins
colors = brewer.pal(5, "PuBu");
d$colorBuckets = as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))));
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = colors[d$colorBuckets]
,fill = TRUE
,resolution = 0
,lty = 0
,lwd= 0.5
)
map("state"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
,lty=1
,lwd=2
)
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
, lty=1
, lwd=.5
)
title(main="Respondent Home ZIP Codes by County")
a source to share