Hey guys, in this video I wanted to do a little walkthrough of how to work with spatial data in R.
In particular,
I have a short script that I've been playing around with and I wanted to go over that
produces a plot like this in R.
So I guess I will go into the details about what this is in a minute
but essentially this is a shapefile--
the black outline is an outline of
the city of Chicago--and then I have overlaid some spatial data on top of that plot and the spatial data are
coordinates for incidences of gun violence in the first six months of
2019. So for this video, I wanted to go over how you work with spatial data, the basics of it, and how to
get a plot like this in R. So let's go ahead and get into the code and as usual
I'll try to put a link to the code and to everything that I reference in the description box below.
To begin with, I'm just going to load in some R
library packages. I'm going to load in datatable for working with data tables,
ggplot2 for graphics, and then I'm going to load in two packages for working with spatial data.
One is sp and another one is called rgdal. Then after I load in those packages,
I'm going to read in my data table using fread from the data table package
and this is a data table that I've already created and cleaned up. It has 23 variables and
1,029 observations and like I said before, these are observations of gun violence in Chicago
from January 1st, 2019 to the end of June
2019. So basically the first six months of
this year. And it defines gun violence as either someone being killed from a gun violence incident or someone being injured and
it also had the addresses for all the incidences that occurred.
This was data that I scraped from a publicly available data set and
the link to where I got the data set is this website gunviolencearchive.org.
So this website seems to have gun violence
public information from across the United States and then you can search the database
and basically query for whatever date range, location,
you know, that you want. So you can basically query this public database and that's what I did and I downloaded the data and then
cleaned it up already.
The data comes with the addresses in Chicago where these violence incidences occur
and I'll talk in a minute about how we can change the addresses into coordinates and how we can plot the coordinates on a shapefile
like this. So that's the data set and that's where I got the data from. Then this next line
just makes sure I loaded in the data as a data table. Then for some reason
I noticed that some of these observations, even though I query the database for just observations in Chicago,
it also gave me some suburbs that are like right outside Chicago. So this line I'm filtering for some of the observations.
It takes out eight observations that are not considered to be in Chicago.
And then this next line is what actually will create a spatial data set.
So I'm not going to go into how I did it in this video,
but basically there are
different websites and different scripts that you can use to convert an
address, if you have the actual address line and the city and the state, you can convert those to latitude and longitude coordinates.
So I have already done that in this data set. I used a script that converted according to
Google Maps,
I think it was. The script used some kind of Google system to get the latitude and longitude.
So I have that here. And there are different websites and things that you can use,
I think I'll make a separate video about how I can get these
coordinates. Making a spatial data set, you need to have the coordinates. You can use different reference systems, but I'm going to use
longitude and latitude. So for this line of code,
I say to look at the longitude and latitude
in my data table from chi_table and the column names are google_longitude and
google_latitude. I'm going to run that. Then this basically assigns a coordinate reference system to the data set and I'm using
longitude and latitude. I'm going to apply that coordinate reference system and I do that with this next line as well.
Now I have basically made my data spatial data.
If I plot chi_tab,
it's going to open up a plot that has the
1000+ observations plotted according to certain coordinates and the coordinates are based on the longitude and latitude
reference system.
I'm partially there to getting my map and now I just have to overlay these points to the map of Chicago.
To do that, I need something that's called a shapefile, which I could also get just from a public website online.
When I googled shape file Chicago,
the first thing that popped up was this
Chicago data portal and I clicked the first link, which gave me the boundaries for community areas and this is what the
shapefile looks like and this is what I loaded in. This gives you the ability to download the shapefile.
It's a couple of files in a folder and this is what the shapefile will look like when you load it in. So basically
Chicago has 77 different communities and it gives you what are called polygons for the different communities'
areas, according to their coordinates as well. So to load in that shapefile,
I'm going to run this line of code. Basically, in my working directory
I have another folder called
communities that has the shapefile and the name of the shapefile is this. This is just from the website. This has a dot
SHP file extension, which is the file extension for shapefiles.
If I read that in, I now have a shapefile assigned to the variable chicago. If I plot
chicago, it will plot the shapefile.
So that's the shape file with the coordinate information
for all the 77 communities. And then if I just do points with my data table that's
spatialized and I say that the color is orange,
then that will produce the plot that I showed in the beginning of this video, which is the spatial
representation of all the gun violence incidents for the first six months of Chicago overlaid on the 77 communities.
This is just a start of the analysis of spatial data.
There's a lot more that you can do with this type of information.
Hopefully I'll make more videos on this in the future, but that's it for this video and thank you guys for watching.
*chiptune music*