Skip to content

Instantly share code, notes, and snippets.

View andrewbtran's full-sized avatar

Andrew Tran andrewbtran

View GitHub Profile
@andrewbtran
andrewbtran / Mapping the data
Created August 7, 2016 00:03
mapping the traffic stop locations over census tracts
# We only need the columns with the latitude and longitude
coords <- stops[c("InterventionLocationLongitude", "InterventionLocationLatitude")]
# Making sure we are working with rows that don't have any blanks
coords <- coords[complete.cases(coords),]
library(sp)
# Letting R know that these are specifically spatial coordinates
sp <- SpatialPoints(coords)
@andrewbtran
andrewbtran / importing shapes
Created August 7, 2016 00:02
Importing shape files in for rendering in R
# Bring in the shape files for census tracts
require(rgdal)
# dsn is the folder the shape files are in. layer is the name of the file.
towntracts <- readOGR(dsn="shapes", layer="census_tracts")
# creating a copy
towntracts_only <- towntracts
@andrewbtran
andrewbtran / importing
Last active August 7, 2016 03:31
Importing data on Hamden traffic stops
# Bring in the data
stops <- read.csv("https://github.com/trendct/walkthroughs/raw/master/spatial-analysis-r-walkthrough/data/hamden_stops.csv", stringsAsFactors=FALSE)
# Check and eliminate the rows that don't have location information
stops <- stops[!is.na(stops$InterventionLocationLatitude),]
# Add a column to identifying a driver as white or a minority
stops$ethnicity <- ifelse(((stops$SubjectRaceCode == "W") & (stops$SubjectEthnicityCode =="N")), "White", "Minority")
# create the pop-up info
beer_advocate$pop_up_content <- paste(sep = "<br/>",
"<b><a href='", beer_advocate$beer_advocate_website, "'>", beer_advocate$brewery_name, "</a></b>", beer_advocate$address,
beer_advocate$phone_number, paste("Average beer rating: ", beer_advocate$beer_avg,"/5.0"))
# add colors corresponding to the average beer ratings
pal <- colorNumeric("YlOrRd", beer_advocate$beer_avg, n = 6)
m <- leaflet(data = beer_advocate) %>%
addTiles() %>%
library(htmlwidgets)
library(leaflet)
library(ggmap)
# This function geocodes a location (find latitude and longitude) using the Google Maps API
geo <- geocode(location = beer_advocate$address, output="latlon", source="google")
# add those coordinates to our dataset
beer_advocate$lon <- geo$lon
beer_advocate$lat <- geo$lat
# remove duplicate columns with unimportant info
beer_advocate$bottomdark_links._text <- NULL
beer_advocate$bottomdark_links._source <- NULL
beer_advocate$pageUrl <- NULL
beer_advocate$image <- NULL
beer_advocate$bottomdark_content_numbers._source <- NULL
beer_advocate$bottomlight_number <- NULL
beer_advocate$link._source <- NULL
# change the column names
@andrewbtran
andrewbtran / gist:505c1f5fcfba376d79d0
Created March 16, 2016 13:31
reading in beer advocate data
beer_advocate <- read.csv("beeradvocate.com API 14th Dec 10-37.csv")
beer_head <- head(beer_advocate)
@andrewbtran
andrewbtran / index.html
Created November 29, 2015 21:39
simple bar chart with new data
<!doctype html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="http://www.d3plus.org/js/d3.js"></script>
<!-- Load d3plus.js -->
<script src="http://www.d3plus.org/js/d3plus.js"></script>
<div id="viz"></div>
@andrewbtran
andrewbtran / index.html
Last active November 29, 2015 21:37
simple bar chart
<!doctype html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="http://www.d3plus.org/js/d3.js"></script>
<!-- Load d3plus.js -->
<script src="http://www.d3plus.org/js/d3plus.js"></script>
<div id="viz"></div>
@andrewbtran
andrewbtran / index.html
Created November 18, 2015 04:34
highcharts demo
<html>
<head>
<title>Highcharts Tutorial</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
$(document).ready(function() {