install.packages("tidyverse") install.packages("leaflet") install.packages("rgdal") install.packages("readr") install.packages("RCurl") install.packages("htmlwidgets") install.packages("leaflet") require(leaflet) require(readr) require(RCurl) require(tidyverse) require(lubridate) require(rgdal) require(htmlwidgets) require(leaflet) # We load the data directly from tubCloud and open the data set download <- getURL("https://hessenbox-a10.rz.uni-frankfurt.de/dl/fi88asBdwcm9oRT5tWy3rFy9") tracks <- read_delim(download, delim = ',') # Let's inspect the data and do some basic statistics # The column "type" tells us that we have tracks and stays in the data. At the moment # we are only interested in tracks. Therefore we select the tracks. We do this by # using the fact that stays obviously don't have a distance (length) tracks <- tracks[!(is.na(tracks$length)),] # Let's find out how many user participated, how many tracks they produced and # which distances they travelled. cnt.user <- length(unique(tracks$user_id)) cnt.user cnt.tracks <- nrow(tracks) cnt.tracks distance <- sum(tracks$length)/1000 distance # Modal split by tracks modal_split <- tracks %>% group_by(started_on, mode) %>% summarise(distance = round(sum(length)/1000, 2), n_mode = length(mode)) %>% mutate(prop_distance = (round(distance / sum(distance), 3) * 100), prop_count = (round(n_mode / sum(n_mode), 3) * 100)) # Diurnal curve: Number of tracks per hour # use package lubridate to get values per hour diurnal_curve <- tracks %>% subset(select = c(mode, started_at)) %>% mutate(started_at = hour(started_at)) %>% group_by(started_at) %>% summarise(tracks_per_hour = n()) # Plot modal split # Let's use some nice colors. But which modes do we have in the dataset? levels(as.factor(modal_split$mode)) # Ok, let's define the colors for these modes mode_colors <- c("Mode::Bicycle" = "#90A72F", "Mode::Bus" = "#5175AE", "Mode::Car" = "#D57F0E", "Mode::Motorbike" = "#D57F0E", "Mode::LightRail" = "#35AD9C", "Mode::Subway" = "#3E8EB6", "Mode::RegionalTrain" = "#D6393A", "Mode::Tram" = "#D6393A", "Mode::Walk" = "#CEC11D") p_modal_split_distance_day <- modal_split %>% ggplot(aes(x = started_on, y = distance, fill = mode)) + theme_bw() + geom_bar(stat = "identity", width = .7) + labs(x = "", y = "Distances (km)", title = "Modal split per day", subtitle = "Share of distances per mode per day", caption = "GUFM Test user | Period: 01.06. - 15.06.2021") + geom_text(aes(x = started_on, label = prop_distance), colour = "white", size = 2, position = position_stack(vjust = 0.6)) + scale_fill_manual(values = mode_colors, limits = c("Mode::Bicycle", "Mode::Bus", "Mode::Car", "Mode::Motorbike","Mode::LightRail", "Mode::Subway", "Mode::RegionalTrain", "Mode::Tram", "Mode::Walk"), labels = c("Bicycle", "Bus", "Car", "Motorbike", "Lightrail", "Subway", "Train", "Tram", "Walk")) # Plot diurnal curve diurnal_curve %>% ggplot(aes(x = started_at, y = tracks_per_hour)) + geom_line(size = 1, stat = "identity") + geom_point() + theme_bw() + labs(x = "Time", y = "Percentage of trips", title = "Diurnal curves", subtitle = "Diurnal curves of test user", caption = "GUFM Test user | Period: 01.06. - 15.06.2021") # Plot a map # I prepared a geojson. Let's download it download <- getURL("https://hessenbox-a10.rz.uni-frankfurt.de/dl/fizfxmvtQ87MXYmT4EeeiB7/tracks.geojson") s.tracks <- readOGR(download, verbose = FALSE) ### Visualize tracks on a map factpal <- colorFactor(rainbow(10), s.tracks$mode) # choose a color palette #### Plot map # For other background maps go to: # https://leaflet-extras.github.io/leaflet-providers/preview/ map <- leaflet() %>% addProviderTiles(providers$CartoDB.DarkMatter) %>% addPolylines(data = s.tracks, stroke = TRUE, fillOpacity = 0.2, smoothFactor = 0.5, color = ~factpal(mode), group = "modes") %>% addLegend(pal = factpal, values = s.tracks$mode, group = "modes", position = "bottomleft") %>% addLayersControl(overlayGroups = c("modes") ) map # saveWidget(map, file = "/Users/robert.schoenduwe/Documents/New/map.html")