繪製全美大專院校地圖
讓我們繼續疊加圖層,將營利性(for-profit)的大專院校加入儲存在 m3 物件中的 leaflet 地圖。
在你以公立、私立與營利性院校作為各自圖層來 print() 出 m3 地圖後,試著先移除三個圖層,再用不同的順序把它們加回地圖。
不同院校部門會依你指定的順序作為圖層加回地圖(也就是你最後選擇的部門會顯示在最上層)。
色盤 pal,以及只含公立與私立院校的 m3 地圖都已為你載入。
本練習屬於課程
在 R 中使用 leaflet 製作互動式地圖
練習說明
- 篩選 ipeds 資料,只保留營利性院校,並指定為
profit。 - 建立包含公立、私立與營利性院校的
leaflet地圖。 - 使用
addLayersControl()函式與overlayGroups參數,讓使用者可以切換這三個院校部門的顯示。 - 將地圖置中於美國本土中部,並將縮放層級設為 4。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create data frame called profit with only For-Profit colleges
___ <- filter(ipeds, sector_label == "For-Profit")
# Add For-Profit colleges to `m3` as a new layer
m3 <- m3 %>%
addCircleMarkers(data = ___, radius = 2, label = ~htmlEscape(name),
color = ~pal(sector_label), group = "___") %>%
addLayersControl(overlayGroups = c("Public", "Private", "___"))
# Center the map on the middle of the US with a zoom of 4
m4 <- m3 %>%
___(lat = 39.8282, lng = -98.5795, zoom = 4)
m4