在 r 中绘制离线地图

问题描述 投票:0回答:1

我能够使用传单库离线运行地图。我想知道是否可以对 r 中的plotly 库做同样的事情?

我的代码和传单:

# Set the working folder
setwd("C:/Users/user/Documents/mapTiles")

library(RgoogleMaps)
GetMapTiles(center = c(lat = 35.75, lon = 65.31), zoom = zoom,
                               nTiles = c(20,20), type = "osm", tileDir= TRUE)

# Start serving working folder on port 8000 in demon mode
deamon_id <- servr::httd(port = 8002, daemon = TRUE)

# Plot with leaflet
library(leaflet)
m = leaflet() %>%
  addTiles(urlTemplate = "http://localhost:8002/osm/{z}_{x}_{y}.png")
m = m %>% leaflet::setView(65.31, 33.67,zoom = 10)
m

# Stop serving
servr::daemon_stop(deamon_id)

谢谢你的帮助

r plotly openstreetmap
1个回答
0
投票

您可以在四开文档中嵌入

plotly
地图。下面是一个四开文档中包含的plotly 地图示例

---
title: "Plotly Map"
format: html
execute:
  warning: false
  message: false
  echo: false
---

```{r}
library(plotly)
```

```{r}
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'

counties <- rjson::fromJSON(file=url)

url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"

df <- read.csv(url2, colClasses=c(fips="character"))

fig <- plot_ly() 

fig <- fig %>% add_trace(
  type = "choroplethmapbox",
  geojson = counties,
  locations = df$fips,
  z = df$unemp,
  colorscale = "Viridis",
  zmin = 0,
  zmax = 12,
  marker = list(line = list(width = 0),
                opacity = 0.5)
)

fig <- fig %>% layout(mapbox = list(
  style = "carto-positron",
  zoom = 2,
  center = list(lon = -95.71, lat = 37.09)
))

fig
```

这是生成的 HTML 文档的屏幕截图:

© www.soinside.com 2019 - 2024. All rights reserved.