当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R ggplot2 annotation_map 注释:Map


在地块上显示固定Map。此函数早于 geom_sf() 框架,并且不适用于 sf 几何列作为输入。但是,它可以与 geom_sf() 层和/或 coord_sf() 结合使用(请参阅示例)。

用法

annotation_map(map, ...)

参数

map

代表Map的 DataFrame 。有关详细信息,请参阅geom_map()

...

用于修改可视参数的其他参数,例如 colourfill

例子

if (FALSE) {
if (requireNamespace("maps", quietly = TRUE)) {
# location of cities in North Carolina
df <- data.frame(
  name = c("Charlotte", "Raleigh", "Greensboro"),
  lat = c(35.227, 35.772, 36.073),
  long = c(-80.843, -78.639, -79.792)
)

p <- ggplot(df, aes(x = long, y = lat)) +
  annotation_map(
    map_data("state"),
    fill = "antiquewhite", colour = "darkgrey"
  ) +
  geom_point(color = "blue") +
  geom_text(
    aes(label = name),
    hjust = 1.105, vjust = 1.05, color = "blue"
  )

# use without coord_sf() is possible but not recommended
p + xlim(-84, -76) + ylim(34, 37.2)

if (requireNamespace("sf", quietly = TRUE)) {
# use with coord_sf() for appropriate projection
p +
  coord_sf(
    crs = sf::st_crs(3347),
    default_crs = sf::st_crs(4326),  # data is provided as long-lat
    xlim = c(-84, -76),
    ylim = c(34, 37.2)
  )

# you can mix annotation_map() and geom_sf()
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p +
  geom_sf(
    data = nc, inherit.aes = FALSE,
    fill = NA, color = "black", linewidth = 0.1
  ) +
  coord_sf(crs = sf::st_crs(3347), default_crs = sf::st_crs(4326))
}}}
源代码:R/annotation-map.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Annotation: a map。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。