如何使用 R 中的 Leaflet 将两个坐标与一条线连接起来 [英] How Do I connect two coordinates with a line using Leaflet in R
问题描述
我正在尝试使用 R 中的 Leaflet 包来绘制放大器并连接下表中给出的纬度和经度信息的标记.
<上一页>|观察 |初始纬度 |初始长 |新纬度 |新龙 ||-------------|------------|-------------|-----------|-----------||一个 |62.469722 |6.187194 |51.4749 |-0.221619 ||乙|48.0975 |16.3108 |51.4882 |-0.302621 ||C |36.84 |-2.435278 |50.861822 |-0.083278 ||D |50.834194 |4.298361 |54.9756 |-1.62179 ||E |50.834194 |4.298361 |54.9756 |-1.62179 ||F |50.834194 |4.298361 |51.4882 |-0.302621 ||克|47.460427 |-0.530804 |51.44 |-2.62021 ||H |51.5549 |-0.108436 |53.4281 |-1.36172 ||我 |51.5549 |-0.108436 |52.9399 |-1.13258 ||Ĵ |51.5549 |-0.108436 |51.889839 |-0.193608 |||51.5549 |-0.108436 |52.0544 |1.14554 |我想从 InitialLat
和 InitialLong
列中的坐标给出的初始点到 NewLat
和 NewLong
列.
这是我当前的 R 代码,它只在地图上绘制标记.
<上一页>图书馆(传单)map3 = 传单(数据)%>% addTiles()map3 %>% addMarkers(~InitialLong,~InitialLat, popup=~Observation)这是使用 leaflet
包的另一种方法.我只是在您的数据中提取了两个数据点,用于演示.
mydf <- data.frame(Observation = c("A", "B"),InitialLat = c(62.469722,48.0975),InitialLong = c(6.187194, 16.3108),新纬度 = c(51.4749, 51.4882),新长 = c(-0.221619, -0.302621),字符串AsFactors = FALSE)
我更改了 mydf
的格式并为传单创建了一个新的数据框.您可以通过各种方式重塑数据.
mydf2 <- data.frame(group = c("A", "B"),纬度 = c(mydf$InitialLat, mydf$NewLat),长 = c(mydf$InitialLong, mydf$NewLong))# 组经纬度#1 62.46972 6.187194#2 B 48.09750 16.310800#3 A 51.47490 -0.221619#4 B 51.48820 -0.302621图书馆(传单)图书馆(magrittr)传单()%>%addTiles() %>%addPolylines(数据 = mydf2, lng = ~long, lat = ~lat, group = ~group)
我修剪了我得到的交互式地图.请看下面的地图.尽管在此图像中两条线相连,但它们是分开的.如果你运行代码并放大,你会看到这两行是分开的.
I am trying to use Leaflet package in R to draw a amp and connect the markers given the latitude and longitude information in the table below.
| Observation | InitialLat | InitialLong | NewLat | NewLong | |-------------|------------|-------------|-----------|-----------| | A | 62.469722 | 6.187194 | 51.4749 | -0.221619 | | B | 48.0975 | 16.3108 | 51.4882 | -0.302621 | | C | 36.84 | -2.435278 | 50.861822 | -0.083278 | | D | 50.834194 | 4.298361 | 54.9756 | -1.62179 | | E | 50.834194 | 4.298361 | 54.9756 | -1.62179 | | F | 50.834194 | 4.298361 | 51.4882 | -0.302621 | | G | 47.460427 | -0.530804 | 51.44 | -2.62021 | | H | 51.5549 | -0.108436 | 53.4281 | -1.36172 | | I | 51.5549 | -0.108436 | 52.9399 | -1.13258 | | J | 51.5549 | -0.108436 | 51.889839 | -0.193608 | | | 51.5549 | -0.108436 | 52.0544 | 1.14554 |
I want to draw lines from an initial point given by the coordinates in the InitialLat
and InitialLong
columns to an end point given by the NewLat
and NewLong
columns.
Here is my current R code which only draws the markers on the map.
library(leaflet) map3 = leaflet(data) %>% addTiles() map3 %>% addMarkers(~InitialLong,~InitialLat, popup=~Observation)
Here is an alternative way using the leaflet
package. I just took two data points in your data for the purpose of demonstration.
mydf <- data.frame(Observation = c("A", "B"),
InitialLat = c(62.469722,48.0975),
InitialLong = c(6.187194, 16.3108),
NewLat = c(51.4749, 51.4882),
NewLong = c(-0.221619, -0.302621),
stringsAsFactors = FALSE)
I changed the format of mydf
and create a new data frame for leaflet. You can reshape your data in various ways.
mydf2 <- data.frame(group = c("A", "B"),
lat = c(mydf$InitialLat, mydf$NewLat),
long = c(mydf$InitialLong, mydf$NewLong))
# group lat long
#1 A 62.46972 6.187194
#2 B 48.09750 16.310800
#3 A 51.47490 -0.221619
#4 B 51.48820 -0.302621
library(leaflet)
library(magrittr)
leaflet()%>%
addTiles() %>%
addPolylines(data = mydf2, lng = ~long, lat = ~lat, group = ~group)
I trimmed the interactive map I got. Please see the map below. Although two lines are connected in this image, they are separated. If you run the code and zoom in, you will see that the two lines are separated.
这篇关于如何使用 R 中的 Leaflet 将两个坐标与一条线连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!