如何在R中使用Leaflet将两个坐标与一条线连接 [英] How Do I connect two coordinates with a line using Leaflet in R

查看:501
本文介绍了如何在R中使用Leaflet将两个坐标与一条线连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的Leaflet包绘制一个放大器并连接下表中给出的纬度和经度信息的标记.

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   |

我想从InitialLatInitialLong列中的坐标给出的起始点到NewLatNewLong列中给出的终点画线.

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.

这是我当前的R代码,只能在地图上绘制标记.

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)

推荐答案

这里是使用leaflet包的另一种方法.我只是在您的数据中取了两个数据点用于演示.

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)

我更改了mydf的格式,并为传单创建了一个新的数据框.您可以通过多种方式重塑数据.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆