ggplot2: Making a Beautiful Map in R!

jwalkeratheart
4 min readDec 8, 2018

--

In my class INFO 201: Technical Foundations this quarter, I have been learning to code in R through project-based assignments.

In my first team project of the year, we collaborated on making an interactive report in RMarkdown — part of which that looked something like this:

Above is our frequency map of UFO sightings for the dataset that we used!

Using the data on UFO sighting reports, we were able to produce visualizations of the data that were embedded within the Markdown document.

Here is a second visualization that we did, that shows the most common UFO sighting shapes on a bar graph.

Today, in this tutorial, I will give a brief overview of how I created the map that you saw above!

To begin, I used the ggplot2 package in R, which allows for clean data visualizations.

Make sure to source this library at the beginning of your code, as shown below, to ensure that it will be used.

Once you have your data filtered the way you want it, all you have to do it start with a simple line of code:

This essentially creates a blank canvas for whatever you would like to plot!

Next, to get the plot looking more like a map, we’re going to use the handy built-in R function of map_data(), which returns a data frame containing information for the map you’d like. In this case, we’ll be using one of the United States!

So, how do we get the map data onto the ggplot?

This can be done by using this state data to fill in a polygon shape on our plot!

Make sure to use the “+” operator to add new layers to your ggplot.

While this may seem overwhelming, let’s break down what each variable means.

data = states

  • This is telling ggplot to use our state map data as a base.

x = long, y =lat

  • This tells ggplot to use the United States longitude and latitude as mapping points.

fill = region, group = group →

  • This makes each region — in this case, each state of the U.S. — individually filled.

color = “white”, fill= “grey” →

  • Aesthetic choices that make the boundaries of the states white, and the overall fill color of the map grey.

This code results in this beautiful map of the United States!

The map we just made!

Now, how can we put points on this map?

We’ve made a beautiful U.S. map, but the next step is to place some points based on where UFO sightings were reported in our data.

Luckily, the handy function geom_point() allows us to place points on our ggplot.

data = ufo_data_continental_usa →

  • This tells ggplot to use our filtered UFO data for this plotting.

mapping = aes (x = lng, y = lat) →

  • Once again, we want our x values to be longitude, and y values to be latitude so that it appears like a map!

color = “green”, size = 1 →

  • This makes the points size 1 and in the color green.

This rather simple line of code is just placing points on the latitudes and longitudes (x axis points and y axis points) that are already recorded in the data!

By adding this on to our previous ggplot code (again, don’t forget to use the “+” operator), we get this!

A great map of the U.S. with UFO sighting points.
The final code for the map we just made.

Aesthetic Changes

Now that you know how to create a U.S. map and plot points on it in ggplot, you can play around with different options and data sets!

For example, by changing “color = purple” and “size = 5” in our geom_point layer, we can change the look of the map:

The map has a whole new look!

Good luck on your R and ggplot journeys, and remember to explore and have fun with your project!

So many possibilities — just have fun with it!

--

--