ggplot centered names on a map

前端 未结 4 1746
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 03:27

I\'m attempting to use ggplot2 and maps to plot the names of the counties in NY state. My approach was to find the means of latitude and longitude by county (I assume this

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 04:28

    It was pointed out to me by @tjebo while I was trying out to make a new stat, that this stat would be an appropriate solution for this question. It's not on CRAN (yet) but lives on github.

    For other people dealing with a similar problem, here is how that would work:

    library(ggh4x) # devtools::install_github("teunbrand/ggh4x")
    #> Loading required package: ggplot2
    #> Warning: package 'ggplot2' was built under R version 4.0.2
    library(maps)
    
    county_df <- map_data('county')
    ny <- subset(county_df, region=="new york")
    ny$county <- ny$subregion
    
    
    ggplot(ny, aes(x = long, y = lat, group = group)) +  
      geom_polygon(colour='black', fill=NA) +
      stat_midpoint(aes(label = subregion), geom = "text",size=3) +
      coord_map()
    

    Created on 2020-07-06 by the reprex package (v0.3.0)

提交回复
热议问题