1

I'm making a map with arc lines connecting between counties for the US state of Missouri. I've calculated the 'good enough' centres of each county by taking the mean of each polygon's long/lat. This works good for the more or less square-shaped counties, but less so for the more intricately shaped counties. I think that this must be a common occurrence, but I can't find the answer online or with any function I've created. I'd like to use a tidyverse work flow (i.e. not transform to spatial objects if I can help it). Are there any tidyverse solutions to the problem at hand.

You can see the problem in the examples below.

library(tidyverse)

# import all state/county fortified
all_states <- as_tibble(map_data('county'))

# filter for missouri
mo_fortify <- all_states %>%
  filter(region == 'missouri')

## Pull Iron county, which is relatively oddly shaped
mo_iron_fortify <- mo_fortify %>% 
 group_by(subregion) %>% 
  mutate(long_c =  mean(long),
         lat_c = mean(lat),
         iron = ifelse(subregion == 'iron','Iron','Others')) %>% 
  ungroup()  

# map a ggplot2 map
mo_iron_fortify %>% 
  ggplot(aes(long, lat, group = group))+
  geom_polygon(aes(fill = iron), 
               color = 'white')+
  geom_point(aes(long_c, lat_c))+
  scale_fill_grey('Iron county is\na good example')+
  coord_map()+
  theme_bw()

Example from code above

elliot
  • 1,844
  • 16
  • 45
  • How about this over on gis.stackexchange.com? https://gis.stackexchange.com/questions/43543/how-to-calculate-polygon-centroids-in-r-for-non-contiguous-shapes – Mako212 Dec 19 '18 at 23:42
  • 2
    See the `sf` package, and specifically the `st_centroid` function. – mikeck Dec 20 '18 at 00:36
  • 1
    To be honest, trying to do spatial calculations without working with spatial objects is a prime example of "*if all you have is a hammer...*". I know the `sf` package mentioned by @mikeck plays nicely with `ggplot2` and I see no reason why it would not in turn fit with `dplyr` workflows. – thelatemail Dec 20 '18 at 01:54
  • 1
    Yes, the `sf` package is designed to fit with `dplyr`. Use that! – william3031 Dec 20 '18 at 04:51
  • Is this a dupe of https://stackoverflow.com/questions/9441436/ggplot-centered-names-on-a-map – Tyler Rinker Dec 20 '18 at 05:19

0 Answers0