使用 R 在传单中绘制弧线

Plot arcs in leaflet using R

我正在尝试使用传单在 R 中绘制频率弧

这是我的数据集的样子

lng lat Freq    Country
67.709953   33.93911    26  Afghanistan
104.990963  12.565679   9   Cambodia
12.354722   7.369722    15  Cameroon
-23.6051868 15.120142   312 Cape Verde
-71.542969  -35.675147  2   Chile
104.195397  35.86166    639 China
-74.297333  4.570868    92  Colombia
21.758664   -4.038333   8   Congo
-77.781167  21.521757   1   Cuba
42.590275   11.825138   1   Djibouti
-70.162651  18.735693   350 Dominican Republic
20.168331   41.153332   34  Albania
-78.183406  -1.831239   16  Ecuador
30.802498   26.820553   4   Egypt
-88.89653   13.794185   207 El Salvador
40.489673   9.145   129 Ethiopia
2.213749    46.227638   12  France

我正在尝试将所有这些国家/地区绘制在传单上,条件是每个国家/地区的起始弧线或起点都来自马萨诸塞州波士顿

我使用的代码如下

library(leaflet)
library(geosphere)

rg1<-read.csv("rg.csv")
bos=geocode(as.character("Boston")) 

gcIntermediate(bos, rg1[,c('lng', 'lat')], 200, breakAtDateLine=FALSE, addStartEnd=TRUE, sp=TRUE) %>% 
  leaflet()  %>% addTiles()%>% 
  addCircleMarkers(data=rg1, radius = 8, color = 'red', fill = TRUE, label = ~as.character(Freq), labelOptions=c(noHide=TRUE)) %>%
  addPolylines(data=rg1, lng = ~lng, lat = ~lat)

我参考了

中指定的说明

但我的输出很模糊

this is the output of the leaflet

请告诉我我做错了什么,导致弧线变成直线而不是来自马萨诸塞州波士顿

谢谢

更新代码: 添加基于频率的分位数,然后尝试根据分位数更改颜色,但它会抛出此错误 "Don't know how to get location data from object of class SpatialLines"

rg1$q<-ifelse(rg1$Freq<=100,"1st Quantile(1-100 occurances)",ifelse(rg1$Freq>=101 & rg1$Freq<=250,"2nd Quantile(101-250 occurances)","3rd Quantile(250+ occurances)"))
rg1$q <- as.ordered(rg1$q)

pal <- colorFactor(c("navy", "red","green"), domain = c("1st Quantile(1-100 occurances)","2nd Quantile(101-250 occurances)","3rd Quantile(250+ occurances)"))


gcIntermediate(bos, rg1[,c('lng', 'lat')], 200, 
               breakAtDateLine=FALSE, addStartEnd=TRUE, sp=TRUE) %>%
  leaflet()  %>% addTiles() %>% 
  addCircleMarkers(
    color = ~pal(q),
    stroke = FALSE, fillOpacity = 0.5)  %>%
  addPolylines()

gcIntermediate 计算弧线,但您正在使用 rg1:

绘制直线
gcIntermediate(bos, rg1[,c('lng', 'lat')], 200, breakAtDateLine=FALSE, addStartEnd=TRUE, sp=TRUE) %>% 
  leaflet()  %>% addTiles()%>% 
  addCircleMarkers(data=rg1, radius = 8, color = 'red', fill = TRUE, label = ~as.character(Freq), labelOptions=c(noHide=TRUE)) %>%
  addPolylines(data=rg1, lng = ~lng, lat = ~lat)

尝试:

gcIntermediate(bos, rg1[,c('lng', 'lat')], 200, 
  breakAtDateLine=FALSE, addStartEnd=TRUE, sp=TRUE) %>%
  leaflet()  %>% addTiles() %>% addPolylines()

对于初学者 - 注意空的 addPolylines() 意味着它从数据中获取坐标 leaflet() - 你的内插大圆曲线。