2

In my data, US zip codes are grouped into 4 groups: I need to highlight those zipcodes with different color for each group & to display the state names for those zip codes.

I was trying with zip.plot function of the muRL package.

Using this function I am able to highlight all the zipcodes I'm considering for my project, but not able to use different color scheme for those 4 groups & to display the state names.

How to find a solution?

j0k
  • 22,600
  • 28
  • 79
  • 90
R Learner
  • 545
  • 1
  • 7
  • 13
  • I'm not familiar with this function but I will guess (judging by the content of the package `muRL` reference manual) that if your input data.frame (`data`) contains a column `zip` with your zip codes and a column `group` with the number of the group to which they belong you can probably do something like this: `zip.plot(data, col=data$group)` – plannapus Aug 06 '12 at 11:07
  • 4
    That being said, please consider editting your question according to [this guideline](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by adding a reproducible example and maybe showing the code you tried so far as it would be considerably easier to provide you with adequate answers that way. – plannapus Aug 06 '12 at 11:08

1 Answers1

1

This is probably not the most elegant solution but that should get you in the right direction, I hope.

First some random data:

library(muRL)
data(zips) #This is the file muRL's zip.plot function is calling: I'm gonna use it to extract random zip codes.
zip.data <- data.frame(zip = sample(zips$zip, 10), group = sample(1:4, 10, replace=TRUE))

Then your plot, with different colors per group:

zip.plot(zip.data, col = zip.data$group)

And for the state name, I'm using here the zips table again:

zips[zips$zip %in% zip.data$zip, ] -> zip_subset
text(zip_subset$lon, zip_subset$lat, labels = zip_subset$state, pos = 4, cex = 0.7, font = 2)

enter image description here

For more elegant ways of plotting zip code related data, you should have a look at the answers for this SO question.

Community
  • 1
  • 1
plannapus
  • 18,529
  • 4
  • 72
  • 94