使用 ggplot 绘制 sf 图
您或许也熟悉 ggplot2 库,其中的图形由多个图层组成。
其实,sf 对象也可以用 ggplot2 来绘制。快速回顾一下:第一层是由 ggplot() 创建的空白画布。随后通过添加几何函数(形式为 geom_<plot>)、labs()、主题等,将各个图层叠加起来。
接下来,您将使用多个 sf 对象构建一幅 ggplot 地图。
数据框 listings、一个名为 london_poly 的多多边形,以及一个名为 num_listings 的点集合都已加载。tidyverse 和 sf 库也已导入。
本练习是课程的一部分
使用 shinydashboard 构建仪表板
练习说明
- 将
num_listings的坐标参考系(CRS)设置为与london_poly相同。 - 将该多多边形添加到空白画布上。
- 设置
fill参数,使每个在Name属性中显示的社区具有不同的填充色。 - 在每个多边形的中心添加一个标签。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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()