เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

พล็อต sf ด้วย ggplot

คุณอาจคุ้นเคยกับไลบรารี ggplot2 ซึ่งสร้างกราฟจากหลาย layer ซ้อนกัน

ที่จริงแล้ว sf object ก็สามารถพล็อตด้วย ggplot2 ได้เช่นกัน ทบทวนสั้น ๆ ว่า layer แรกคือ canvas เปล่าที่สร้างด้วย ggplot() จากนั้น layer ถัดไปจะถูกวางซ้อนกันโดยการเพิ่มฟังก์ชัน geom (ในรูปแบบ geom_<plot>), labs(), theme และอื่น ๆ

ในแบบฝึกหัดนี้ จะได้สร้างแผนที่ใน ggplot โดยใช้ sf object หลายตัว

โหลด dataframe ชื่อ listings, multipolygon ชื่อ london_poly และชุดจุดข้อมูลชื่อ num_listings ไว้ให้แล้ว รวมถึงนำเข้าไลบรารี tidyverse และ sf เรียบร้อยแล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การสร้างแดชบอร์ดด้วย shinydashboard

ดูคอร์ส

คำแนะนำการฝึกหัด

  • กำหนด coordinate reference system (CRS) ของ num_listings ให้ตรงกับ CRS ของ london_poly
  • เพิ่ม multipolygon ลงบน canvas เปล่า
  • กำหนด argument fill เพื่อให้แต่ละย่านซึ่งระบุผ่าน attribute Name แสดงสีที่แตกต่างกัน
  • เพิ่ม label ที่จุดกึ่งกลางของแต่ละ polygon

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Set the CRS of num_listings to that of london_poly
st_crs(num_listings) <- ___(___)

# Add the multipolygon to the blank canvas
___ + 
  # Set a different fill for each neighborhood
  geom_sf(aes(___)) + 
  # Add a label at the center of each polygon
  ___(data=num_listings, aes(label=`Number of listings`)) +
  theme_classic()
แก้ไขและรันโค้ด