Guest User

Untitled

a guest
May 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. library(ggmap)
  2. library(readr)
  3. library(rjson)
  4.  
  5. test <- read_csv("../input/test.csv.zip")
  6. map <- readRDS("../input/99percentile_bw_map_copyright_openstreetmap_contributors.rds")
  7.  
  8. positions <- function(row) as.data.frame(do.call(rbind, fromJSON(row$POLYLINE)))
  9.  
  10. coordinates <- data.frame(TripId=c(), Ordinal=c(), Lat=c(), Lon=c(), Status=c(), EndPoint=c())
  11.  
  12. for (i in 1:nrow(test)) {
  13. pos <- positions(test[i,])
  14. if (nrow(pos)==1) {
  15. status <- c("Start")
  16. } else {
  17. status <- c("Start", rep("During Trip", nrow(pos)-2), "Last Observation")
  18. }
  19. coordinates <- rbind(coordinates, data.frame(TripId=test$TRIP_ID[i],
  20. Ordinal=1:nrow(pos),
  21. Lat = pos$V2,
  22. Lon = pos$V1,
  23. Status = status,
  24. Endpoint = status != "During Trip"))
  25. }
  26. coordinates$Status <- factor(coordinates$Status, levels <- c("Start", "During Trip", "Last Observation"))
  27.  
  28. p <- ggmap(map) +
  29. geom_point(data=coordinates, aes(x=Lon, y=Lat, color=Status, size=Endpoint)) +
  30. ggtitle("Taxi Trips in the Test Set") +
  31. scale_color_manual(values=c("#377EB8", "#4DAF4A", "#E41A1C")) +
  32. scale_size_manual(values=c(1.5, 3))
  33. ggsave("test_trips_map.png", p, width=10, height=8, units="in")
Add Comment
Please, Sign In to add comment