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?
1 Answer 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.
muRLreference manual) that if your input data.frame (data) contains a columnzipwith your zip codes and a columngroupwith the number of the group to which they belong you can probably do something like this:zip.plot(data, col=data$group)