Display SpatialPolygonsDataFrame on the elevator map with R

I would like to display the polygon of Canada on a map flyer.

# create map
library(leaflet)
m = leaflet() %>% addTiles()
m

I managed to find the polygon for Canada: http://www.gadm.org/country . I chose the SpatialPolygonsDataFrame format for R, but there are other formats available (such as Shapefile)

# load object in R
load("country_polygons/CAN_adm0.RData")
pol_can <- gadm

How can I display a form on a map? I suppose I need to use the sp package, but I could not figure out how to do this. Many thanks for your help!

+4
source share
1 answer

You can pass an object to a SpatialPolygons*function addPolygonsin accordance with section 2.2 of the document here .

( , ~ 11,4 ):

library(sp)
download.file('http://biogeo.ucdavis.edu/data/gadm2/R/CAN_adm0.RData', f <- tempfile())
load(f)
leaflet() %>% addTiles() %>% addPolygons(data=gadm, weight=2)

enter image description here

, GADM getData raster:

library(raster)
can <- getData('GADM', country='VAT', level=0)

, Natural Earth. , 1: 50 000 000 (Admin 0) Natural Earth, . 1 .

library(rgdal)
library(leaflet)

download.file(file.path('http://www.naturalearthdata.com/http/',
                        'www.naturalearthdata.com/download/50m/cultural',
                        'ne_50m_admin_0_countries.zip'), 
              f <- tempfile())
unzip(f, exdir=tempdir())

world <- readOGR(tempdir(), 'ne_50m_admin_0_countries', encoding='UTF-8')

commonwealth <- c("Antigua and Barb.", "Australia", "Bahamas", "Bangladesh", 
  "Barbados", "Belize", "Botswana", "Brunei", "Cameroon", "Canada", "Cyprus",
  "Dominica", "Fiji", "Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya",
  "Kiribati", "Lesotho", "Malawi", "Malaysia", "Maldives", "Malta", "Mauritius",
  "Mozambique", "Namibia", "Nauru", "New Zealand", "Nigeria", "Pakistan", "Papua
  New Guinea", "Rwanda", "St. Kitts and Nevis", "Saint Lucia", "St. Vin. and
  Gren.", "Samoa", "Seychelles", "Sierra Leone", "Singapore", "Solomon Is.",
  "South Africa", "Sri Lanka", "Swaziland", "Tanzania", "Tonga", "Trinidad and
  Tobago", "Tuvalu", "Uganda", "United Kingdom", "Vanuatu", "Zamibia")

leaflet() %>% addTiles() %>% 
  addPolygons(data=subset(world, name %in% commonwealth), weight=2)

enter image description here

+12

All Articles