sgsg sgsg - 2 months ago 15
R Question

How to create a map with zipcode dataset? ( US Zipcode )


How to create a map in R with zipcode dataset?


data <- c("22313","10100","25001")
[example zipcode]

In this moment, I have a dataset full of zipcode and I would like to print in a map and save it in pdf.

Best regards,

E.P.

Answer

Try the following code which I modified from January at how do I map (on a geographical map) data in R just given the US Zipcodes:

pdf("myzipcodes.pdf")
library(maps)
map(database="usa")# national boundaries

library(zipcode)
data("zipcode")
myzips <- c("22313","83701","32301")
selected <- zipcode[ zipcode$zip %in% myzips, ]
points( selected$longitude, selected$latitude, pch= 19, cex= 2 )
text( selected$longitude, selected$latitude, selected$zip, pos=3, cex= 2 )
dev.off()

You provided two phony zip codes so I added a couple real ones.

In RStudio you can also export a plot as a PDF file using the 'export' menu.

Comments