published by Qingyun Wu
Import all the libraries needed and load all the csv data source:
library(ggplot2)
library(ggmap)
Google Maps API Terms of Service: http://developers.google.com/maps/terms.
Please cite ggmap if you use it: see citation('ggmap') for details.
library(gridExtra)
library(lubridate)
data_407_day = read.csv("RTL150407_day.csv", header=T)
data_407_night = read.csv("RTL150407_night.csv", header=T)
data_1208_day = read.csv("RTL151208_day.csv", header=T)
data_1208_night = read.csv("RTL151208_night.csv", header=T)
The following four graph shows differences in path (latitude, longitude and altitude) over time.
We can clearly see that altitude is differed by the color from red to blue, and the line in the map shows the path of the flight.
fight_mapplot <- function(fightpath) {
SMO <- data.frame(label = "SMO", lon=-118.456667, lat=34.010167)
smo <- c(SMO$lon, SMO$lat)
map.google <- get_map(smo, zoom = 12) # get map around SMO
p <- ggmap(map.google)
return (p + geom_point(data = SMO, aes(x=lon, y=lat), color="red", size=5, alpha=.5)
+ geom_path(data = fightpath, aes(x = lon, y = lat, color=alt), alpha=.5)
+ scale_colour_gradient(limits=c(1000, 11000), low="red", high="blue"))
}
fight_mapplot(data_407_day) + labs(title = "Daytime Fight Path on 04/07/2015")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=34.010167,-118.456667&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
fight_mapplot(data_407_night) + labs(title = "Nighttime Fight Path on 04/07/2015")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=34.010167,-118.456667&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
fight_mapplot(data_1208_day) + labs(title = "Daytime Fight Path on 12/08/2015")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=34.010167,-118.456667&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
fight_mapplot(data_1208_night) + labs(title = "Nighttime Fight Path on 12/08/2015")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=34.010167,-118.456667&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
The following four graph are bar charts to show number of different fight which distance from SMO is between 10000 to 50000km over time. The function extractTime can do all the work, and I use ggplot2 to make the barchart.
For example, in the first graph, we can see 9:30 to 10:00 has the most number of flight which conform our regulation.
extractTime <- function(oridf, start, end) {
oridf$timestamp = as.POSIXct(oridf$timestamp, format = "%Y-%m-%d %H:%M:%S")
start = as.POSIXct(start, format = "%Y-%m-%d %H:%M:%S")
end = as.POSIXct(end, format = "%Y-%m-%d %H:%M:%S")
gain <- ymd_hms("2015-03-02 11:30:00", tz="America/Los_Angeles")-ymd_hms("2015-03-02 11:00:00", tz="America/Los_Angeles")
newdf <- subset(oridf, timestamp >= start & timestamp < end, select=c(code, alt))
udf <- data.frame(code = unique(newdf$code))
tmpoend <- start + gain
result <- data.frame()
while (tmpoend <= end) {
intervaltime <- subset(oridf, timestamp >= start
& timestamp <= tmpoend
& dist >= 10000
& dist <= 50000,
select=(c(code,dist)))
count <- length(unique(intervaltime$code))
entry <- data.frame(starttime = start, flightsnum = count)
result <- rbind(result, entry)
start <- tmpoend
tmpoend <- tmpoend + gain
}
result$starttime <- strftime(result$starttime, format = "%H:%M")
return(result)
}
finaldata <- extractTime(data_407_day, "2015-04-07 06:30:00","2015-04-07 12:00:00")
require(ggplot2)
install.packages('gcookbook')
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/gcookbook_1.0.tgz'
Content type 'application/x-gzip' length 4002525 bytes (3.8 MB)
==================================================
downloaded 3.8 MB
tar: Failed to set default locale
The downloaded binary packages are in
/var/folders/pk/x3_vr3nn6ydbk8ydx01nqs5c0000gn/T//RtmpVFTWJk/downloaded_packages
library(gcookbook)
Attaching package: 'gcookbook'
The following object is masked from 'package:ggmap':
wind
library(ggplot2)
#ggplot(finaldata, aes(starttime, flightsnum)) + geom_line() + geom_point()
#ggplot() + geom_line(data=finaldata, aes(x=starttime, y=flightsnum))+geom_point()
#ggplot(data = finaldata, aes(y = flightsnum, x = starttime)) + geom_line()
ggplot(data=finaldata, aes(y = flightsnum, x = starttime)) +
geom_bar(stat="identity", aes(fill=factor(starttime))) +
xlab("date and time") +
ylab("number of flights") + labs(title = "Number of Selected Distance Flight on 04/07/2015 Day Time")
finaldata <- extractTime(data_407_night, "2015-04-07 00:00:00","2015-04-07 06:30:00")
ggplot(data=finaldata, aes(y = flightsnum, x = starttime)) +
geom_bar(stat="identity", aes(fill=factor(starttime))) +
xlab("date and time") +
ylab("number of flights") + labs(Title = "Number of Selected Distance Flight on 04/07/2015 Night Time")
finaldata <- extractTime(data_1208_day, "2015-12-08 06:30:00","2015-12-08 12:00:00")
ggplot(data=finaldata, aes(y = flightsnum, x = starttime)) +
geom_bar(stat="identity", aes(fill=factor(starttime))) +
xlab("date and time") +
ylab("number of flights") + labs(Title = "Number of Selected Distance Flight on 12/08/2015 Day Time")
finaldata <- extractTime(data_1208_night, "2015-12-08 00:00:00","2015-12-08 06:30:00")
ggplot(data=finaldata, aes(y = flightsnum, x = starttime)) +
geom_bar(stat="identity", aes(fill=factor(starttime))) +
xlab("date and time") +
ylab("number of flights") + labs(Title = "Number of Selected Distance Flight on 12/08/2015 Night Time")
Compare unique flight numbers between two choosen days using bar chart. You can see each day’s flight number percentages on the graph. It can be obvious that flight number on 2015-12-08 is much more than 2015-04-07.
install.packages('plotrix')
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/plotrix_3.6-6.tgz'
Content type 'application/x-gzip' length 679987 bytes (664 KB)
==================================================
downloaded 664 KB
tar: Failed to set default locale
The downloaded binary packages are in
/var/folders/pk/x3_vr3nn6ydbk8ydx01nqs5c0000gn/T//RtmpVFTWJk/downloaded_packages
library(plotrix)
library(dplyr)
Attaching package: 'dplyr'
The following object is masked from 'package:gridExtra':
combine
The following objects are masked from 'package:lubridate':
intersect, setdiff, union
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
firstday <- length(unique(data_407_day$code))
firstnight <- length(unique(data_407_night$code))
firsttotal = firstday + firstnight
secondday <- length(unique(data_1208_day$code))
secondnight <- length(unique(data_1208_night$code))
secondtotal = secondday + secondnight
data <- c(firsttotal,secondtotal)
lbls <- c("2015-04-07","2015-12-08")
pct <- round(data/sum(data)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(data,labels = lbls, col=rainbow(length(lbls)),
main="Unique Flights Between Two Days")