Jekyll, R Markdown and Plotly (Fulton County Example)

R Markdown and Plotly

July 8 2017 Saul Cruz

Objective

In this post, I’ll show a very simple example of how to use plotly using R and Jekyll. This post will basically demonstrate the function plot_ly().

For more information please refer to Plot_ly R Documentation

Pre-requisites

install.packages(plotly)

Dataset

The dataset used for this exploratory analysis can be found in Fulton Conuty Open Data and represents the base (ground-level) outline, or footprint, of buildings and other man-made structures in Fulton County, Georgia.

I download the data using getURL() and load it into a dataframe.

Exploratory Analysis using Plotly

This simple example shows the use of add_lines() , this function connects x/y pairs with lines in the order of their x values, it is very helpful when plotting time series, for more information about it and some more examples please refer to the Plot_ly interface

Below, there’s an example that helps us to understand how many buildings have been built over time in Fulton, County, Georgia.

I do a simple count by year using the table() function, and I convert it back to DataFrame to easily visualize it.

data_count_by_year<-data.frame(table(data$YearBuilt))
colnames(data_count_by_year) <- c("Year", "No.Buildings")
p1 <- plot_ly(data_count_by_year, x = ~Year, y = ~No.Buildings) %>% 
  add_lines(name = "No.Buildings") %>%
        layout(title="# of Buildings over time in Fulton, Georgia")
        
p1

Jekyll

To be able to display this plot I convert the elements to html. using the following function knit_htmlwidgets(“2017-07-08-Plotly_Example.Rmd”), this function converts the .Rmd file to html, and after that you can just call the html file from your post markdown file in jekyll. Thanks to Ben Cunningham and Brendan Rocks, Ben’s example really helped me to get where I needed.

knit_htmlwidgets <- function(input,
                             output_dir = "./_includes/htmlwidgets",
                             ...) {
        
        file_name <- rev(unlist(strsplit(input, split = "/")))[1]
        path <- rmarkdown::render(input, "html_document", output_dir = output_dir)
        remove_doctype(path)
        
}

remove_doctype <- function(input) {
        
        html_lines <- readLines(input)
        keep <- grep("^<!DOCTYPE html>$", html_lines, invert = TRUE)
        writeLines(html_lines[keep], input)
        
}

References

Coursera Week 3 (R Markdown) Product Development Tools

Read More

R Markdown and Leaflet (Atlanta Example)

R Markdown and Leaflet

June 25 2017 Saul Cruz

Objective

The objective of this post is to do a very simple exploratory analysis of crime in Atlanta, Georgia. I recently moved to Atlanta from Houston, and found a very nitty tool called Leaflet, in this post I will show a simple use case of this library.

Pre-requisites

install.packages(leaflet) library(leaflet)

Dataset

The dataset used for this exploratory analysis can be found in University of Georgia Police Department

The last update was on May 10, 2017, it contains 10.4 K records

Data Cleansing

These are the steps followed to clean the data, make it more readable and relevant. This is not feature selection since we’re not predicting yet.

Since we are only interested in crimes which take place in Atlanta, we need to restrict the data set. As stated in Kahle & Wickham’s article To determine a bounding box, we use google maps to define the top and bottom boundaries (lats and lons), then we create the map using qmap from ggmap package

atlanta<-data[-84.43743<=data$longitude & data$longitude<=-84.36238 & 33.70419<=data$latitude & data$latitude<=33.76848,]

Exploratory Analysis using Leaflet

Looks like the input data is not correct as most of the locations is the actual Georgia State Capitol.

However, this simple example shows the use of addMarkers , the use of Popups and labels, I’m using the paste function to concatenate multiple labels.

crimeLatLong<-data.frame(lat=atlanta$latitude,lng=atlanta$longitude)
crimeTitles<-atlanta$incident_type_primary
dateTitles<-atlanta$incident_datetime
incidentTypeTitles<-atlanta$parent_incident_type

crimeLatLong%>% 
  leaflet()%>%
  addTiles()%>%
  addMarkers(clusterOptions = markerClusterOptions(), popup = paste("Crime:",crimeTitles, "<br>",
                                                                    "Date:",dateTitles, "<br>",
                                                                    "Type:",incidentTypeTitles))

References

Coursera Week 2 (R Markdown) Product Development Tools

Read More