alanarazi7
alanarazi7

Reputation: 65

Plot a map in R - show only external borders

My problem is with an Israel map, but I'd ask the question with US data which exists in the maps library to make it easier. I guess it should work.

I know how to control the borders of a map both on the basic plot and in ggplot. I can draw it with or without plots. However, I would like sometimes to draw only the external borders of the map, and not the borders across states (like in the drawing below).

library(ggplot2)
library(maps)
all_states <- map_data("state")
ggplot(all_states, aes(x=long, y=lat, group=group, fill = region)) + 
geom_polygon(color = "black", size = 1) + coord_equal(ratio=1)  

Another question which is a bit more complex:

Let's now imagine I have a way to group specific states to 'regions'. Let's imagine we apply a category of 'south' and 'north' to every state. Then I would like to differentiate and be able to control 3 different type of borders - (1) the external border of the country; (2) the external border of every region; (3) the external border of each state.

Thanks! Alan

Upvotes: 2

Views: 4512

Answers (3)

rnahumaf
rnahumaf

Reputation: 501

As answered by Camille, you can use dplyr::summarise() to merge the borders of a shapefile. For example:

library(dplyr)
library(ggplot2)


shp <- ... # Load your shapefile here

shp %>%
  summarise() %>%
    ggplot() +
      geom_sf()

Upvotes: 1

hrbrmstr
hrbrmstr

Reputation: 78792

Or, use a proper shapefile:

library(rgdal)
library(ggplot2)

if (!file.exists("israel_geojson.tgz")) download.file("https://s3.amazonaws.com/osm-polygons.mapzen.com/israel_geojson.tgz", "israel_geojson.tgz")

(untar("israel_geojson.tgz", list = TRUE))
##  [1] "./israel/"                          "./israel/admin_level_2.geojson"    
##  [3] "./israel/admin_level_other.geojson" "./israel/admin_level_95.geojson"   
##  [5] "./israel/admin_level_11.geojson"    "./israel/admin_level_94.geojson"   
##  [7] "./israel/admin_level_5.geojson"     "./israel/regions.geojson"          
##  [9] "./israel/admin_level_1.geojson"     "./israel/admin_level_12.geojson"   
## [11] "./israel/admin_level_3.geojson"     "./israel/admin_level_7.geojson"    
## [13] "./israel/admin_level_0.geojson"     "./israel/admin_level_13.geojson"   
## [15] "./israel/admin_level_10.geojson"    "./israel/admin_level_6.geojson"    
## [17] "./israel/admin_level_15.geojson"    "./israel/admin_level_4.geojson"    
## [19] "./israel/admin_level_9.geojson"     "./israel/admin_level_8.geojson"    

According to Mapzen, their admin layer 2 is the outline.

israel <- readOGR("./israel/admin_level_2.geojson")
israel_map <- fortify(israel)

ggplot() +
  geom_map(data=israel_map, map=israel_map, aes(long, lat, map_id=id),
           color="#2b2b2b", fill="white") +
  ggalt::coord_proj("+proj=aeqd +lat_0=31.471357089512118 +lon_0=35.189208984375") +
  ggthemes::theme_map()

enter image description here

Upvotes: 4

Mateusz1981
Mateusz1981

Reputation: 1867

With USA it is easy

library(ggplot2)
library(maps)
border <- map_data("usa")

border is a data frame so you can manipulate it ex:

border %>% mutate(part = ifelse(region == "main", "south", "north"))

Then you can plot

 all_states <- map_data("state")
    ggplot(all_states, aes(x=long, y=lat, group=group, fill = region)) + 
      geom_polygon(color = "red", size = 5, data = border) + coord_equal(ratio=1) +
      geom_polygon(size = 1) + coord_equal(ratio=1) 

or you can draw whatever you want

However I doubt that there are information about israel regions in the maps package. You need to download shape files that contain the information about the israel borders. There are here http://www.gadm.org/country (Rspatial polygon). You will google out what you can do with them

Upvotes: -1

Related Questions