2

I am not sure why this piece of code works

library(leaflet)
library(raster)
adm <- getData('GADM', country='SGP', level=1)
popup <- paste0("<strong>Name: </strong>", 
                adm$NAME_1)

leaflet() %>% 
  addTiles() %>% 
  addPolygons(data=adm, weight = 2, fillColor = "yellow", popup=popup)


> head(adm)
       GID_0    NAME_0   GID_1     NAME_1 VARNAME_1 NL_NAME_1 TYPE_1 ENGTYPE_1 CC_1 HASC_1
260003   SGP Singapore SGP.1_1    Central      <NA>      <NA> Region    Region <NA>   <NA>
260001   SGP Singapore SGP.2_1       East      <NA>      <NA> Region    Region <NA>   <NA>
259999   SGP Singapore SGP.3_1      North      <NA>      <NA> Region    Region <NA>   <NA>
260000   SGP Singapore SGP.4_1 North-East      <NA>      <NA> Region    Region <NA>   <NA>
260002   SGP Singapore SGP.5_1       West      <NA>      <NA> Region    Region <NA>   <NA>

enter image description here

and why this doesn't works:

################################

library(leaflet)
library(raster)
library(rgdal)

adm <- readOGR("MP14_SUBZONE_WEB_PL.shp")

popup <- paste0("<strong>Name: </strong>", 
                adm$SUBZONE_N)

leaflet() %>% 
  addTiles() %>% 
  addPolygons(data=adm, weight = 2, fillColor = "yellow", popup=popup)

enter image description here

The difference between them is the shape file.

head(adm)
  OBJECTID SUBZONE_NO      SUBZONE_N SUBZONE_C CA_IND      PLN_AREA_N PLN_AREA_C       REGION_N REGION_C
0        1          1   MARINA SOUTH    MSSZ01      Y    MARINA SOUTH         MS CENTRAL REGION       CR
1        2          1   PEARL'S HILL    OTSZ01      Y          OUTRAM         OT CENTRAL REGION       CR
2        3          3      BOAT QUAY    SRSZ03      Y SINGAPORE RIVER         SR CENTRAL REGION       CR
3        4          8 HENDERSON HILL    BMSZ08      N     BUKIT MERAH         BM CENTRAL REGION       CR
4        5          3        REDHILL    BMSZ03      N     BUKIT MERAH         BM CENTRAL REGION       CR
5        6          7 ALEXANDRA HILL    BMSZ07      N     BUKIT MERAH         BM CENTRAL REGION       CR
           INC_CRC FMEL_UPD_D   X_ADDR   Y_ADDR SHAPE_Leng SHAPE_Area
0 5ED7EB253F99252E 2014/12/05 31595.84 29220.19   5267.381  1630379.3
1 8C7149B9EB32EEFC 2014/12/05 28679.06 29782.05   3506.107   559816.2
2 C35FEFF02B13E0E5 2014/12/05 29654.96 29974.66   1740.926   160807.5
3 3775D82C5DDBEFBD 2014/12/05 26782.83 29933.77   3313.625   595428.9
4 85D9ABEF0A40678F 2014/12/05 26201.96 30005.70   2825.594   387429.4
5 9D286521EF5E3B59 2014/12/05 25358.82 29991.38   4428.913  1030378.8
2

Your data must be projected to WGS84 before plotting:

adm_proj <- spTransform(adm, ‘+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs’)

leaflet() %>% 
  addTiles() %>% 
  addPolygons(data=adm_proj, weight = 2, fillColor = "yellow", popup=popup)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.