讓地圖更吸睛
就像用 ggplot2 繪圖或用 dplyr 處理資料一樣,如果你之後還要在程式碼中引用,地圖需要先存進一個物件裡。
說到 dplyr,%>% 運算子可以把資料串接進建立 leaflet 地圖的函式鏈。
ipeds %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(popup = ~name, color = "#FF0000")
使用管線讓程式碼更易讀,且你可以用 ~ 運算子來引用變數,而不必一再指定資料框。
addCircleMarkers() 的 color 參數可以使用顏色名稱或十六進位色碼。例如 red 或 #FF0000。
已經為你印出了 map。請注意,圓形標記不見了!
本練習屬於課程
在 R 中使用 leaflet 製作互動式地圖
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Add circle markers with popups for college names
map %>%
addCircleMarkers(data = ca, radius = 2, popup = ___)