如何在传单中的地图上添加本地图像?

时间:2019-03-25 18:15:05

标签: r leaflet

我想像这样将本地图像放到R中的传单地图上(我将图像手动放置在画图中):

enter image description here 我根据library(mapview) library(leaflet) cities <- read.csv(textConnection(" City,Lat,Long,Pop Boston,42.3601,-71.0589,645966 Hartford,41.7627,-72.6743,125017 New York City,40.7127,-74.0059,8406000 Philadelphia,39.9500,-75.1667,1553000 Pittsburgh,40.4397,-79.9764,305841 Providence,41.8236,-71.4222,177994 ")) img1 <- system.file("img1.png", package = "png") leaflet(cities) %>% addTiles() %>% addCircles(lng = ~Long, lat = ~Lat, weight = 1, radius = ~sqrt(Pop) * 30, popup = ~City)%>% addLogo(img1, src = "local", position = "bottomright", alpha = 0.3) 中的示例对此进行了尝试:

{{1}}

但是它没有将图像添加到地图中:

enter image description here

1 个答案:

答案 0 :(得分:0)

您对?addLogo的帮助中的示例感到困惑(并且有充分的理由,这是一个令人困惑的帮助示例)。 addLogo所需要的只是图像的路径。在该帮助示例中使用的对system.file的调用仅返回指向png包中包含的示例图像的路径,该图像用于示例目的。如果您已经知道图像的路径,则只需提供它即可。

因此,假设您的映像名为img1.png并且在当前工作目录中,则可以执行以下操作:

leaflet(cities) %>%
    addTiles() %>%
    addCircles(lng = ~Long, lat = ~Lat, weight = 1,
               radius = ~sqrt(Pop) * 30, popup = ~City)%>%
    addLogo("img1.png", src = "local",position = "bottomright", alpha = 0.3)