0

I am stuck with a problem with leaflet.
I want to plot flight routes (already done with gcIntermediate) but i want to colour the routes based on a factor variable (Status:cancelled,delayed).
I would want delayed routes colored with blue and the cancelled ones with red

my R code:

gcIntermediate(coord1[,c(1,2)], coord1[,c(3,4)],  
               n=100,   
               addStartEnd=TRUE,  
               sp=TRUE) %>%   
  leaflet() %>%   
  addTiles() %>%   
  setView(-100,38, zoom = 4.49) %>%   
  addPolylines(color="red") %>%   
  addMarkers(lng=c(coord1[,1],coord1[,3]),lat=c(coord1[,2],coord1[,4]))

where coord1 is a dataframe with: the latitudes and logitudes of two cities and the factor variable(status).

peeebeee
  • 2,541
  • 6
  • 21
  • 26
Gus CR
  • 57
  • 7
  • Could you provide some example data so we can run your code? –  May 14 '18 at 15:41
  • coord1 <- data.frame("Longitud1" = c(-84.42694,-82.42694,-104.66700), "Latitud1"=c(33.64044,33.64044,39.85841),"Longitud2"=c(-97.03720,-80.15275,-112.00806),"Latitud2"=c(32.89595,26.07258,33.43417),"estado"=c("Delayed","Delayed","Cancelled")) – Gus CR May 15 '18 at 08:02
  • I think I've made it! :D – Gus CR May 15 '18 at 08:35
  • gcIntermediate(coord1[,c(1,2)], coord1[,c(3,4)], n=100, addStartEnd=TRUE, sp=TRUE) %>% leaflet() %>% addTiles() %>% setView(-100,38, zoom = 4.49) %>% addPolylines(color=colores(coord1)) %>% addMarkers(lng=c(coord1[,1],coord1[,3]),lat=c(coord1[,2],coord1[,4])) colores<-function(df){ color<-c() for( i in 1: nrow(coord1)){ if (df$estado[i]=="Cancelled") { color<-c(color,"red") }else{ color<-c(color,"orange") } } return(color) } – Gus CR May 15 '18 at 08:36
  • Instead of adding the info in the comment section, you should edit your post. – MLavoie May 15 '18 at 10:31
  • And if you've worked out an answer, you can answer your own question. –  May 15 '18 at 16:14

1 Answers1

0
        coord1 <- data.frame("Longitud1" = c(-84.42694,-82.42694,-104.66700),   
  "Latitud1"=c(33.64044,33.64044,39.85841), 
 "Longitud2"=c(-97.03720,-80.15275,-112.00806), 
     "Latitud2"=c(32.89595,26.07258,33.43417), 
     "estado"=c("Delayed","Delayed","Cancelled")) 

And the answer is:

colores<-function(df){ color<-c() for( i in 1: nrow(coord1)){ if (df$estado[i]=="Cancelled") { color<-c(color,"red") }else{ color<-c(color,"orange") } } return(color) } 

 gcIntermediate(coord1[,c(1,2)], coord1[,c(3,4)], n=100, addStartEnd=TRUE, sp=TRUE) %>% leaflet() %>% addTiles() %>% setView(-100,38, zoom = 4.49) %>% addPolylines(color=colores(coord1)) %>% 
  addMarkers(lng=c(coord1[,1],coord1[,3]),lat=c(coord1[,2],coord1[,4])) 
Gus CR
  • 57
  • 7