how do I map (on a geographical map) data in R just given the US Zipcodes

前端 未结 1 1956
暗喜
暗喜 2021-01-27 00:23

for a school project I have to map some data on a geographical map in R. Therefore I\'ve got some data containing the zipcode and many other information (just no more informatio

相关标签:
1条回答
  • 2021-01-27 00:54

    Take a look at the R zipcode package; the website contains some examples. The package features geographical coordinates of all zipcodes, so it will be trivial to show them on a map.

    Here is another pointer into the right direction: install the package "maps" and "zipcode". Load both of them into your environment:

    library( zipcode ) ; library( maps )
    

    Now plot the map of the US:

    map( "usa" )
    

    Load the zipcode data

    data( "zipcode" )
    

    Say, you have some zipcodes, for example 90001, 46243, 32920 and you want to show them on the map.

    selected <- zipcode[ zipcode$zip %in% c( "90001", "46243", "32920" ), ]
    

    The selected data frame contain information about the zipcodes. Plot them.

    points( selected$longitude, selected$latitude, pch= 19, cex= 2 )
    text( selected$longitude, selected$latitude, selected$zip, pos=3, cex= 2 )
    

    Here is the result:

    enter image description here

    0 讨论(0)
提交回复
热议问题