Improve centering county names ggplot & maps

后端 未结 5 1260
一个人的身影
一个人的身影 2020-11-30 07:04

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

5条回答
  •  Happy的楠姐
    2020-11-30 07:20

    This was a very helpful discussion. For the benefit of those who grew up with dplyr, here is a minor tweak, using pipes in place of aggregate:

    library(maps); library(dplyr); library(ggplot2)
    ny <- map_data('county', 'new york') 
    
    cnames1 <- aggregate(cbind(long, lat) ~ subregion, data=ny, 
                         FUN=function(x)mean(range(x)))
    cnames2 <- ny %>% group_by(subregion) %>%
        summarize_at(vars(long, lat), ~ mean(range(.)))
    
    all.equal(cnames1, as.data.frame(cnames2))
    

提交回复
热议问题