Giter Site home page Giter Site logo

hystreet's Introduction

hystReet

CRAN Build Status Build status Project Status: Active – The project has reached a stable, usable state and is being actively developed.

Introduction

hystreet is a company that collects data on pedestrian traffic in shopping streets of different German cities. After registering you can access and download the data via their website.

Installation

Until now the package is not on CRAN but you can install it from GitHub with the following command:

if (!require("devtools"))
  install.packages("devtools")
devtools::install_github("JohannesFriedrich/hystReet")

API Keys

To use this package, you will first need to get a hystreet API key. To do so, you first need to set up an account on https://hystreet.com/. After that you can request an API key via e-mail. Once your request has been granted, you will find you key in your hystreet account profile.

Now you have three options:

Once you have your key, you can save it as an environment variable for the current session by running the following command:

Sys.setenv(HYSTREET_API_TOKEN = "PASTE YOUR API TOKEN HERE")
  1. Alternatively, you can set it permanently with the help of usethis::edit_r_environ() by adding the following line to your .Renviron:
HYSTREET_API_TOKEN = PASTE YOUR API TOKEN HERE
  1. If you don’t want to save your API token here, you can enter it for each function of this package using the API_token parameter.

Usage

Function name Description Example
get_hystreet_stats() request common statistics about the hystreet project get_hystreet_stats()
get_hystreet_locations() request all available locations get_hystreet_locations()
get_hystreet_station_data() request data from a stations get_hystreet_station_data(71)
set_hystreet_token() set your API token set_hystreet_token(123456789)

Load some statistics

The function get_hystreet_stats() summarises the number of available stations and the sum of all counted pedestrians.

library(hystReet)
## Loading required package: httr
## Loading required package: jsonlite
## 
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten

stats <- get_hystreet_stats()
stats

stations

today_count

119

1085039

Request all stations

The function get_hystreet_locations() generates a data frame with all available stations of the project.

locations <- get_hystreet_locations()
locations

id

name

city

251

Annastraße

Augsburg

209

Lange Straße

Oldenburg

151

Stiftstraße

Stuttgart

253

Neustraße

Bocholt

168

Hohe Straße (Nord)

Köln

93

Große Bleichen

Hamburg

135

Schönbornstraße

Würzburg

252

Holm

Flensburg

131

Sack

Braunschweig

142

Hauptstraße (Süd)

Erlangen

Request data from a specific station

The (probably) most interesting function is get_hystreet_station_data(). Using the hystreetID it is possible to request a specific station. By default, all the data from the current day are received. With the query argument it is possible to define the time and sampling frame of the data more precisely: from: datetime of earliest measurement (default: today 00:00:00:): e.g. “2018-10-01 12:00:00” or “2018-10-01” to : datetime of latest measurement (default: today 23:59:59): e.g. “2018-12-01 12:00:00” or “2018-12-01” resoution: Resultion for the measurement (default: hour): “day”, “hour”, “month”, “week”

data <- get_hystreet_station_data(
  hystreetId = 71,
  query = list(from = "2018-12-01", to = "2018-12-31", resolution = "day"))

Some ideas for visualising the data

Let´s see if we can find the busiest days in December 2018. Saturdays were probably quite busy, while there should have been substantially less pedestrian traffic on the 24th and 25th of December, both of which are holidays in Germany.

data <- get_hystreet_station_data(
    hystreetId = 71, 
    query = list(from = "2018-12-01", to = "2019-01-01", resolution = "hour"))
ggplot(data$measurements, aes(x = timestamp, y = pedestrians_count, colour = weekdays(timestamp))) +
  geom_path(group = 1) +
  scale_x_datetime(date_breaks = "7 days") +
  scale_x_datetime(labels = date_format("%d.%m.%Y")) +
  labs(x = "Date",
       y = "Pedestrians",
       colour = "Day")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

Compare different stations

Now let´s compare data from different stations:

  1. Load the data
data_73 <- get_hystreet_station_data(
    hystreetId = 73, 
    query = list(from = "2019-01-01", to = "2019-01-31", resolution = "day"))$measurements %>% 
  select(pedestrians_count, timestamp) %>% 
  mutate(station = 73)

data_74 <- get_hystreet_station_data(
    hystreetId = 74, 
    query = list(from = "2019-01-01", to = "2019-01-31", resolution = "day"))$measurements %>% 
    select(pedestrians_count, timestamp) %>% 
  mutate(station = 74)

data <- bind_rows(data_73, data_74)
ggplot(data, aes(x = timestamp, y = pedestrians_count, fill = weekdays(timestamp))) +
  geom_bar(stat = "identity") +
  scale_x_datetime(labels = date_format("%d.%m.%Y")) +
  facet_wrap(~station, scales = "free_y") +
  theme(legend.position = "bottom",
        axis.text.x = element_text(angle = 45, hjust = 1))

Highest ratio (pedestrians/day)

Now a little bit of big data analysis. Let´s find the station with the highest pedestrians per day ratio:

hystreet_ids <- get_hystreet_locations()

all_data <- lapply(hystreet_ids[,"id"], function(x){
  temp <- get_hystreet_station_data(
    hystreetId = x)
  
  
    lifetime_count <- temp$statistics$lifetime_count
    days_counted <- as.numeric(temp$metadata$latest_measurement_at  - temp$metadata$earliest_measurement_at)
    
    return(data.frame(
      id = x,
      station = paste0(temp$city, " (",temp$name,")"),
      ratio = lifetime_count/days_counted))
  
})

ratio <- bind_rows(all_data)

Which stations have the highest ratio?

ratio %>% 
  top_n(5, ratio) %>% 
  arrange(desc(ratio))
##    id                     station    ratio
## 1  73  München (Neuhauser Straße) 91904.43
## 2  47 Köln (Schildergasse (West)) 86939.41
## 3 165   München (Kaufingerstraße) 71187.87
## 4  48  Köln (Hohe Straße (Mitte)) 65037.64
## 5  63      Hannover (Georgstraße) 64433.47

Now let´s visualise the top 10 locations:

ggplot(ratio %>% 
         top_n(10,ratio), aes(station, ratio)) +
  geom_bar(stat = "identity") +
  labs(x = "Location",
       y = "Pedestrians per day") + 
    theme(legend.position = "bottom",
        axis.text.x = element_text(angle = 45, hjust = 1))

hystreet's People

Contributors

jobreu avatar johannesfriedrich avatar uepsilon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.