Export a spatial object as KML with R

It can be useful sometimes to visualize your data using Google Earth. For example, if you want to explore the population density map of a city, you can project it on Google Earth to investigate in detail which neighbours are most populated. In this post, I’m going to show you how to export a spatial object in KML format with R.

Cartography with R

In a recent post I presented you how to build and export spatial objects with R. Today, I’ll show you how to add points to a map and how to manipulate spatial objects with R. Let’s consider a data frame tweets containing geographical coordinate of tweets posted in Spain in 2012. Each row of the data frame contains the longitude and latitude coordinate of a tweet.

Building spatial objects with R

I started to work with geolocalized data some time ago. I had already performed basic spatial analysis but it was my (real) first steps in the wonderful world of Geographical Information Systems (GIS). I first used QGIS which is a pretty great (free!) tool to work with spatial data. However, I needed a more flexible tool and I decided to start learning how to perform spatial analysis with R. In this post I’m going to show you how to build and export spatial objects with R using the sp package. This package provides classes and methods for dealing with spatial data such as points, lines or polygons. We will focus on the creation of a spatial polygon that we will then export in shapefile format.

Log-log plots in R

Plotting data on log-log scale can be helpful when your data cover a large range of values and/or to detect power law behaviors in your data. Create such a plot in R is quite simple, you just have to add log = “xy” as parameter when you call the plot function. In the same way, you can use log = “x” or log = “y” to plot only the x-axis or the y-axis on log scale.