In this tutorial, we will teach you how to visualize earthquakes in North America, from 1980 to 2017, on a map using Plotly.
First things first, we need some data to work with. You can head on over to this link and make the following modifications to the portal: Sort the data by “Magnitude, date,” select the North America and Hawaii region, and set the data range from 1980 to 2017 (Figures 1 and 2). All the other options should be left as default. Then click “Search Database.”
As you can see, we have the data but can’t download it. Ideally, we want to be able to download the data in a format that we can open with Excel or Google Sheets. But we will make do: Copy and paste all of the results and paste it on an new Excel workbook. Ensure that you have all of the data by cross-referencing with the link we copied it from. Save a copy of the workbook to refer back to if you need to or as a backup in case anything goes awry when cleaning the data.
To clean the data
Let’s start by organizing the top of our data first. Delete rows two through seven because we are just going to rename the column headers with a lone name, no subcategories. Refer to the original data and appropriately rename each column. To save you some time (unless you want to practice checking yourself) here is the appropriate header per column. You should have 26 columns.
Column A is Year
Column B is Month
Column C is Day
Column D is Hour
Column E is Minute
Column F is Second
Column G is Tsu
Column H is Vol
Column I is Additional Earthquake Information
Column J is Country Name/Location of the Earthquake
Column K is Latitude
Column L is Longitude
Column M is Focal Depth
Column N is Magnitude
Column O is Modified Mercalli Intensity
Column P Number of Deaths
Column Q is Death Description
Column R is Number of Injuries
Column S is Injury Descriptions
Column T is Damage in Millions of Dollars
Column U Damage Description
Column V is Number of Houses Destroyed
Column W is Description of Houses Destroyed
Column X is Number of Houses Destroyed
Column Y is Description of Houses Destroyed
Column Z is Photos
That is a lot more than we need for this tutorial but your data is looking better.
Now that we labeled our columns we can expand each one so that it also looks cleaner.
Check every couple of observations in each column by cross referencing with the original data because we copied and pasted it. This practice can be helpful even when you don’t copy and paste the data from somewhere — you want to ensure you have the data right before analyzing and visualizing it.
Visualizing your data
Now we are ready to upload it to Plotly.
Go to “New Chart” and when you get redirected to the new page, click on “Import” at the upper right-hand corner (Figure 5). Then upload the data file that we just cleaned. You can also directly copy and paste the data to the spreadsheet on Plotly. Your page should now look like Figure 6. Notice that our column headers didn’t upload as a header but as an individual row.
In order to be clear, we have to rename each column header by using row 1. When finished, delete row 1.
Now that it is all clean and ready to go, we can start visualizing.
Head on over to “Chart Type” and select Atlas Map. Plotly allows us to use coordinates to map data, which is perfect for mapping out the data we have right now.
Change the region to North America because that is the same as the data that we have. You should see your map be refocused (Figure 7).
We decided to make the Hover Text the year the earthquake occurred. Notice that Latitude and Longitude still appear when you hover over a dot. We will clean this later when we stylize our map.
We made the focal depth the variable for the size of our dots and the magnitude the variable for the color of our dots.
We also changed the projection to hammer view because we liked the way it looked. Use your discretion when stylizing your map. This is what our variable list looked like (Figure 8).
Stylize your map
Now, head on over to the “Style” section of Plotly and we can start changing the style of our map.
First go to “Traces” option of the “Style” section. Here you can change your display, color scale, your shape of the dot, the actual dot and such. This is what our map looked like (Figure 9).
To get rid of the longitude and latitude in your hover text, scroll toward the “values shown on hover” section, which is at bottom of the “Traces” option, and check “Longitude” and “Latitude” off so that you only have “Text” when you hover.
While we are stylizing, let’s also add an annotation so that it is more clear that the dot’s size indicates the earthquake’s focal depth (Figure 10).
We need to title our map. First go to “Layout” option in the “Style” section, and here we can change the title at “Title and Fonts” option for the typeface, word size and color. You should give an appropriate title. For example, if you see any trends in the chart, that can be the title. Also you can set your margins, hover interaction, geo style and geo layout here (Figure 11).
Add a source for your data by going to the “Notes” option under the “Layout.” Then click on “Annotation” at the upper left-hand corner and choose “source to data” to add the source for your data. Make sure that if you add a key or magnitude scale, you label it.
And voila, you have just mapped North American Earthquakes since 1980. Here is what our final product looked like. It is evident that there is a lot more earthquakes near the Pacific Ocean–in the United States and Mexico–than near the Atlantic Ocean. We can also note that they aren’t necessarily predictable and the earthquakes in the middle of the U.S. were not as strong as the coastal ones, despite medium-sized focal depths. You should save your map and data. You can export from Plotly to publish your map.