พล็อต 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เพื่อให้แต่ละย่านซึ่งระบุผ่าน attributeNameแสดงสีที่แตกต่างกัน - เพิ่ม 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()