Tải bản đồ Việt Nam với gói rnaturalearth
Lưu ý, bản đồ này thiếu vùng Hoàng Sa và Trường Sa.
Vẽ bản đồ với ggplot
và geom_sf
pacman::p_load(ggplot2,sf,ggspatial)
theme_set(theme_bw())
(map1 <- ggplot(vn_map) +
geom_sf(fill = "blue", alpha = 0.7) +
scale_x_continuous(breaks = seq(102,110,2)) +
coord_sf(xlim = c(102, 110)))
Thêm ký hiệu hướng bắc với hàm annotation_north_arrow
của gói ggspatial
Vị trí đặt ký hiệu
Ta có thể chọn vị trí đặt ký hiệu bằng cách thay đổi location
với các giá trị như "tl"
cho “topleft”, "tr"
cho “topright”, "br"
cho “bottomright” và "bl"
cho “bottomleft”.
Kiểu mũi tên
Ta có thể chọn 1 trong 4 kiểu mũi tên chỉ hướng bắc có sẵn của gói này. Với tham số style
map1 +
#north_arrow_fancy_orienteering
annotation_north_arrow(location = "tl",
height = unit(1,"cm"), width = unit(0.75, "cm"),
style = north_arrow_fancy_orienteering) +
#north_arrow_minimal
annotation_north_arrow(location = "tr",
height = unit(1,"cm"), width = unit(0.75, "cm"),
style = north_arrow_minimal) +
#north_arrow_nautical
annotation_north_arrow(location = "bl",
height = unit(1,"cm"), width = unit(0.75, "cm"),
style = north_arrow_nautical) +
#north_arrow_orienteering
annotation_north_arrow(location = "br",
height = unit(1,"cm"), width = unit(0.75, "cm"),
style = north_arrow_orienteering)
Thêm thước tỷ lệ
Ngoài ký hiệu hướng bắc, ta có thể thêm thước tỷ lệ với hàm annotation_scale()
như sau:
Đổi kiểu với style = "ticks"
Tuỳ chỉnh màu sắc và kích thước với các tham số của hàm.
map1 +
annotation_scale(location = "bl", width_hint = 0.5,
bar_cols = c("blue", "white"), text_cex = 1, line_col = "blue")
Thêm cùng lúc ký hiệu hướng bắc và thước tỷ lệ
map1 +
annotation_north_arrow(location = "tr",
height = unit(1,"cm"), width = unit(0.75, "cm"),
style = north_arrow_fancy_orienteering) +
annotation_scale(location = "bl", width_hint = 0.5, height = unit(0.1,"cm"),
bar_cols = c("blue", "white"), text_cex = 0.7,
line_col = "blue",
pad_x = unit(1.4, "cm"), pad_y = unit(0.2, "cm")) +
labs(x = "Longitude", y = "Latitude", title = "Vi\u1EC7t Nam")
Một tí màu mè
library(ggpomological)
library(extrafont)
#world_map
world <- ne_countries(scale = "medium", returnclass = "sf")
mycol <- "#fd8f24"
#plot
ggplot(world) +
geom_sf(fill = NA, color = mycol) +
geom_sf(data = vn_map, fill = mycol, alpha = 0.7) +
coord_sf(xlim = c(100,114), ylim = c(8,24)) +
scale_x_continuous(breaks = seq(100,114,4)) +
annotation_north_arrow(location = 'tr',
height = unit(1,"cm"), width = unit(0.75, "cm"),
style = north_arrow_fancy_orienteering) +
annotation_scale(location = "bl", width_hint = 0.5, height = unit(0.1,"cm"),
bar_cols = c(mycol, "white"), text_cex = 0.7,
line_col = mycol, text_col = mycol,
pad_x = unit(2, "cm"), pad_y = unit(0.2, "cm")) +
theme_pomological_fancy(base_family = "Homemade Apple") +
theme(panel.grid.major = element_line(size = 0.1, linetype = 'solid')) +
labs(x = "Longitude", y = "Latitude", title = "Viet Nam")