Visualizing the Magnitude of Earthquakes in New Zealand

I wanted to test out a few packages in R and explore some data visualization techniques so I decided to write this script. The power and versatility of ggplot2 continually amazes me.

“datasets” gives me access to the ‘quakes’ dataset which gives the locations and magnitudes of 1000 seismic events near Fiji. Since the ‘maps’ package does not include a map of Fiji, I used a map of New Zealand to display the locations.

I called a map of New Zealand and then plotted the latitudes and longitudes of the seismic events. The ‘maps’ package is very easy to use and has plenty of helpful documentation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
library(ggplot2)
library(datasets)
library(maps)
library(mapproj)

data(quakes)

quakes <- quakes[,-3]
quakes <- quakes[,-4]

nz <- map_data("world", region="New Zealand")
nz <- subset(nz, long > 100)
qplot(long, lat, data=nz, geom="path", group=group) + coord_map()

ggplot() + geom_path(aes(x = long, y = lat, group=group), data=nz) +
  geom_point(data=quakes, aes(x = long, y = lat, color=mag, size=mag,
          alpha=mag)) + coord_map() + opts(title="Magnitude and 
               Location of Earthquakes in New Zealand")

ggplot2 really shines here since I can scale the size, opaqueness, and color of the location points based on magnitude with a few simple commands.