{"id":256,"date":"2016-11-10T18:55:37","date_gmt":"2016-11-11T01:55:37","guid":{"rendered":"https:\/\/blogs.ubc.ca\/anniefang\/?page_id=256"},"modified":"2017-03-18T22:30:26","modified_gmt":"2017-03-19T05:30:26","slug":"r-studio","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/anniefang\/r-studio\/","title":{"rendered":"R Studio"},"content":{"rendered":"<p><a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/break-and-enter-commercial.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-565\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/break-and-enter-commercial.png\" alt=\"\" width=\"741\" height=\"600\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/break-and-enter-commercial.png 741w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/break-and-enter-commercial-300x243.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Break-and-enter-residential-or-other.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-566\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Break-and-enter-residential-or-other.png\" alt=\"\" width=\"742\" height=\"599\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Break-and-enter-residential-or-other.png 742w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Break-and-enter-residential-or-other-300x242.png 300w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-567\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot.jpeg\" alt=\"\" width=\"605\" height=\"377\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot.jpeg 605w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot-300x187.jpeg 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-568\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot.png\" alt=\"\" width=\"605\" height=\"377\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot.png 605w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Crime2013DataPlot-300x187.png 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/mischef.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-569\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/mischef.png\" alt=\"\" width=\"741\" height=\"599\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/mischef.png 741w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/mischef-300x243.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/other-theft.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-570\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/other-theft.png\" alt=\"\" width=\"746\" height=\"601\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/other-theft.png 746w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/other-theft-300x242.png 300w\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.37.53-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-571\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.37.53-PM.png\" alt=\"\" width=\"606\" height=\"376\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.37.53-PM.png 606w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.37.53-PM-300x186.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.40.21-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-572\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.40.21-PM.png\" alt=\"\" width=\"606\" height=\"380\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.40.21-PM.png 606w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.40.21-PM-300x188.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.43.53-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-573\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.43.53-PM.png\" alt=\"\" width=\"606\" height=\"376\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.43.53-PM.png 606w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.43.53-PM-300x186.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.24-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-574\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.24-PM.png\" alt=\"\" width=\"605\" height=\"377\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.24-PM.png 605w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.24-PM-300x187.png 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.39-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-575\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.39-PM.png\" alt=\"\" width=\"604\" height=\"381\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.39-PM.png 604w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/Screen-Shot-2016-11-09-at-9.56.39-PM-300x189.png 300w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/theft-from-vehicle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-576\" src=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/theft-from-vehicle.png\" alt=\"\" width=\"743\" height=\"600\" srcset=\"https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/theft-from-vehicle.png 743w, https:\/\/blogs.ubc.ca\/anniefang\/files\/2016\/11\/theft-from-vehicle-300x242.png 300w\" sizes=\"auto, (max-width: 743px) 100vw, 743px\" \/><\/a>PROCESS<\/p>\n<p>After parsing, geocoding, and filtering the data,\u00a027275 out of 34354 records were retained.<\/p>\n<p>VISUALIZATION<\/p>\n<p>REFLECTION<\/p>\n<p>&nbsp;<\/p>\n<p>A <strong>write up<\/strong> about your\u00a0<u>process<\/u>\u00a0and some\u00a0<u>insights from your visualization<\/u> and a <u>reflection<\/u> on learning R and leaflet<\/p>\n<p><u>Process<\/u><u>:<\/u><\/p>\n<ul>\n<li>You will need to demonstrate that you understand each step of the process of the data visualization pipeline as it relates to the work you&#8217;ve done. For each chunk of code that you&#8217;ve written, you will need to explain in writing on your blog what each step is doing &#8211; imagine if you needed to go step by step with your methods to the City of Vancouver Open Data Department, how would you document each step? This is\u00a0in addition to\u00a0commenting your code.\n<ul>\n<li>After you finish creating the final data file to map, comment on: Understanding this data:\n<ol>\n<li>Lost records of data: How many records did you start with? After running the data through parsing and geocoding and filtering, how many values did you end up with?<\/li>\n<li>Error: Do you think this is a lot or a little \u2018error\u2019? How reliable is this data?<\/li>\n<li>File formats: What is the difference between a shape (shp), csv, excel, geojson file. Which one did you create? Why? Given an example of when you would use each file.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><u>Visualization<\/u><u>:<\/u><\/p>\n<ul>\n<li>Discuss the following. Be creative and thoughtful in your responses.\n<ol>\n<li>cartographic design: What are the cartographic constraints you encountered?<\/li>\n<li>Discuss some insights you&#8217;ve found in your visualization upon using it: the spatial distribution, the types of crimes, what other data might be useful in this context, the interactivity &#8211; is it useful or not, etc?<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><u>Reflection <\/u><\/p>\n<p>You have experience with expensive proprietary software packages ArcGIS and Adobe Illustrator that are supported by the UBC geography labs. Reflect on these few weeks of learning enough of an open source tool, R and leaflet, to create this interactive map. In your reflection discuss pros and cons of proprietary software and FOSS.<\/p>\n<pre>\r\n\r\n######################################################\r\n# Vancouver Crime: Data Processing Script\r\n# Date: October 30, 2016\r\n# By: Annie Fang\r\n# Desc: Vancouver Crime Data Visualization\r\n######################################################\r\n\r\n# ------------------------------------------------------------------ #\r\n# ---------------------- Install Libraries ------------------------- #\r\n# ------------------------------------------------------------------ #\r\ninstall.packages(\"GISTools\")\r\ninstall.packages(\"RJSONIO\")\r\ninstall.packages(\"rgdal\")\r\ninstall.packages(\"RCurl\")\r\ninstall.packages(\"curl\")\r\n\r\n# Unused Libraries:\r\n# install.packages(\"ggmap\")\r\n# library(ggmap)\r\n\r\n# ------------------------------------------------------------------ #\r\n# ----------------------- Load Libararies -------------------------- #\r\n# ------------------------------------------------------------------ #\r\nlibrary(GISTools)\r\nlibrary(RJSONIO)\r\nlibrary(rgdal)\r\nlibrary(RCurl)\r\nlibrary(curl)\r\n\r\n# ------------------------------------------------------------------ #\r\n# ---------------------------- Acquire ----------------------------- #\r\n# ------------------------------------------------------------------ #\r\n# access from the interweb using \"curl\"\r\nfname = curl('https:\/\/raw.githubusercontent.com\/anniefangg\/cartographiste\/master\/Crime2013.csv')\r\n\r\n# Read data as csv\r\ndata = read.csv(fname, header=T)\r\n\r\n# inspect your data\r\nprint(head(data))\r\n\r\n# ------------------------------------------------- #\r\n# -------------- Parse: Geocoder ------------------- #\r\n# ------------------------------------------------- #\r\n# change intersection to 00's\r\ndata$HUNDRED_BLOCK = gsub(\"X\", \"0\", data$HUNDRED_BLOCK)\r\nprint(head(data$HUNDRED_BLOCK))\r\n\r\n# Join the strings from each column together &amp; add \"Vancouver, BC\":\r\ndata$full_address = paste(data$HUNDRED_BLOCK,\r\npaste(data$STREET_NAME,\r\n\"Vancouver, BC\",\r\nsep=\", \"),\r\nsep=\" \")\r\n\r\n# removing \"Intersection \" from the full_address entries\r\ndata$full_address = gsub(\"Intersection \", \"\", data$full_address)\r\nprint(head(data$full_address))\r\n\r\n# a function taking a full address string, formatting it, and making\r\n# a call to the BC government's geocoding API\r\nbc_geocode = function(search){\r\n# return a warning message if input is not a character string\r\nif(!is.character(search)){stop(\"'search' must be a character string\")}\r\n\r\n# formatting characters that need to be escaped in the URL, ie:\r\n# substituting spaces ' ' for '%20'.\r\nsearch = RCurl::curlEscape(search)\r\n\r\n# first portion of the API call URL\r\nbase_url = \"http:\/\/apps.gov.bc.ca\/pub\/geocoder\/addresses.json?addressString=\"\r\n\r\n# constant end of the API call URL\r\nurl_tail = \"&amp;locationDescriptor=any&amp;maxResults=1&amp;interpolation=adaptive&amp;echo=true&amp;setBack=0&amp;outputSRS=4326&amp;minScore=1&amp;provinceCode=BC\"\r\n\r\n# combining the URL segments into one string\r\nfinal_url = paste0(base_url, search, url_tail)\r\n\r\n# making the call to the geocoding API by getting the response from the URL\r\nresponse = RCurl::getURL(final_url)\r\n\r\n# parsing the JSON response into an R list\r\nresponse_parsed = RJSONIO::fromJSON(response)\r\n\r\n# if there are coordinates in the response, assign them to `geocoords`\r\nif(length(response_parsed$features[[1]]$geometry[[3]]) &gt; 0){\r\ngeocoords = list(lon = response_parsed$features[[1]]$geometry[[3]][1],\r\nlat = response_parsed$features[[1]]$geometry[[3]][2])\r\n}else{\r\ngeocoords = NA\r\n}\r\n\r\n# returns the `geocoords` object\r\nreturn(geocoords)\r\n}\r\n\r\n# Geocode the events - we use the BC Government's geocoding API\r\n# Create an empty vector for lat and lon coordinates\r\nlat = c()\r\nlon = c()\r\n\r\n# loop through the addresses\r\nfor(i in 1:length(data$full_address)){\r\n# store the address at index \"i\" as a character\r\naddress = data$full_address[i]\r\n# append the latitude of the geocoded address to the lat vector\r\nlat = c(lat, bc_geocode(address)$lat)\r\n# append the longitude of the geocoded address to the lon vector\r\nlon = c(lon, bc_geocode(address)$lon)\r\n# at each iteration through the loop, print the coordinates - takes about 20 min.\r\nprint(paste(\"#\", i, \", \", lat[i], lon[i], sep = \",\"))\r\n}\r\n\r\n# add the lat lon coordinates to the dataframe\r\ndata$lat = lat\r\ndata$lon = lon\r\n\r\n# Write my file out\r\nofile = \"\/Users\/anniefang\/Desktop\/GEOB_472\/2013CaseLocationsDetails-Geo.csv\"\r\nwrite.csv(data, ofile)\r\n# Find my geocoded dataset on GitHub: \"https:\/\/raw.githubusercontent.com\/anniefangg\/cartographiste\/CID\/2013CaseLocationsDetails-Geo.csv\"\r\n\r\n# ------------------------------------------------- #\r\n# --------------------- Mine ---------------------- #\r\n# ------------------------------------------------- #\r\n# --- Examine the unique cases --- #\r\n\r\n# examine how the cases are grouped - are these intuitive?\r\nunique(data$TYPE)\r\n\r\n# examine the types of cases - can we make new groups that are more useful?\r\n# Print each unique case on a new line for easier inspection\r\nfor (i in 1:length(unique(data$TYPE))){\r\nprint(unique(data$TYPE)[i], max.levels=0)\r\n}\r\n\r\n# Resulting 6 Groups\r\n# [1] Other Theft\r\n# [1] Break and Enter Residential\/Other\r\n# [1] Mischief\r\n# [1] Theft of Vehicle\r\n# [1] Theft from Vehicle\r\n# [1] Break and Enter Commercial\r\n\r\n# ------------------------------------------------- #\r\n# --------------- Assign Vectors ------------------ #\r\n# ------------------------------------------------- #\r\n\r\n# Other Theft\r\nOther_Theft = c('Other Theft')\r\n\r\n# Break and Enter Residential\/Other\r\nBreak_and_Enter_Residential_or_Other = c('Break and Enter Residential\/Other')\r\n\r\n# Mischief\r\nMischief = c('Mischief')\r\n\r\n# Theft of Vehicle\r\nTheft_of_Vehicle = c('Theft of Vehicle')\r\n\r\n# Theft from Vehicle\r\nTheft_from_Vehicle = c('Theft from Vehicle')\r\n\r\n# Break and Enter Commercial\r\nBreak_and_Enter_Commercial = c('Break and Enter Commercial')\r\n\r\n# ------------------------------------------------- #\r\n# ------------------ Assign CID ------------------- #\r\n# ------------------------------------------------- #\r\n\r\n# give class id numbers:\r\ndata$cid = 9999\r\nfor(i in 1:length(data$TYPE)){\r\nif(data$TYPE[i] %in% Other_Theft){\r\ndata$cid[i] = 1\r\n}else if(data$TYPE[i] %in% Break_and_Enter_Residential_or_Other){\r\ndata$cid[i] = 2\r\n}else if(data$TYPE[i] %in% Theft_of_Vehicle){\r\ndata$cid[i] = 3\r\n}else if(data$TYPE[i] %in% Theft_from_Vehicle){\r\ndata$cid[i] = 4\r\n}else if(data$TYPE[i] %in% Break_and_Enter_Commercial){\r\ndata$cid[i] = 5\r\n}else{\r\ndata$cid[i] = 0\r\n}\r\n}\r\n\r\n# --- handle overlapping points --- #\r\n# Set offset for points in same location:\r\ndata$lat_offset = data$lat\r\ndata$lon_offset = data$lon\r\n\r\n# Run loop - if value overlaps, offset it by a random number\r\nfor(i in 1:length(data$lat)){\r\nif ( (data$lat_offset[i] %in% data$lat_offset) &amp;&amp; (data$lon_offset[i] %in% data$lon_offset)){\r\ndata$lat_offset[i] = data$lat_offset[i] + runif(1, 0.0001, 0.0005)\r\ndata$lon_offset[i] = data$lon_offset[i] + runif(1, 0.0001, 0.0005)\r\n}\r\n}\r\n\r\n# --- what are the top calls? --- #\r\n# get a frequency distribution of the calls:\r\ntop_calls = data.frame(table(data$TYPE))\r\ntop_calls = top_calls[order(top_calls$Freq), ]\r\nprint(top_calls)\r\n\r\n# Var1 Freq\r\n# 6 Theft of Vehicle 840\r\n# 1 Break and Enter Commercial 1758\r\n# 2 Break and Enter Residential\/Other 3008\r\n# 3 Mischief 3715\r\n# 5 Theft from Vehicle 7012\r\n# 4 Other Theft 10942\r\n\r\n# ------------------------------------------------------------------ #\r\n# ---------------------------- Filter ------------------------------ #\r\n# ------------------------------------------------------------------ #\r\n# Filter data outside vancouver boundaries or an NA\r\ndata_filter = subset(data, (lat &lt;= 49.313162) &amp; (lat &gt;= 49.199554) &amp;\r\n(lon &lt;= -123.019028) &amp; (lon &gt;= -123.271371) &amp; is.na(lon) == FALSE )\r\n# plot the data\r\nplot(data_filter$lon, data_filter$lat)\r\n\r\n# Write out my file:\r\nofile_filtered = '\/Users\/anniefang\/Desktop\/GEOB_472\/Crime2013-CID.csv'\r\nwrite.csv(data_filter, ofile_filtered)\r\n\r\n# Data set can also be found on GitHub: \"https:\/\/raw.githubusercontent.com\/anniefangg\/cartographiste\/CID\/Crime2013-CID.csv\"\r\n\r\n# --- Convert Data to Shapefile --- #\r\n# store coordinates in dataframe\r\ncoords_crime = data.frame(data_filter$lon_offset, data_filter$lat_offset)\r\n\r\n# create spatialPointsDataFrame\r\ndata_shp = SpatialPointsDataFrame(coords = coords_crime, data = data_filter)\r\n\r\n# set the projection to wgs84\r\nprojection_wgs84 = CRS(\"+proj=longlat +datum=WGS84\")\r\nproj4string(data_shp) = projection_wgs84\r\n\r\n# set an output folder for our geojson files:\r\ngeofolder = \"\/Users\/anniefang\/Desktop\/GEOB_472\/\"\r\n\r\n# Join the folderpath to each file name:\r\nopoints_shp = paste(geofolder, \"crime_\", \"2013\", \".shp\", sep = \"\")\r\nprint(opoints_shp)\r\n# File Name: '\/Users\/anniefang\/Desktop\/GEOB_472\/crime_2013.shp'\r\n\r\nopoints_geojson = paste(geofolder, \"crime_\", \"2013\", \".geojson\", sep = \"\")\r\nprint(opoints_geojson)\r\n# File Name: '\/Users\/anniefang\/Desktop\/GEOB_472\/crime_2013.geojson'\r\n\r\n# write the file to a shp\r\nwriteOGR(data_shp, opoints_shp, layer = \"data_shp\", driver = \"ESRI Shapefile\",\r\ncheck_exists = FALSE)\r\n\r\n# write file to geojson\r\nwriteOGR(data_shp, opoints_geojson, layer = \"data_shp\", driver = \"GeoJSON\",\r\ncheck_exists = FALSE)\r\n\r\n# ------------------------------------------------------------------ #\r\n# ---------------------- Create Hexagon Grid ----------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\n# ------------------------ aggregate to a grid --------------------- #\r\n# ref: http:\/\/www.inside-r.org\/packages\/cran\/GISTools\/docs\/poly.counts\r\n# set the file name - combine the shpfolder with the name of the grid\r\ngrid_fn = '\/Users\/anniefang\/Desktop\/GEOB_472\/hexagonfiles\/grid_fn.txt'\r\n\r\n# read in the hex grid setting the projection to utm10n\r\nhexgrid = readOGR(grid_fn, 'OGRGeoJSON')\r\n\r\n# OGR data source with driver: GeoJSON\r\n# Source: \"\/Users\/anniefang\/Desktop\/GEOB_472\/hexagonfiles\/grid_fn.txt\", layer: \"OGRGeoJSON\"\r\n# with 5104 features\r\n# It has 4 fields\r\n\r\n# transform the projection to wgs84 to match the point file and store it to a new variable (see variable: projection_wgs84)\r\nhexgrid_wgs84 = spTransform(hexgrid, projection_wgs84)\r\n\r\n# ------------------------------------------------------------------ #\r\n# ------------------ Assign Values to Hexagons --------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\n# Use the poly.counts() function to count the number of occurrences of calls per grid cell\r\ngrid_cnt = poly.counts(data_shp, hexgrid_wgs84)\r\n\r\n# define the output names:\r\nohex_shp = paste(geofolder, \"hexgrid_250m_\", \"Crime2013\", \"_cnts\",\r\n\".shp\", sep = \"\")\r\nprint(ohex_shp)\r\n# Output Name File: '\/Users\/anniefang\/Desktop\/GEOB_472\/hexgrid_250m_Crime2013_cnts.shp'\r\n\r\nohex_geojson = paste(geofolder, \"hexgrid_250m_\",\"Crime2013\",\"_cnts\",\".geojson\")\r\nprint(ohex_geojson)\r\n# Output Name File: '\/Users\/anniefang\/Desktop\/GEOB_472\/ hexgrid_250m_ Crime2013 _cnts .geojson'\r\n\r\n# write the file to a shp\r\nwriteOGR(check_exists = FALSE, hexgrid_wgs84, ohex_shp,\r\nlayer = \"hexgrid_wgs84\", driver = \"ESRI Shapefile\")\r\n\r\n# write file to geojson\r\nwriteOGR(check_exists = FALSE, hexgrid_wgs84, ohex_geojson,\r\nlayer = \"hexgrid_wgs84\", driver = \"GeoJSON\")\r\n\r\n# ------------------------------------------------------------------ #\r\n# --------------------- END DATA PROCESSING ------------------------ #\r\n# ------------------------------------------------------------------ #\r\n\r\n######################################################\r\n# Vancouver Crime: Leaflet Script\r\n# Date: October 30, 2016\r\n# By: Annie Fang\r\n# Desc: Vancouver Crime Data Visualization\r\n######################################################\r\n\r\n# ------------------------------------------------------------------ #\r\n# -------------------- Install Leaflet Library --------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\n# install the leaflet library\r\ninstall.packages(\"leaflet\")\r\n\r\n# add the leaflet library to your script\r\nlibrary(leaflet)\r\n\r\n# initiate the leaflet instance and store it to a variable\r\nm = leaflet()\r\n\r\n# we want to add map tiles so we use the addTiles() function - the default is openstreetmap\r\nm = addTiles(m)\r\n\r\n# we can add markers by using the addMarkers() function\r\nm = addMarkers(m, lng=-123.256168, lat=49.266063, popup=\"T\")\r\n\r\n# we can \"run\"\/compile the map, by running the printing it\r\nm\r\n\r\n# ------------------------------------------------------------------ #\r\n# -------------------- Working With Point Data --------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\nfilename = \"\/Users\/anniefang\/Desktop\/GEOB_472\/Crime2013-CID.csv\"\r\ndata_filter = read.csv(filename, header = TRUE)\r\n\r\n# Data on GitHub Provided:\r\n# data_filter = read.csv(curl('https:\/\/raw.githubusercontent.com\/anniefangg\/cartographiste\/CID\/Crime2013-CID.csv'), header=TRUE)\r\n\r\n# subset out your data_filter:\r\ndf_Other_Theft = subset(data_filter, cid == 1)\r\ndf_Break_and_Enter_Residential_or_Other = subset(data_filter, cid == 2)\r\ndf_Mischief = subset(data_filter, cid == 3)\r\ndf_Theft_of_Vehicle = subset(data_filter, cid == 4)\r\ndf_Theft_from_Vehicle = subset(data_filter, cid == 5)\r\ndf_Break_and_Enter_Commercial = subset(data_filter, cid == 0)\r\n\r\n# ------------------------------------------------------------------ #\r\n# -------------------- Initiate Base Map Tiles --------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\n# initiate leaflet\r\nm = leaflet()\r\n\r\n# add openstreetmap tiles (default)\r\nm = addTiles(m)\r\n\r\n# you can now see that our maptiles are rendered\r\nm\r\n\r\n# ------------------------------------------------------------------ #\r\n# --------------- Define Symbology for Point Layers ---------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'),\r\ndomain = data_filter$cid)\r\n# Other_Theft\r\nm = addCircleMarkers(m,\r\nlng = df_Other_Theft$lon_offset,\r\nlat = df_Other_Theft$lat_offset,\r\npopup = df_Other_Theft$Case_Type,\r\nradius = 2,\r\nstroke = FALSE,\r\nfillOpacity = 0.75,\r\ncolor = colorFactors(df_Other_Theft$cid),\r\ngroup = \"1 - Other Theft\")\r\n\r\n# Break_and_Enter_Residential_or_Other\r\nm = addCircleMarkers(m,\r\nlng = df_Break_and_Enter_Residential_or_Other$lon_offset,\r\nlat = df_Break_and_Enter_Residential_or_Other$lat_offset,\r\npopup = df_Break_and_Enter_Residential_or_Other$Case_Type,\r\nradius = 2,\r\nstroke = FALSE,\r\nfillOpacity = 0.75,\r\ncolor = colorFactors(df_Other_Theft$cid),\r\ngroup = \"2 - Break and Enter Residential or Other\")\r\n\r\n# Mischief\r\nm = addCircleMarkers(m,\r\nlng = df_Mischief$lon_offset,\r\nlat = df_Mischief$lat_offset,\r\npopup = df_Mischief$Case_Type,\r\nradius = 2,\r\nstroke = FALSE,\r\nfillOpacity = 0.75,\r\ncolor = colorFactors(df_Mischief$cid),\r\ngroup = \"3 - Mischief\")\r\n\r\n# Theft_of_Vehicle\r\nm = addCircleMarkers(m,\r\nlng = df_Theft_of_Vehicle$lon_offset,\r\nlat = df_Theft_of_Vehicle$lat_offset,\r\npopup = df_Theft_of_Vehicle$Case_Type,\r\nradius = 2,\r\nstroke = FALSE,\r\nfillOpacity = 0.75,\r\ncolor = colorFactors(df_Theft_of_Vehicle$cid),\r\ngroup = \"4 - Theft of Vehicle\")\r\n\r\n# Theft_from_Vehicle\r\nm = addCircleMarkers(m,\r\nlng = df_Theft_from_Vehicle$lon_offset,\r\nlat = df_Theft_from_Vehicle$lat_offset,\r\npopup = df_Theft_from_Vehicle$Case_Type,\r\nradius = 2,\r\nstroke = FALSE,\r\nfillOpacity = 0.75,\r\ncolor = colorFactors(df_Theft_from_Vehicle$cid),\r\ngroup = \"5 - Theft from Vehicle\")\r\n\r\n# Break_and_Enter_Commercial\r\nm = addCircleMarkers(m,\r\nlng = df_Break_and_Enter_Commercial$lon_offset,\r\nlat = df_Break_and_Enter_Commercial$lat_offset,\r\npopup = df_Break_and_Enter_Commercial$Case_Type,\r\nradius = 2,\r\nstroke = FALSE,\r\nfillOpacity = 0.75,\r\ncolor = colorFactors(df_Break_and_Enter_Commercial$cid),\r\ngroup = \"0 - Break and Enter Commercial\")\r\n\r\n# ------------------------------------------------------------------ #\r\n# ------------------- Additional Base Map Tiles -------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\nm = addTiles(m, group = \"OSM (default)\")\r\nm = addProviderTiles(m,\"Stamen.Toner\", group = \"Toner\")\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\n\r\nm = addLayersControl(m,\r\nbaseGroups = c(\"Toner Lite\",\"Toner\"),\r\noverlayGroups = c(\"1 - Other Theft\", \"2 - Break and Enter Residential or Other\",\r\n\"3 - Mischief\",\"4 - Theft of Vehicle\", \"5 - Theft from Vehicle\",\r\n\"0 - Break and Enter Commercial\")\r\n)\r\n\r\n# make the map\r\nm\r\n\r\n# ------------------------------------------------------------------ #\r\n# ----------------------------- Toggle ----------------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\n# Put variables into a list to loop:\r\ndata_filterlist = list(df_Other_Theft = subset(data_filter, cid == 1),\r\ndf_Break_and_Enter_Residential_or_Other = subset(data_filter, cid == 2),\r\ndf_Mischief = subset(data_filter, cid == 3),\r\ndf_Theft_of_Vehicle = subset(data_filter, cid == 4),\r\ndf_Theft_from_Vehicle = subset(data_filter, cid == 5),\r\ndf_Break_and_Enter_Commercial = subset(data_filter, cid == 0))\r\n\r\n# List of Layers:\r\nlayerlist = c(\"1 - Other Theft\", \"2 - Break and Enter Residential or Other\",\r\n\"3 - Mischief\",\"4 - Theft of Vehicle\", \"5 - Theft from Vehicle\",\r\n\"0 - Break and Enter Commercial\")\r\n\r\n# Same color variable:\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'), domain=data_filter$cid)\r\n\r\n# Loop - add markers to the map object:\r\nfor (i in 1:length(data_filterlist)){\r\nm = addCircleMarkers(m,\r\nlng=data_filterlist[[i]]$lon_offset,\r\nlat=data_filterlist[[i]]$lat_offset,\r\npopup=data_filterlist[[i]]$Case_Type,\r\nradius=2,\r\nstroke = FALSE,\r\nfillOpacity = 0.75,\r\ncolor = colorFactors(data_filterlist[[i]]$cid),\r\ngroup = layerlist[i]\r\n)\r\n}\r\n\r\n# ------------------------------------------------------------------ #\r\n# ------------------- Radiobutton Functionality -------------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\nm = addTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\nm = addLayersControl(m,\r\noverlayGroups = c(\"Toner Lite\"),\r\nbaseGroups = layerlist\r\n)\r\nm\r\n\r\n# ------------------------------------------------------------------ #\r\n# ---------------- Aggregate Data to Hexagonal Grid ---------------- #\r\n# ------------------------------------------------------------------ #\r\n\r\nlibrary(rgdal)\r\nlibrary(GISTools)\r\n\r\n# define the filepath\r\nhex_crime_fn = '\/Users\/anniefang\/Desktop\/GEOB_472\/hexgrid_250m_Crime2013_cnts.geojson'\r\n\r\n# read in the geojson\r\nhex_crime = readOGR(hex_crime_fn, \"OGRGeoJSON\")\r\n\r\n# OGR data source with driver: GeoJSON\r\n# Source: \"https:\/\/raw.githubusercontent.com\/anniefangg\/cartographiste\/Hexgrid-Crime2013-geojson\/%20hexgrid_250m_%20Crime2013%20_cnts%20.geojson\", layer: \"OGRGeoJSON\"\r\n# with 5104 features\r\n# It has 4 fields\r\n\r\n# Stamen Toner Lite Style\r\nm = leaflet()\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\n\r\n# Create a continuous palette function\r\npal = colorNumeric(\r\npalette = \"Greens\",\r\ndomain = hex_crime$data\r\n)\r\n\r\n# add the polygons to the map\r\nm = addPolygons(m,\r\ndata = hex_crime,\r\nstroke = FALSE,\r\nsmoothFactor = 0.2,\r\nfillOpacity = 1,\r\ncolor = ~pal(hex_crime$data),\r\npopup = paste(\"Number of calls: \", hex_crime$data, sep=\"\")\r\n)\r\n\r\n# Add legend\r\nm = addLegend(m, \"bottomright\", pal = pal, values = hex_crime$data,\r\ntitle = \"Vancouver Crime Density 2013\",\r\nlabFormat = labelFormat(prefix = \" \"),\r\nopacity = 0.75\r\n)\r\n\r\nm\r\n\r\n# initiate leaflet map layer\r\nm = leaflet()\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\n\r\n# --- hex grid --- #\r\n# store the file name of the geojson hex grid\r\nhex_crime_fn = '\/Users\/anniefang\/Desktop\/GEOB_472\/hexgrid_250m_Crime2013_cnts.geojson'\r\n\r\n# read in the data\r\nhex_crime = readOGR(hex_crime_fn, \"OGRGeoJSON\")\r\n\r\n# Create a continuous palette function\r\npal = colorNumeric(\r\npalette = \"Greens\",\r\ndomain = hex_crime$data\r\n)\r\n\r\n# add hex grid\r\nm = addPolygons(m,\r\ndata = hex_crime,\r\nstroke = FALSE,\r\nsmoothFactor = 0.2,\r\nfillOpacity = 1,\r\ncolor = ~pal(hex_crime$data),\r\npopup= paste(\"Number of calls: \",hex_crime$data, sep=\"\"),\r\ngroup = \"hex\"\r\n)\r\n\r\n# add legend\r\nm = addLegend(m, \"bottomright\", pal = pal, values = hex_crime$data,\r\ntitle = \"Vancouver Crime Density 2013\",\r\nlabFormat = labelFormat(prefix = \" \"),\r\nopacity = 0.75\r\n)\r\n\r\n# --- points data --- #\r\n\r\n# subset out your data:\r\ndf_Other_Theft = subset(data_filter, cid == 1)\r\ndf_Break_and_Enter_Residential_or_Other = subset(data_filter, cid == 2)\r\ndf_Mischief = subset(data_filter, cid == 3)\r\ndf_Theft_of_Vehicle = subset(data_filter, cid == 4)\r\ndf_Theft_from_Vehicle = subset(data_filter, cid == 5)\r\ndf_Break_and_Enter_Commercial = subset(data_filter, cid == 0)\r\n\r\ndata_filterlist = list(df_Other_Theft = subset(data_filter, cid == 1),\r\ndf_Break_and_Enter_Residential_or_Other = subset(data_filter, cid == 2),\r\ndf_Mischief = subset(data_filter, cid == 3),\r\ndf_Theft_of_Vehicle = subset(data_filter, cid == 4),\r\ndf_Theft_from_Vehicle = subset(data_filter, cid == 5),\r\ndf_Break_and_Enter_Commercial = subset(data_filter, cid == 0))\r\n# List of Layers:\r\nlayerlist = c(\"1 - Other Theft\", \"2 - Break and Enter Residential or Other\",\r\n\"3 - Mischief\",\"4 - Theft of Vehicle\", \"5 - Theft from Vehicle\",\r\n\"0 - Break and Enter Commercial\")\r\n\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'), domain=data_filter$cid)\r\nfor (i in 1:length(data_filterlist)){\r\nm = addCircleMarkers(m,\r\nlng=data_filterlist[[i]]$lon_offset, lat=data_filterlist[[i]]$lat_offset,\r\npopup=data_filterlist[[i]]$Case_Type,\r\nradius=2,\r\nstroke = FALSE, fillOpacity = 0.75,\r\ncolor = colorFactors(data_filterlist[[i]]$cid),\r\ngroup = layerlist[i]\r\n)\r\n\r\n}\r\n\r\nm = addLayersControl(m,\r\noverlayGroups = c(\"Toner Lite\", \"hex\"),\r\nbaseGroups = layerlist\r\n)\r\n\r\n# show map\r\nm\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PROCESS After parsing, geocoding, and filtering the data,\u00a027275 out of 34354 records were retained. VISUALIZATION REFLECTION &nbsp; A write up about your\u00a0process\u00a0and some\u00a0insights from your visualization and a reflection on learning R and leaflet Process: You will need to demonstrate that you understand each step of the process of the data visualization pipeline as it relates to the work you&#8217;ve done. For each chunk of code that you&#8217;ve written, you will need to explain in writing on your blog what each step is doing &#8211; imagine if you needed to&#8230;<a class=\"read-more\" href=\"https:\/\/blogs.ubc.ca\/anniefang\/r-studio\/\">read more<\/a><\/p>\n","protected":false},"author":35383,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-256","page","type-page","status-publish","hentry","et-no-image","et-bg-layout-dark","et-white-bg"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/pages\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/users\/35383"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":7,"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/pages\/256\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/pages\/256\/revisions\/577"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/anniefang\/wp-json\/wp\/v2\/media?parent=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}