Introduction to plotting maps with R

Introduction to plotting maps with R

I've been looking at DataScience lately and have come to appreciate R for working with data. Much has been developed around the platform making it easy, even for the unexperienced, to collect and visualise data.

In example, we all know that earthquakes cannot be predicted in time. But if we look at historic data we can easily determine which regions are more seismic than others. So I wanted to give it a try. I spawned my RStudio installation and tried something out.

Getting the data

First thing we need to do is get some data. The Istituto Nazionale di Geofisica e Vulcanologia (INGV) has a neat site where you can access lots of data. So that will be the source for my work.

# Get data from INGV

today <- as.character(Sys.Date())

thecall <- paste("curl 'http://webservices.ingv.it/fdsnws/event/1/query?starttime=2005-04-01T00%3A00%3A00&endtime=",today,"T23%3A59%3A59&minmag=2&maxmag=10&mindepth=-10&maxdepth=1000&minlat=35&maxlat=49&minlon=5&maxlon=20&minversion=100&orderby=time-asc&format=text&limit=10000' -o ./ingv.csv", sep = "")

system(thecall)

# Create data frame
ingv <- read.csv("./ingv.csv", sep="|")

What I'm doing here is getting the current date and creating a call to invoke the INGV web service so that i t can return a text file containing all events starting from a certain date in Italy. That text file is saved in my working directory as ./ingv.csv

INGV returns the values separated by a pipe character so I need to specify it in the csv reading function.

The data is already tidy and in the format that I need for now so I won't run other commands in the data frame in order to manipulate it.

Quick plot with rworldmap

First way of plotting the data would be by using the rworldmap package. I'll use that to create a png file for my plot that will map all events occurred in the region.

# create a with rworldmap to display events from INGV
 
therwmap <- getMap(resolution = "low")

png("./therwmap.png")

plot(therwmap, xlim = range(ingv$Longitude), ylim = range(ingv$Latitude))

points(ingv$Longitude,ingv$Latitude, col = "red", cex = .6)

dev.off()

If I were to plot the map as it get's returned from the getMap function I would plot the entire world. Since I have events confined to a region I specify the xlim and ylim parameters by getting the ranges directly form the data frame using the range function.

And here's my first map.

We can probably do better than that.

Nicer plot with ggmap and ggplot2

The ggplot2 package [1] is a more powerful tool that we can use in order to create plots and visualise data in R. what I want to achieve is to have bigger circles for higher magnitude of the events and also find a way of making the plot more readable when many events occur in the same region.

# create a map with ggmap to display events got from INGV

bbox <- make_bbox(range(ingv$Longitude),range(ingv$Latitude), f = 0.05)

theggmap <- get_map(bbox)

jpeg("./theggmap.jpg", width = 1024, height = 1024)

theggmapplot <- ggmap(theggmap) + geom_point(aes(x = Longitude, y = Latitude, size = Magnitude), data = ingv, alpha = .5)

theggmapplot

dev.off()

Again, I'm creating a bounding box to pass the the get_map function and then I'm adding the points to the plot specifying their size and alpha values. So that the end result is this.

This looks better but can we make this interactive?

Interaction with mapview

Of course we can. We can use the map view package. This tools give us the possibility of exploring geo spatial data on an interactive map.

#create an inteactive map to explore the ingv data frame

coordinates(ingv) <- ~ Longitude + Latitude

proj4string(ingv) <- "+init=epsg:4326"

mapview(ingv, cex = "Magnitude")

What we need to do here is to set the spatial coordinates to create a Spatial object with he coordinates function. We then define the reference system for rasterising the map with the proj4string function. Finally we create out interactive plot specifying different sizes for different magnitudes by setting the cex parameter. And here's what we get.

Conclusion

Looking at data plotted in graph is a powerful way of understanding and learning.

Download files used for this article here

__________________________

[1] D. Kahle and H. Wickham. ggmap: Spatial Visualization with ggplot2. The R Journal, 5(1), 144-161. URL http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf

To view or add a comment, sign in

More articles by Paolo Bianchini

Others also viewed

Explore content categories