1

I have a data frame that plots the urban area units from the Timaru District, New Zealand. To give you some idea of the size of the map, this is the Wiki page on Timaru showing geographical location and a rough idea of size.

I have my tmap of the Timaru Urban Area, a subset of the Timaru District:

 tm_shape(TimaruUrbanArea) +
  tm_polygons("AU2013_label",
          col = "lightgrey",
          fill.scale = tm_scale_ordinal(values = c("Fairview-Scarborough" = "#41AB5D","Fraser Park" = "#41AB5D", 
                                                   "Gleniti" = "#41AB5D", "Glenwood" = "#41AB5D",
                                                   "Highfield" = "#41AB5D", "Maori Park" = "#41AB5D",
                                                   "Marchwiel" = "#41AB5D", "Otipua Creek-Washdyke Flat" = "#41AB5D",
                                                   "Parkside" = "#41AB5D", "Redruth" = "#41AB5D",
                                                   "Seaview" = "#41AB5D", "Timaru Gardens" = "#41AB5D",
                                                   "Waimataitai" = "#41AB5D", "Washdyke" = "#41AB5D",
                                                   "Watlington" = "#41AB5D"))
          ) + 
tm_layout(legend.show = FALSE, frame = FALSE)

which gives me this map

tmap plot output of the Urban Areas

The str of the TimaruUrbanArea file is

 Classes ‘sf’ and 'data.frame': 15 obs. of  13 variables:
 $ AU2013_V1_  : num  598201 598202 598700 598800 598900 ...
 $ AU2013_V_1  : chr  "Fairview-Scarborough" "Otipua Creek-Washdyke Flat" "Washdyke" "Waimataitai" ...
 $ AREA_SQ_KM  : num  21.63 28.13 5.48 1.03 2.17 ...
 $ LAND_AREA_  : num  21.63 28.13 5.48 1.03 2.17 ...
 $ Shape_Leng  : num  22567 42556 12274 5984 9532 ...
 $ AU2013_label: chr  "Fairview-Scarborough" "Otipua Creek-Washdyke Flat" "Washdyke" "Waimataitai" ...
 $ UA2013_code : int  113 113 113 113 113 113 113 113 113 113 ...
 $ UA2013_label: chr  "Timaru" "Timaru" "Timaru" "Timaru" ...
 $ TA2013_code : int  64 64 64 64 64 64 64 64 64 64 ...
 $ TA2013_label: chr  "Timaru District" "Timaru District" "Timaru District" "Timaru District" ...
 $ DHB_code    : int  19 19 19 19 19 19 19 19 19 19 ...
 $ DHB_label   : chr  "South Canterbury" "South Canterbury" "South Canterbury" "South Canterbury" ...
 $ geometry    :sfc_MULTIPOLYGON of length 15; first list element: List of 1
  ..$ :List of 1
  .. ..$ : num [1:294, 1:2] 1456046 1456563 1456788 1457034 1457097 ...
  ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
  ..- attr(*, "names")= chr [1:12] "AU2013_V1_" "AU2013_V_1" "AREA_SQ_KM" "LAND_AREA_" ...

What I would like to do is to put little markers on the map, showing where some schools are. The blue upside-down droplet markers on this page, just above the "Link" section heading.

The code there just seems to use:

 tm_shape(cities)+
 tm_markers()

to provide the data frame information, and the type of marker.

I can't get this to work.

I have my schools data frame as:

 TimaruSchoolGeoCoded <- data.frame(SchoolName = c("Barton Rural School", "Bluestone School", "Craighead Diocesan School",
                                              "Gleniti School", "Grantlea Downs School", "Highfield School",
                                              "Mountainview High School", "Oceanview Heights School", "Roncalli College",
                                              "Sacred Heart School (Timaru)", "St Joseph's School (Timaru)",
                                              "Timaru Boys' High School", "Timaru Christian School",
                                              "Timaru Girls' High School","Timaru South School", "Waimataitai School"),
                               Address = c("462 Fairview Road, Fairview, Timaru", "46 Raymond Street, West End, Timaru",
                                           "1 Wrights Avenue, Highfield, Timaru", "22 Heath Street, Gleniti, Timaru",
                                           "65 Grants Road, Marchwiel, Timaru", "26 Rimu Street, Highfield, Timaru",
                                           "97 Pages Road, Timaru", "241 Selwyn Street, Marchwiel, Timaru", 
                                           "19 Wellington Street, Parkside, Timaru", "54 Heaton Street, Parkside, Timaru",
                                           "50 Kelvin Street, Marchwiel, Timaru", "211 North Street, Timaru",
                                           "10 Quarry Road, Watlington, Timaru", "68 Cain Street, Parkside, Timaru",
                                           "44 Queen Street, Parkside, Timaru", "25 Trafalgar Street, Waimataitai, Timaru"),
                               Type = c("Primary", "Primary", "Secondary", "Primary", "Primary", "Primary", "Secondary",
                                        "Primary", "Secondary", "Primary", "Primary", "Secondary", "Primary",
                                        "Secondary", "Primary", "Primary"))

 TimaruSchoolGeoCoded <- TimaruSchoolGeoCoded %>%
   tidygeocoder::geocode(Address, method = "arcgis")

 TimaruSchoolSP <- st_as_sf(TimaruSchoolGeoCoded, coords = c("lat", "long"), crs = 4326)

which gives me point geometry, but I can't get it to plot on my map.

My attempts have included:

 tm_shape(TimaruUrbanArea) +
 tm_polygons("AU2013_label",
             col = "lightgrey",
             fill.scale = tm_scale_ordinal(values = c("Fairview-Scarborough" = "#41AB5D","Fraser Park" = "#41AB5D", 
                                                      "Gleniti" = "#41AB5D", "Glenwood" = "#41AB5D",
                                                      "Highfield" = "#41AB5D", "Maori Park" = "#41AB5D",
                                                      "Marchwiel" = "#41AB5D", "Otipua Creek-Washdyke Flat" = "#41AB5D",
                                                      "Parkside" = "#41AB5D", "Redruth" = "#41AB5D",
                                                      "Seaview" = "#41AB5D", "Timaru Gardens" = "#41AB5D",
                                                      "Waimataitai" = "#41AB5D", "Washdyke" = "#41AB5D",
                                                      "Watlington" = "#41AB5D"))
             ) + 
 tm_layout(legend.show = FALSE, frame = FALSE) +
 
 tm_shape(TimaruSchoolSP$geometry) +
 tm_markers()

And then I thought maybe I needed to give more information to tm_markers(), but this didn't work, either:

 tm_shape(TimaruUrbanArea) +
 tm_polygons("AU2013_label",
             col = "lightgrey" #,
             fill.scale = tm_scale_ordinal(values = c("Fairview-Scarborough" = "#41AB5D","Fraser Park" = "#41AB5D", 
                                                      "Gleniti" = "#41AB5D", "Glenwood" = "#41AB5D",
                                                      "Highfield" = "#41AB5D", "Maori Park" = "#41AB5D", 
                                                      "Marchwiel" = "#41AB5D", "Otipua Creek-Washdyke Flat" = "#41AB5D",
                                                      "Parkside" = "#41AB5D", "Redruth" = "#41AB5D",
                                                      "Seaview" = "#41AB5D", "Timaru Gardens" = "#41AB5D", 
                                                      "Waimataitai" = "#41AB5D", "Washdyke" = "#41AB5D", 
                                                      "Watlington" = "#41AB5D"))
             ) + 
 tm_layout(legend.show = FALSE, frame = FALSE) +
 
 tm_shape(TimaruSchoolSP) +
 tm_markers(text = "SchoolName", dots_shape = marker_icon(), dots_fill = "black")

As the preceding code to draw the urban areas is the same, I will only show the modifications to trying to get the schools to show. I tried

 tm_shape(TimaruSchoolSP) +
 tm_markers(text = "SchoolName", dots_shape = marker_icon(), dots_fill = "blue", dots_size = 100)

and

 tm_shape(TimaruSchoolSP) +
 tm_markers(text = "SchoolName", dots_shape = marker_icon())

This answer suggests it isn't because my points are too small and I tried this answer from a similar question but I couldn't get a dot to render, either.

The area unit data is sourced from here. The shapes file is area-unit-2013.shp. Timaru District is TA2013_code == 64.

1 Answer 1

2

Always have a look at your data before you plot it to check its at least reasonable:

   SchoolName                   Address          Type              geometry
 * <chr>                        <chr>            <chr>          <POINT [°]>
 1 Barton Rural School          462 Fairview Ro… Prim… (-44.40989 171.1718)
 2 Bluestone School             46 Raymond Stre… Prim… (-44.39807 171.2261)
 3 Craighead Diocesan School    1 Wrights Avenu… Seco… (-44.38837 171.2258)

Coordinate should be X-Y order, these numbers aren't valid in EPSG:4326 because you've got a latitude greater than 90... How has this happened?

TimaruSchoolSP <- st_as_sf(TimaruSchoolGeoCoded, coords = c("lat", "long"), crs = 4326)

You should have coords=c("long","lat") here.

Always try the simplest thing before trying complex things like tmap, in this case, a simple plot would have revealed the situation:

> plot(TimaruSchoolSP$geometry)
Warning message:
In st_is_longlat(x) :
  bounding box has potentially an invalid value range for longlat data
1
  • Ah, I got that error message and didn't know what it meant.
    – Michelle
    Commented Jun 2 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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