I want to do some spatial statistic analysis with the county-level crop yield data in Nebraska for the STAT class. For that I need the longitude and latitude of the geographic centroids of each county. Anybody know how to do it in R? I know it can be done in ArcGIS but I have no access to it now.
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
You didn't give any details where you got your shapefile from, but I got one from here and you can use
gCentroid
fromrgeos
thusly:You can use the
get_map()
function from theggplot2
package to extract the US county map data from themaps
package to a dataframe. Then you can calculate the mid points of the ranges of the lat/lon columns by county (or whatever method you want to use to define geographic center).You can also extract centroids of
SpatialPolygons*
objects withcoordinates
, though the centroids won't be returned asSpatialPoints
as withrgeos::gCentroid
.For example:
Note that, as pointed out by @Spacedman in the comments, the polygons should be projected to a planar coordinate system first.