简体   繁体   中英

Plot arcs in leaflet using R

I'm trying to plot frequency arcs in R using leaflet

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

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"

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(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM