GGMap + GGPlot

We already know how to download part of an background map from Google Maps. Today we will plot our data on the downloaded map using the ggplot2 library. Let’s load the libraries we need:

In our post, we’ll use railroad lines, which we’ve used many times before. Let’s load the layer:

To display the vector layer in ggplot, it is necessary to transform it into a data frame using the fortify function:

In the variable map we write the map we want to display:

The individual map elements are:

ggplot() – initializes the object, we declare in it the input data (railroad_df) and coordinates and grouping variable aes()

geom_path() – a way to plot the data as lines according to their entry in the input table. Here we also define the color and size of the lines

ggtitle() – the name of the table

Display the result:

Let’s now add the ggmap underlay to our railway lines. First we need to convert the vector layer to a geographic layout:

We download a Google satellite map fragment for the vector layer:

We display the underlay:

We transform the layer into a data frame:

We store the map in a different way than before. The base is sat_map, on which we overlay geom_path()

Display the result:

Ggplot is a very rich library for visualizing different types of data. It has something to offer for everyone. In this post we have shown how to use it simply to visualize shp levels on Google Maps, but we will certainly show you how to use other features of this library in the future.

Leave a Reply

Your email address will not be published.

Translate using Google Translate»
Social media & sharing icons powered by UltimatelySocial

Podoba Ci się nasza strona? Odwiedź nasz profil