Search code examples
rgoogle-mapsggplot2ggmap

Draw boundaries using groups and order for each region gg_map Rgooglemaps


I'm trying to plot custom boundaries (dma's) into a google map.

library(ggmap)
library(ggplot2)

US <- get_map(location = c(-95.7129,37.0902),zoom = 3)
US_map <- ggmap(US)

then using the following CSV file with the coordinates groups and order

https://www.dropbox.com/s/3xv192k5401np4r/DMAs%20coordinates%20sample.csv?dl=0

Then I can plot the coordinates using dots:

smpl <- read.csv('DMAs coordinates sample.csv')
US_map + geom_point(data=smpl,aes(x=Longitude, y=Latitude),size=0.01)

US map for sample of 6 dma's

But I would like to plot lines connected by these dots using the path from point_order and group each set of lines using dma_boundary.UniqueID I'm sure there's a way to do this. but I cannot find the right way.


Solution

  • I found a way to do this,

    Using geom_path for each group represented by dma_boundaty.UniqueID

    As the data is already sorted by sub_polygon_id and point_order we use geom_path to use the order set in data. Then,

    library(ggplot2)
    library(data.table)
    library(ggmap)
    
    dma_boundaty <- data.table(read.csv('.../path')
    
        US <- get_map(location = c(-95.7129,37.0902),zoom = 3)
        US_map <- ggmap(US)
        dma_map <- US_map + lapply(0:205,function(i) {
                      geom_path(data=dma_boundary[dma_boundary.UniqueID == i,],aes(x=Longitude, y=Latitude))})
    

    enter image description here