การพล็อต DataCamp HQ

Interactive Maps with leaflet in R

Rich Majerus

Vice President of Strategy & Planning, Queens University of Charlotte

การพล็อตจุด

# add marker layer to map
leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = -73.98575, 
                lat = 40.74856)

ตัวอย่างมาร์กเกอร์จุดเดียว

  • การระบุข้อมูลมาร์กเกอร์

    • คอลัมน์ตัวเลขใน data frame
    • เวกเตอร์ตัวเลข
  • ค่าเริ่มต้นของ addMarkers()

    • จัดกึ่งกลางที่จุดเดียว
    • ซูมให้พอดีกับทุกจุด
Interactive Maps with leaflet in R

การพล็อตหลายจุด

dc_hq <- 
     tibble(
         hq  = c("DataCamp - NYC", "DataCamp - Belgium"),
         lon = c(-73.98575, 4.717863),
         lat = c(40.74856, 50.881363))
leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = dc_hq$lon, lat = dc_hq$lat)

ตัวอย่างมาร์กเกอร์หลายจุด

Interactive Maps with leaflet in R

การพล็อตหลายจุด II

# When piping a data frame into the leaflet function, R will search 
# for columns named lat/latitude and lon/lng/long/longitude  
dc_hq  %>% 
     leaflet() %>% 
     addTiles() %>% 
     addMarkers() 
Assuming 'lon' and 'lat' are longitude and latitude, respectively

ตัวอย่างมาร์กเกอร์หลายจุด

Interactive Maps with leaflet in R

Pop-ups

leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = dc_hq$lon, lat = dc_hq$lat, 
               popup = dc_hq$hq)

ตัวอย่างมาร์กเกอร์พร้อม popup

Interactive Maps with leaflet in R

Pop-ups II

leaflet() %>% 
     addTiles() %>% 
     addPopups(lng = dc_hq$lon, lat = dc_hq$lat,
               popup = dc_hq$hq)

ตัวอย่าง popup

Interactive Maps with leaflet in R

การเก็บแผนที่ leaflet เป็น Object

m <- leaflet() %>%
     addTiles()  %>% 
     setView(lng = dc_hq$lon[1], 
             lat = dc_hq$lat[1], 
             zoom = 12)

การเก็บแผนที่เป็น object

# %>% leaflet objects to functions 
# to add or edit layers
m %>% addMarkers(lng = dc_hq$lon, 
                 lat = dc_hq$lat, 
                 popup = dc_hq$hq)

การเพิ่ม layer

Interactive Maps with leaflet in R

มาฝึกกันเถอะ!

Interactive Maps with leaflet in R

Preparing Video For Download...