简体   繁体   中英

How can I label points on a mapview map in r

Please help me label points on a mapview map. I can plot the points ok. I'd like to then label the points "Point A", "Point B". I'd also prefer to do this without markers for the points ie I'll just use the coordinates to locate the labels.

library(sf)
library(mapview)
library(tidyverse)

points <- tribble(~name, ~lat, ~lon,
                     'Point A',     -38.119151, 145.401893,
                     'Point B',     -38.127870, 145.685598)

points_sf <- st_as_sf(points, coords = c("lon", "lat"), crs = 4326)

mapview(points_sf)

This is supported by leaflet , for which mapview depends on - but mapview adds other behavior on-top.

Here is the closest equivalent in mapview and how to do it exactly as requested in base leaflet .

Note: mapview::addStaticLabels is a wrapper for leaflet::addLabelOnlyMarkers .

library(sf)
library(mapview)
library(leaflet)
library(tidyverse)

points <- tribble(~name, ~lat, ~lon,
                  'Point A',     -38.119151, 145.401893,
                  'Point B',     -38.127870, 145.685598)

points_sf <- st_as_sf(points, coords = c("lon", "lat"), crs = 4326)

leaflet(points_sf) %>%
  addTiles() %>%
  addLabelOnlyMarkers(label =  ~name, 
                      labelOptions = labelOptions(noHide = T,
                                                  direction = 'top',
                                                  textOnly = T))

在此处输入图片说明

mapview(points_sf) %>%
  addStaticLabels(label = points$name,
                  noHide = TRUE,
                  direction = 'top',
                  textOnly = TRUE,
                  textsize = "20px")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM