Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:34:27+00:00 2026-05-30T11:34:27+00:00

Early I posted a question about plotting county names on a map using ggplot

  • 0

Early I posted a question about plotting county names on a map using ggplot and maps found HERE. My first approach was to take the means of all the lat and long coordinates per county as seen here:
enter image description here

Thankfully Andrie had 2 suggestions to improve the centering using a center of ranges and then the coord_map() {which appears to keep the aspect ratio correct}. This imporved the centering a great deal as seen here:
enter image description here

I think this looks better but still has some difficulties with overlap problems. I am hoping to further improve the centering (In that same thread Justin suggested a kmeans approach). I am ok with rotating text if necessary but am hoping for names that are centered and rotated if necessary (they extend beyond the county borders) to best display the county names on the map.

Any ideas?

library(ggplot2); library(maps)

county_df <- map_data('county')  #mappings of counties by state
ny <- subset(county_df, region=="new york")   #subset just for NYS
ny$county <- ny$subregion
p <- ggplot(ny, aes(long, lat, group=group)) +  geom_polygon(colour='black', fill=NA)

#my first approach to centering
cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, FUN=mean)
ggplot(ny, aes(long, lat)) +  
    geom_polygon(aes(group=group), colour='black', fill=NA) +
    geom_text(data=cnames, aes(long, lat, label = subregion), size=3)

#Andrie's much improved approach to centering
cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, 
                    FUN=function(x)mean(range(x)))
ggplot(ny, aes(long, lat)) +  
    geom_polygon(aes(group=group), colour='black', fill=NA) +
    geom_text(data=cnames, aes(long, lat, label = subregion), size=3) +
    coord_map()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:34:28+00:00Added an answer on May 30, 2026 at 11:34 am

    As I worked this out last night over at Talk Stats (link), it’s actually pretty easy (as a product of the hours I spent into the early morning!) if you use the R spatial package (sp). I tested some of their other functions to create a SpatialPolygons object that you can use coordinates on to return a polygon centroid. I only did it for one county, but the label point of a Polygon (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.

    library(ggplot2)  # For map_data. It's just a wrapper; should just use maps.
    library(sp)
    library(maps)
    getLabelPoint <- # Returns a county-named list of label points
    function(county) {Polygon(county[c('long', 'lat')])@labpt}
    
    df <- map_data('county', 'new york')                 # NY region county data
    centroids <- by(df, df$subregion, getLabelPoint)     # Returns list
    centroids <- do.call("rbind.data.frame", centroids)  # Convert to Data Frame
    names(centroids) <- c('long', 'lat')                 # Appropriate Header
    
    map('county', 'new york')
    text(centroids$long, centroids$lat, rownames(centroids), offset=0, cex=0.4)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in the early stages of building an app using Rails 3. User
Early on I was doing some debugging and testing using the chrome dev tools(known
I'm in the early stages of an iPhone app and I have a question.
This question is a general one, and I've already posted a version of it
Currently in the early stages of developing a very large project using ASP.Net MVC.
I'm in early (pre-coding) stages of developing a mobile web application using jQuery Mobile
One of the things you learn early on when manipulating a DOM using Javascript
I am early in development on a new ASP.Net MVC project and I using
I'm using Java with hibernate and am in the early stages of learning how
When Jeremy & Chad posted about their FubuMvc project , one of the differentiators

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.