Hey guys, in this video I'm going to do another video on spatial data analysis and in particular,
I'm going to go over how you can create choropleth maps using a package called leaflet in R. I have
all this code already up on an Rpubs site and I'll put it in the description box below. And
basically what I'm going to go over is the R code to create a map like this. This is an example of
a choropleth map. Choropleth map is a spatial map that's broken down according to certain
boundaries. In this case, my map is of the city of Chicago and it's broken down into community
areas. There are 77 different community areas in Chicago. I did my previous two spatial data
analysis videos on the same dataset. This is a continuation of those videos. And basically
what you can do is you can color the different regions of your map based on a statistic. For my
particular dataset that I loaded in and created a map of, I have the number of gun violence
incidents in the city of Chicago broken down by community. And then I have for each community,
the number of incidents for the first six months of 2019. This is for example one community and
it had 77 gun violence incidents. And then if I hover over the different communities,
I see that I have different numbers for each different community. And the number corresponds to
the color bar on the right. And this is therefore an interactive map. I can zoom in and I can zoom
out. And it also has an interactive feature of I can put text every time I hover over a different
community and it will give me a different value. That's something that I coded into the map. This
is done using a JavaScript library called leaflet and you can also load that in as an R package and
basically run it in R and it produces this map, which can be embedded into an HTML web page. I'm
going to go over how you can do that in this video. Let's go ahead and get started. I have
it in Rpubs and I also have it in R but I think I'll just go over it in the Rpubs since I already
have it up. I'm going to use these libraries. The most important one is basically leaflet,
which will actually produce the choropleth maps. And then this other stuff is just for working with
data and then working with data tables. This next line reads in my data. Again I'm just continuing
using an analysis that I did for two previous videos, which is gun violence incidences in the
city of Chicago. And I went over in a previous video how you spatialize this data. This is kind
of what I'm doing all over again. Basically, I read in the data, I convert it to a data table,
I clean up the data a little bit to make sure the data points are all in the city of Chicago,
and then I spatialize the data by basically converting each address into a latitude and
longitude coordinate. That's just doing these steps here. And then when I plot my spatialized
data, each data point is now spatialized according to a latitude and longitude coordinate. These are
a couple hundred data points of gun violence incidences and the locations are different
locations in the city of Chicago. This was the same as in previous videos. And then next
I'm going to read in the shapefile, which is the shapefile of the city of Chicago. This is
publicly available. Most major cities will have publicly available shapefiles and basically what
that does is it gives me the spatial information of the city of Chicago and also it breaks down the
city of Chicago by communities. That's why I have been using this example over and over again. And
I got it from--I think it's called the Chicago data portal or something but it's just publicly
available data. And the shapefile basically gives me all the community areas, so the black outline.
And these community areas are basically called polygons, which is a term used in shapefiles,
and that means it gives you the correct latitude and longitude coordinates of the boundaries and
the insides of the different polygons, which are the communities in this case. Having the
shapefile allows us to produce basically the background of this map, so the city of
Chicago and then the boundaries which are the black outlines. Then basically what I'm going
to do is I'm going to intersect the points onto the polygons of the shapefile. And to do that,
you have to have the same coordinate reference system for your shapefile and for the spatial
points. In this line, I'm manually forcing the two coordinate reference systems to be the same.
Usually you don't want to manually force that, so it produces an error, but I know that they're the
same coordinate reference system so I just ignored that warning and I moved on. Then this next line
of code is where I actually start working on data manipulation in order to produce the
choropleth maps. What I actually want is, from the original data table that has all the gun violence
incidences, I want to bin by community area to get the numbers that I'm going to put in each
community area. I want to basically aggregate that across communities. I'm going to do that
using the aggregate function. This says aggregate by the column incidents underscore people in my
data table and then I'm going to aggregate by Chicago, which is the name of my shapefile,
and that's going to separate or aggregate by communities, which are the polygons in the
shape file. And the aggregation is just the length of the number of incidences. For each
gun violence incident, that's going to correspond to a specific community and then I'm just saying,
count the number of incidences for each community area. And that aggregation is what's going to be
plotted and that's the number that I get when I hover over each community area. Then this next
line of code is working with the coloring of the choropleth map. I'm going to say to do a red map.
The map is going to be variations or different shades of red. You can put reds or purples or
different colors in this spot. And then I'm going to say my data that I'm actually going to plot is
going to be the incidence people and then I'm saying to have four bins, which is the number
of bins that you have in your color bar. Then this next line of code will actually plot the
choropleth map using leaflet. The only command that you need is actually the beginning part.
The second part of the code is just setting up customizations for what you want the plot to look
like. It's like optional parameters that you can also set. But the only command that you need is
leaflet and then you're passing in the spatialized and aggregated data. And then after that, I'm just
setting some optional parameters. I'm setting the opacity. I'm saying use the colorbar that we set
above. I'm saying to use the fill color as reds and the number of bins that I specified above. And
then this last line is just adding some optional parameters for the legend. I'm saying to pass in
the number of people, or the number of incidences, gun violence incidences, and the title is going to
be "Incidences". And that produces a map like this. I still don't have the hover over functionality
added yet. I'm going to add that in the next step. And also what I noticed was with my data set that
I passed in chi underscore ag, I had some missing data. I told it to only plot four bins but it also
is missing data, so it adds a fifth bin of N/A or not available. And what I noticed was basically
the data table didn't include values that were zero in my original data table. Communities that
had zero gun violence incidences were missing values. The zero was missing. Basically these
values should have all been zero but they just didn't exist in the data table. To fix that,
I could have either gone back to the data table and just added those communities and then added
the values zero or I could have--and what I ended up doing--was I just manually added zero to these
communities. And that's basically what I did in the next line of code. I manually found these
seven communities that had missing values, which were the grayed out areas in the previous map.
And I found their corresponding indices and then I just set those values manually to be equal to
zero. Then I basically worked on creating the final choropleth map again. Instead of having
four bins, another way you can bin the data is by setting an actual vector to the intervals that
you want. I said, I don't want it to be so spread out that it goes from 0 to 20. I want it to spread
out by values of 10. I just entered a vector that had 0, 10, 20, and then intervals of 10,
basically. And that's another way of setting the number of bins. You can either set the number to
be a specific number or you can pass through a vector, either way is fine. Then I just kept
the same colorbar information. This line of code adds the labels, which is the interactive part
that will appear when you want to hover over the different communities and have them pass through a
specific string. I called that particular variable "labels", and basically what I'm passing through
is a string. And particularly the string is the number of incidences per community. And that's
going to get added into the optional parameters later on. I set that first. Then again I run
leaflet and then I'm going to add this additional stuff that has information for the labels that
we're going to get when we hover over. I set label equal to labels and then there are a couple
label options that you can set as well, such as the font, font size, so I said 15 point font,
and the padding on the actual boxes, the white boxes that accompany the text, you can set that
as well. And that was the only difference. This is the same as before. And then after I
added the missing zeros and the additional hover functionality, I get the final map which looks
like this. Now these values have been correctly added as 0 and you see you have the additional
functionality of when you hover over the different polygons or the different communities,
you can add any text that you want. And that's it for this video and thank you guys for watching!
*chiptune music*