公告:本號的「地理計算與分析專輯」已經更名為「地理製圖與分析專輯」,原先關注該專輯的讀者需要重新關注下。
ggplot2工具包用於繪製地圖的有如下幾個函數:
這些函數中,最常用的是前兩個函數,本篇就來介紹這兩個函數。
加載相關工具包和示例數據:
library(ggplot2)
library(patchwork)
library(sf)
library(RColorBrewer)
library(tidyverse)
data <- socviz::county_data
albersusa::counties_sf(proj = "laea") %>%
mutate(fips = as.character(fips)) %>%
left_join(data, by = c("fips" = "id")) -> usa使用geom_sf()繪製地圖:
map <- ggplot(usa) +
geom_sf(aes(fill = log(pop, 10))) +
scale_fill_stepsn(colours = brewer.pal(4, "Spectral"),
breaks = c(4, 5, 6),
name = "pop",
label = c(expression(10^4, 10^5, 10^6))) +
theme_bw()
map
geom_sf()函數與其他幾何圖形函數的使用方法類似,區別在於其不用指定x、y變量。coord_sf()函數用於地圖的投影轉換:
map + coord_sf(crs = 2345)
上圖只是隨便指定了一個投影坐標,更多投影坐標的EPSG代碼可以在http://epsg.io/上進行查詢。查詢界面:
查詢結果:map + coord_sf(crs = 8826)
相關閱讀