]> git.vanrenterghem.biz Git - R/project-wa-schools.git/commitdiff
Remove OpenStreetMap dependency. Use geom_sf.
authorFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Mon, 2 Oct 2023 14:30:39 +0000 (22:30 +0800)
committerFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Mon, 2 Oct 2023 14:30:39 +0000 (22:30 +0800)
* Replace OpenStreetMap with maptiles and tidyterra due to sp support being removed from CRAN in Oct 2023.
* Use geom_sf as that is now part of ggplot2 on CRAN.

WA_active_schools.R

index 18816aab1f3537dd90e100aa5d23974f19456503..0811702e1d00d2da6f87af44d384f7f53ec01636 100644 (file)
@@ -1,7 +1,8 @@
 library(sf)
 library(dplyr)
 library(ggplot2)
 library(sf)
 library(dplyr)
 library(ggplot2)
-library(OpenStreetMap)
+library(maptiles)
+library(tidyterra)
 library(viridis)
 library(httr2)
 
 library(viridis)
 library(httr2)
 
@@ -35,32 +36,24 @@ shape.df$totalsecon <- as.numeric(shape.df$totalsecon)
 # filter out remote islands
 shape.df <- filter(shape.df,longitude > 100)
 # Obtain the contour box of the shape
 # filter out remote islands
 shape.df <- filter(shape.df,longitude > 100)
 # Obtain the contour box of the shape
-
 lat <- c(min(shape.df$latitude), max(shape.df$latitude))
 lon <- c(min(shape.df$longitude), max(shape.df$longitude))
 lat <- c(min(shape.df$latitude), max(shape.df$latitude))
 lon <- c(min(shape.df$longitude), max(shape.df$longitude))
-
+# Define Perth metro contours
 lat.metro <- c(-32.1547, -31.8519)
 lon.metro <- c(115.7162,116.0733)
 lat.metro <- c(-32.1547, -31.8519)
 lon.metro <- c(115.7162,116.0733)
-upperLeft.metro <- c(-31.8519,115.7162) 
-lowerRight.metro <- c(-32.1547,116.0733)
-
 # Obtain an openstreetmap background image
 # Obtain an openstreetmap background image
-map <- openmap(upperLeft = c(lat[2],lon[1]), 
-               lowerRight = c(lat[1],lon[2]), 
-               minNumTiles = 6,
-               type="https://stamen-tiles.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}.png")
-map.metro <- openmap(upperLeft = upperLeft.metro,
-                     lowerRight = lowerRight.metro,
-                     minNumTiles = 6,
-                     type="https://stamen-tiles.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}.png")
-
-mapLatLon <- openproj(map)
+sfg_WA <- st_polygon(list(rbind(c(lon[1],lat[1]),
+                                   c(lon[2],lat[1]),
+                                   c(lon[2],lat[2]),
+                                   c(lon[1],lat[2]),
+                                   c(lon[1],lat[1])
+                                   )))
+WATiles <- get_tiles(st_transform(st_sfc(sfg_WA, crs = 4326), 3857), provider = "Stadia.Stamen.TonerLite", crop = TRUE, zoom = 6)
 
 
-OpenStreetMap::autoplot.OpenStreetMap(mapLatLon) +
-  geom_point(data = shape.df, 
-             aes(x = longitude, 
-                 y = latitude,
-                 color = totalschoo),
+ggplot() +
+  geom_spatraster_rgb(data = WATiles) +
+  geom_sf(data = shape.df, 
+             aes(color = totalschoo),
              show.legend = TRUE) +
   scale_color_viridis(option="viridis",
                       name = "Number\nof pupils") +
              show.legend = TRUE) +
   scale_color_viridis(option="viridis",
                       name = "Number\nof pupils") +
@@ -74,23 +67,28 @@ OpenStreetMap::autoplot.OpenStreetMap(mapLatLon) +
        y="")
 
 
        y="")
 
 
-# Zooming in on metro
+# Zooming in on Perth metro
 shape.metro.df <- filter(shape.df, abs(latitude) <= max(abs(lat.metro)),
                          abs(latitude) >= min(abs(lat.metro)),
                          abs(longitude) <= max(abs(lon.metro)),
                          abs(longitude) >= min(abs(lon.metro)))
 shape.metro.df <- filter(shape.df, abs(latitude) <= max(abs(lat.metro)),
                          abs(latitude) >= min(abs(lat.metro)),
                          abs(longitude) <= max(abs(lon.metro)),
                          abs(longitude) >= min(abs(lon.metro)))
-mapLatLon.metro <- openproj(map.metro)
+sfg_metro <- st_polygon(list(rbind(c(lon.metro[1],lat.metro[1]),
+                                   c(lon.metro[2],lat.metro[1]),
+                                   c(lon.metro[2],lat.metro[2]),
+                                   c(lon.metro[1],lat.metro[2]),
+                                   c(lon.metro[1],lat.metro[1])
+                                   )))
+metroTiles <- get_tiles(st_transform(st_sfc(sfg_metro, crs = 4326), 3857), provider = "Stadia.Stamen.TonerLite", crop = TRUE, zoom = 11)
 # Let's look at secondary school only
 # 95th percentile of school size
 secon95th <- quantile(shape.metro.df[,"totalsecon">0]$totalsecon,probs = .95)
 # Let's look at secondary school only
 # 95th percentile of school size
 secon95th <- quantile(shape.metro.df[,"totalsecon">0]$totalsecon,probs = .95)
-OpenStreetMap::autoplot.OpenStreetMap(mapLatLon.metro) +
-  geom_point(data = filter(shape.metro.df, totalsecon > 0), # geom_sf is not yet in ggplot2 on CRAN, so sticking to this for now
-             aes(x = longitude, 
-                 y = latitude,
-                 color = totalsecon,
-                 size = totalsecon)
+ggplot() +
+  geom_spatraster_rgb(data = metroTiles) +
+  geom_sf(data = filter(shape.metro.df, totalsecon > 0),
+             aes(color = totalsecon,
+                 size = totalsecon),
              ) +
              ) +
-  geom_text(data = filter(shape.metro.df, totalsecon > secon95th), # name top 5% schools
+  geom_sf_label(data = filter(shape.metro.df, totalsecon > secon95th), # name top 5% schools
              aes(x = longitude, 
                  y = latitude,
                  label = schoolname),
              aes(x = longitude, 
                  y = latitude,
                  label = schoolname),