R包:ggspatial空间画图

发布于:2024-10-11 ⋅ 阅读:(4) ⋅ 点赞:(0)

在这里插入图片描述

ggplot2语法的空间图形画图

Spatial data plus the power of the ggplot2 framework means easier mapping.

加载R包

# install.packages("ggspatial")

library(ggplot2)
library(ggspatial)
load_longlake_data()

Using layer_spatial() and annotation_spatial()

ggplot() +
  annotation_spatial(longlake_waterdf) +
  layer_spatial(longlake_depthdf, aes(col = DEPTH_M))

在这里插入图片描述

With raster layers, you can also use layer_spatial()

ggplot() + 
  layer_spatial(longlake_depth_raster, aes(fill = after_stat(band1))) +
  scale_fill_viridis_c(na.value = NA)

在这里插入图片描述

Using north arrow and scalebar

ggplot() +
  annotation_spatial(longlake_waterdf) +
  layer_spatial(longlake_depthdf, aes(col = DEPTH_M)) +
  annotation_scale(location = "tl") +
  annotation_north_arrow(location = "br", which_north = "true")

在这里插入图片描述

Tile map layers

ggplot() +
  annotation_map_tile(type = "osm") +
  layer_spatial(longlake_depthdf, aes(col = DEPTH_M))

在这里插入图片描述

Data frames with coordinates

cities <- data.frame(
  x = c(-63.58595, 116.41214), 
  y = c(44.64862, 40.19063), 
  city = c("Halifax", "Beijing")
)

ggplot(cities, aes(x, y)) +
  annotation_map_tile(type = "stamenwatercolor") +
  geom_spatial_point() +
  geom_spatial_label_repel(aes(label = city), box.padding = 1) +
  coord_sf(crs = 3995)

在这里插入图片描述

案例

library(ggplot2)
library(ggspatial)
load_longlake_data()

ggplot() +
  # loads background map tiles from a tile source
  annotation_map_tile(zoomin = -1) +
  
  # annotation_spatial() layers don't train the scales, so data stays central
  annotation_spatial(longlake_roadsdf, size = 2, col = "black") +
  annotation_spatial(longlake_roadsdf, size = 1.6, col = "white") +

  # raster layers train scales and get projected automatically
  layer_spatial(longlake_depth_raster, aes(colour = after_stat(band1))) +
  # make no data values transparent
  scale_fill_viridis_c(na.value = NA) +
  
  # layer_spatial trains the scales
  layer_spatial(longlake_depthdf, aes(fill = DEPTH_M)) +
  
  # spatial-aware automagic scale bar
  annotation_scale(location = "tl") +

  # spatial-aware automagic north arrow
  annotation_north_arrow(location = "br", which_north = "true")

在这里插入图片描述

参考

  • https://paleolimbot.github.io/ggspatial/articles/ggspatial.html