Visualize the clusters

We will use a mapping library or mapping tool to plot the clusters on the map of Los Angeles.

Here it makes sense to define a color pallette (a list of colors) and to use the Cluster number to index over it.

If you open the HTML/PNG file from file explorer/finder, it should look something like this:

After creating the map:

🏴‍☠️:

You can create the map using Plotly (or Leaflet).

On the Plotly website, you will find a bunch of examples. Choose one that you like, but make sure you don't need tokens for it. Your map doesn't have to look exactly like the one shown above.

In order to get a similar figure, use the scattermapbox type with the open street map style.

Please note that the figure from above was made in Python and some things (like the marker) might not be as easily implementable in R. For the marker, you can just choose another color that is not already used by one of the clusters.

If you want a better parity to the Python example, try this task with Leaflet. :)

🐍:

To create a folium map use the folium.Map class.

For iterating through rows of a pandas dataframe use the iterrows() method and loop over its returned value.

To create circle markers use the folium.CircleMarker class and make sure to add it to the map using add_to(...). You can also use the folium.Marker class similarly for centroids.

To save the interactive map to an HTML file use the save("example.html") method of the folium map. Make sure the filename you choose ends with .html.

Last updated