Heat Map

Creating a new data frame with average values per hour

output_1 <- select(merge_whole_set,Hour, avgMeasuredTime, vehicleCount)
total_per_hour <- data.matrix(output_1)
average_per_hour <- rollapply(total_per_hour,1335,(mean),by=1335,column=TRUE,align='right')
aph_kmeans <- kmeans(average_per_hour, 3)
plot3d(average_per_hour, size=7, type = "p", xlab = "Hour", 
       ylab="Average Time", zlab="Vehicle Count per Interval", 
       main="Average Time and Vehicle Count per Hour", lwd=2, col=aph_kmeans$cluster)

Creating a data frame containing co-ordinate points along the road (7 points in total) Columns 1 to 3 are copied 7 times, each tied to a different co-ordinate along the road.

lat_long <- data.frame(rep(c(56.23172069428216,56.230342, 56.228521, 56.228345, 56.227602, 56.226643, 56.22579478256016),24), rep(c(10.104986076057457, 10.109538,10.113668, 10.114011, 10.115441,10.116618, 10.116589665412903),24))
colnames(lat_long) <- c("Latitude","Longitude")

Binding all columns and sorting by hour and then latitude, ascending.

average_with_coordinates <- cbind(average_per_hour, lat_long)
average_with_coordinates <- arrange(average_with_coordinates,Hour, Latitude)
head(average_with_coordinates)
##       Hour avgMeasuredTime vehicleCount Latitude Longitude
## 1 1.043446        59.33558     0.211236 56.22579  10.11659
## 2 1.043446        59.33558     0.211236 56.22664  10.11662
## 3 1.043446        59.33558     0.211236 56.22760  10.11544
## 4 1.043446        59.33558     0.211236 56.22834  10.11401
## 5 1.043446        59.33558     0.211236 56.22852  10.11367
## 6 1.043446        59.33558     0.211236 56.23034  10.10954

Conducting a PCA Analysis of Hour, Time and Vehicle Count

par(bg='white')
plot(average_with_coordinates[1:3], cex=0.1, col="black", pch=16, 
     main="PCA Analysis of Hinnerup Traffic Data")

## package 'ggmap' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Aidan\AppData\Local\Temp\RtmpspgDPc\downloaded_packages

Creates a value for the centre of the map and retrieves the map from Google Maps using given co-ordinates.

centre <- rev(sapply(lat_long, mean))
map <- get_map(centre, zoom=15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=56.228424,10.112979&zoom=15&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false

Plots the map along with two points specifying the start and end points and a path connecting the two. Next step, find a way to curve the path to the map and animate for the mean values of time, vehicle count over 24 hours (green coloured line = free flowing, red = congestion) Create a function which takes “hour of day” as input and returns time, vehicle count and plots map?

myfunc <- function(time_of_day){
  if(time_of_day >= 6 & time_of_day <= 8){
    cat("Caution! Rush hour, average travel time: ",
        average_with_coordinates$avgMeasuredTime[time_of_day*7])
    ggmap(map) + geom_point(aes(x=10.104986076057457, y=56.23172069428216), color="black",
                            size=5, shape=8) + geom_point(aes(x=10.116589665412903,
                                                              y=56.22579478256016),
                                                          color="black", size=5, shape = 8) +
      geom_path(aes(x = Longitude, y = Latitude), data =
                  average_with_coordinates[(1+(time_of_day-1)*7):(7+(time_of_day-1)*7),4:5],
                colour ='red', size = 2, lineend = "round", linejoin = "round", linemitre = 1,
                show.legend = FALSE, na.rm=TRUE)
  }
  else if(time_of_day >= 3 & time_of_day <= 4){
    cat("Traffic may be busy: ", average_with_coordinates$avgMeasuredTime[time_of_day*7])
    ggmap(map) + geom_point(aes(x=10.104986076057457, y=56.23172069428216), color="black",
                            size=5, shape=8) + geom_point(aes(x=10.116589665412903,
                                                              y=56.22579478256016),
                                                          color="black", size=5, shape=8) +
      geom_path(aes(x = Longitude, y = Latitude), data =
                  average_with_coordinates[(1+(time_of_day-1)*7):(7+(time_of_day-1)*7),4:5],
                colour = 'orange', size = 2, lineend = "round", linejoin = "round", linemitre =
                  1, show.legend = FALSE, na.rm=TRUE)
  }
  else{
    cat("Traffic is moving freely: ", average_with_coordinates$avgMeasuredTime[time_of_day*7])
    ggmap(map) + geom_point(aes(x=10.104986076057457, y=56.23172069428216), color="black",
                            size=8) + geom_point(aes(x=10.116589665412903, y=56.22579478256016),
                                                 color="black", size=5, shape=8) +
      geom_path(aes(x = Longitude, y = Latitude),data =
                  average_with_coordinates[(1+(time_of_day-1)*7):(7+(time_of_day-1)*7),4:5],
                colour = 'green', size = 2, lineend = "round", linejoin = "round", linemitre =
                  1, show.legend = FALSE, na.rm=TRUE)
  }
}