[英]Plot arcs in leaflet using R
I'm trying to plot frequency arcs in R using leaflet 我正在尝试使用传单在R中绘制频率弧
This is what my dataset looks like 这就是我的数据集的样子
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
I'm trying to plot all these countries on a leaflet and the condition is that the starting arc or starting point for each country would be from Boston, MA 我正在尝试在传单上绘制所有这些国家/地区的情况,条件是每个国家/地区的起点或起点将来自马萨诸塞州波士顿
The code that I used is as under 我使用的代码如下
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)
I referred to instructions specified in 我指的是
Adding Curved Flight path using R's Leaflet Package 使用R的传单包添加弯曲的飞行路径
but I'm getting a vague output 但我得到的输出模糊
this is the output of the leaflet 这是传单的输出
Please let me know what am I doing wrong thats causing the arcs to be as a straight line and not originate from Boston, MA 请让我知道我在做什么错,那会导致弧线是一条直线而不是起源于马萨诸塞州的波士顿
Thanks 谢谢
Updated Code: Added Quantiles based on the frequency and then tried changing colors based on the Quantiles, but it throws this error "Don't know how to get location data from object of class SpatialLines" 更新的代码:添加基于频率的分位数,然后尝试根据分位数更改颜色,但是它引发此错误“不知道如何从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
computes the arcs but you are plotting lines with rg1
: 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)
Try: 尝试:
gcIntermediate(bos, rg1[,c('lng', 'lat')], 200,
breakAtDateLine=FALSE, addStartEnd=TRUE, sp=TRUE) %>%
leaflet() %>% addTiles() %>% addPolylines()
for starters - note the empty addPolylines()
means it gets the coordinates from the data piped into leaflet()
- your interpolated great circle curves. 对于初学者-请注意,空的addPolylines()
表示它是通过管道addPolylines()
到leaflet()
的数据获取坐标的-您的插值大圆曲线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.