Visualize Fiji Earthquakes with Map and Scatterplots in ggplot2
In this work, we’ll create a map of the Oceania and Fiji islands region, and use scatterplot to illustrate the earthquake sites and relative activities in Fiji. Major techniques covered in this work include:
library(ggplot2)library(dplyr) w <-map_data("world") %>%as_tibble()head(w, 4)
Output:
# A tibble: 4 × 6 long lat group order region subregion <dbl> <dbl> <dbl> <int> <chr> <chr> 1 -69.9 12.5 1 1 Aruba <NA> 2 -69.9 12.4 1 2 Aruba <NA> 3 -69.9 12.4 1 3 Aruba <NA> 4 -70.0 12.5 1 4 Aruba <NA>
Create a world map, with zoom-in over the Australia - Fiji region.
p1 <- w %>%ggplot(aes(long, lat)) +geom_polygon(aes(group = group), fill ="yellow1", color ="yellow4", alpha = .1) +coord_fixed(ratio =1.1, xlim =c(130,192),ylim =c(-50, 0))p1
In addition to coord_fixed(), we can use coord_map() to better preserve the shape of geographical features. This function though takes longer time to render the map.
# The package `mapproj` is required for `coord_map()`# install.packages("mapproj")p1 +coord_map( xlim =c(130,192),ylim =c(-50, 0))
Highlight the Fiji islands, with text and arrow annotation.
p2 <- p1 +# redraw the Fiji islands in the map with blue highlightgeom_polygon(data = w %>%filter(region =="Fiji"), aes(group = group), fill ="blue") +# add text annotationannotate(geom ="text", x =179, y =-7, label ="Fiji",size =8, fontface ="bold", color ="blue") +# add arrowgeom_segment(aes(x =179, xend =179, y =-11, yend =-15),arrow =arrow(type ="closed", length =unit(6, "pt") )) p2
Add the earthquake data using the R built-in dataset quakes. Both datasets w and quakes have the long and lat variables, which are mapped to the axial aesthetics.
library(ggplot2)library(dplyr)w <-map_data("world") # Create a world map, with zoom-in over the Australia - Fiji regionp1 <- w %>%ggplot(aes(long, lat)) +geom_polygon(aes(group = group), fill ="yellow1", color ="yellow4", alpha = .1) +coord_fixed(ratio =1.1, xlim =c(130,192),ylim =c(-50, 0))p1 # Alternatively, we can use `coord_map` to determine the aspect ratio. # This function though takes longer time to render the map. # The package `mapproj` is required for `coord_map()`# install.packages("mapproj")p1 +coord_map( xlim =c(130,192), ylim =c(-50, 0)) # Highlight the Fiji islands, with text and arrow annotation.p2 <- p1 +# redraw the Fiji islands in the map with blue highlightgeom_polygon(data = w %>%filter(region =="Fiji"), aes(group = group), fill ="blue") +# add text annotationannotate(geom ="text", x =179, y =-7, label ="Fiji",size =8, fontface ="bold", color ="blue") +# add arrowgeom_segment(aes(x =179, xend =179, y =-11, yend =-15),arrow =arrow(type ="closed", length =unit(6, "pt") )) p2 # Add the earthquake data using the R built-in dataset `quakes`. # The `long` and `lat` variables in both datasets are used in the aesthetic mapping. p3 <- p2 +geom_point(data = quakes, alpha = .05, size =3, color ="tomato") p3 # Add text annotation to Australia and New Zealand, and add plot title. p4 <- p3 +annotate(geom ="text", x =c(138, 180), y =c(-25, -45), label =c("Australia", "New\nZealand"),size =7, fontface ="bold", color ="yellow4", alpha = .4) +labs(x =NULL, y =NULL, # remove axis titlestitle ="Earthquakes in Fiji since 1964") p4 # A brief touch on the theme.p5 <- p4 +theme_void() +theme(plot.title = element_text (hjust = .9, vjust =-3, size =15, face ="bold.italic")) p5
Continue Exploring — 🚀 one level up!
The above graphic combines a geographic map with a scatterplot to illustrate earthquake occurrences. The same technique can be readily extended to visualize the distribution of cities of different population sizes.
In addition to scatterplots, a map is often integrated with line plots to create powerful data visualizations. Check out the following article that visualizes global flights and airports as illustrated below. And also check how to turn static flight lines into animation.