0

Currently I have a dataframe consisting of several flight as such

dataframe

Using ggplot as shown below, I have managed to plot the flight path from origin to destination however cannot seem to change the path line to gradient colour that can visualise the flight from origin to destination.

Do advise as from my understanding, ggplot colour must be reliant on a variable.

q4c%>% 
  mutate(TailNum = factor(x=TailNum, levels=c("N351UA","N960DL","N524", "N14998", "N355CA","N711UW", "N587AA", "N839UA","N941CA","N516UA"))) %>%
  ggplot() + usMap2 +
  geom_curve(aes(x=OriginLong, y=OriginLat, xend=DestLong, yend=DestLat, size=TotalDelay,color=TailNum),
             curvature=0.2)+
  scale_size_continuous(range = c(0.02, 0.5))+
  geom_point(aes(x=OriginLong, y=OriginLat), 
             size=0.02) +
  geom_point(aes(x=DestLong, y=DestLat), 
             size=0.02) +
  facet_wrap(~TailNum)

Current Plot

edit:

I have tried ggforce::geom_link however it only shows solid colors instead of gradient as i added dummy sequence of 0,1 to get the color contrast

structure(list(Year = c(2005L, 2005L, 2005L, 2005L, 2005L, 2005L
), Month = c(1L, 1L, 1L, 1L, 1L, 1L), DayofMonth = c(1L, 1L, 
1L, 1L, 1L, 1L), DepTime = c(1022L, 1025L, 1037L, 1054L, 1110L, 
1111L), ArrTime = c(1527L, 1057L, 1209L, 1219L, 1454L, 1409L), 
    DepDelay = c(3L, 0L, -10L, -1L, 65L, -2L), ArrDelay = c(6L, 
    -3L, -11L, -27L, 52L, -8L), TotalDelay = c(9L, -3L, -21L, 
    -28L, 117L, -10L), TailNum = c("N351UA", "N524", "N14998", 
    "N941CA", "N355CA", "N587AA"), Origin = c("DEN", "PHX", "LBB", 
    "LGA", "SLC", "DFW"), Dest = c("CLT", "BUR", "IAH", "GSO", 
    "STL", "IND"), AirportOrigin = c("Denver Intl", "Phoenix Sky Harbor International", 
    "Lubbock International", "LaGuardia", "Salt Lake City Intl", 
    "Dallas-Fort Worth International"), OriginLong = c(-104.6670019, 
    -112.0080556, -101.8227778, -73.87260917, -111.9777731, -97.0372
    ), OriginLat = c(39.85840806, 33.43416667, 33.66363889, 40.77724306, 
    40.78838778, 32.89595056), AirportDest = c("Charlotte/Douglas International", 
    "Burbank-Glendale-Pasadena", "George Bush Intercontinental", 
    "Piedmont Triad International", "Lambert-St Louis International", 
    "Indianapolis International"), DestLong = c(-80.94312583, 
    -118.3584969, -95.33972222, -79.9372975, -90.35998972, -86.29438417
    ), DestLat = c(35.21401111, 34.20061917, 29.98047222, 36.09774694, 
    38.74768694, 39.71732917), id = 1:6, seqnum = c(1, 6, 1, 
    6, 1, 6)), row.names = c(NA, 6L), class = "data.frame")

dataframe

q4cc%>% 
  ggplot() + usMap2 +
  geom_link2(aes(x=OriginLong, y=OriginLat, size=TotalDelay, colour=seqnum))+
  scale_size_continuous(range = c(0.02, 1))+
  scale_color_gradient(name="Journey Path", high="red", low="blue")+
  scale_alpha_continuous(range=c(0.03,0.3))+
  geom_point(aes(x=OriginLong, y=OriginLat), 
             colour="red",
             size=0.02) +
  facet_wrap(~TailNum)

New Plot

TYMING
  • 1
  • 2
  • Do you mean you want the lines to change colour, as in each line starts one color then gradually changes to another colour? This is not natively possible in ggplot, though there are some add on packages that can achieve this. – Allan Cameron Mar 24 '22 at 14:20
  • @AllanCameron Yes correct thats what i meant, do you know what package it is ? I have tried like adding id number and sequence but the result isn't what i want. – TYMING Mar 24 '22 at 14:39
  • 1
    You could look at `geom_link` from the `ggforce` package. Or [this](https://stackoverflow.com/questions/71226957/how-to-apply-a-continous-color-ramp-to-a-straight-line-in-ggplot2) previous Stack Overflow question – Allan Cameron Mar 24 '22 at 14:40
  • @AllanCameron i have tried your suggestion however it only display solid color instead of the gradient color which i wanted to show for origin to destination. Do advice. – TYMING Mar 24 '22 at 15:02
  • I can't really show you how to use it on your data set because I don't have your data set. Can you make your example reproducible by showing how you are getting `q4c` and `usMap2` in a way that I can get them too? – Allan Cameron Mar 24 '22 at 15:14
  • @AllanCameron my q4c is the screenshot at the start. There are 1000+ observation thus i only post a single row just to show what variables i have. USmap is basically the usmap library and the code is as follow `usMap2 <- borders("state",colour="grey", fill="white")` – TYMING Mar 24 '22 at 16:29
  • @AllanCameron i added the list in the question above if it helps. – TYMING Mar 24 '22 at 17:13

0 Answers0