Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I worked this out last night over at Talk Stats (<a href="http://www.talkstats.com/showthread.php/23324-An-R-learning-project-%28feel-free-to-learn-with-me%29?p=77869&amp;viewfull=1#post77869">link</a>), it's actually pretty easy (as a product of the hours I spent into the early morning!) if you use the R spatial package (<em>sp</em>). I tested some of their other functions to create a <em>SpatialPolygons</em> object that you can use <em>coordinates</em> on to return a polygon centroid. I only did it for one county, but the <em>label point</em> of a <em>Polygon</em> (S4) object matched the centroid. Assuming this is true, then label points of Polygon objects are centroids. I use this little process to create a data frame of centroids and use them to plot on a map. </p> <pre><code>library(ggplot2) # For map_data. It's just a wrapper; should just use maps. library(sp) library(maps) getLabelPoint &lt;- # Returns a county-named list of label points function(county) {Polygon(county[c('long', 'lat')])@labpt} df &lt;- map_data('county', 'new york') # NY region county data centroids &lt;- by(df, df$subregion, getLabelPoint) # Returns list centroids &lt;- do.call("rbind.data.frame", centroids) # Convert to Data Frame names(centroids) &lt;- c('long', 'lat') # Appropriate Header map('county', 'new york') text(centroids$long, centroids$lat, rownames(centroids), offset=0, cex=0.4) </code></pre> <p>This will not work well for every polygon. Very often the process of labeling and annotation in GIS requires that you adjust labels and annotation for those peculiar cases that do not fit the automatic (systematic) approach you want to use. The code-look-recode approach we would take to this is not apt. Better to include a check that a label of a given size for the given plot will fit within the polygon; if not, remove it from the record of text labels and manually insert it later to fit the situation--e.g., add a leader line and annotate to the side of the polygon or turn the label sideways as was displayed elsewhere. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload