R Programming Series: Create Dynamic Map Using ggmap & R

R Programming

R is a well-known and popular programming language that is heavily used for statistical computing and graphics by the researchers and statisticians from all over the world. R programming is mainly seen in data science where statisticians & data miners use it for drawing insights from the given dataset.

In recent years, despite the popularity of Java or Python, R is still used by a good number of people from all over the world. It is mainly because of the fact that ‘R language’ was solely designed for statisticians. R programming has immense scope and is important for programmers in data science.

Considering this, we are bringing this exclusive series that will teach you various aspects of Data Science using various tools of R programming language. Below are the different concepts that you will be going to learn with this series.

So, let’s begin!

As mentioned earlier, we will first start with creating dynamic maps with the help of R and related packages. We will focus on a dataset that helps in analyzing the range of votes to be given to the mentioned geographical region.

We will implement the following steps to create a dynamic map from the mentioned dataset, where we will implement the necessary packages which are needed for creating the map.

Step 1: Install the necessary packages which are needed for creating the dynamic map in R. Include the packages in the mentioned workspace.

Step 2: Create a dataset from the maps package using a specific function that helps in the creation of a data frame suitable for plotting with ggplot2.

Step 3: Now let us understand the structure of the data frame namely “states_map” which includes all the necessary attributes.

data frame structure- states_map

We have 15,537 observations or records with 6 columns mentioned in it. The dataset also includes the combination of latitude and longitude which helps in catering the required values while plotting a particular plot. The map_data() function returns a data frame with the following columns: 

long – Longitude

lat – Latitude

group – This is a grouping variable for each polygon

A region or subregion might have multiple polygons, for example, if it includes islands.

Step 4: Let us plot the geographical regions from the mentioned set of coordinates of latitudes and longitudes.

US Coordinates

We refine the attributes to have a proper visualization and the output for that is given below:

Step 5: Now let us create the map with regions that are colored according to the mentioned values.

In this step, we are merging two data sets into one to understand the voting rate of the population of the US. If you observe there is a parameter called “ObamaVote” which defines the rate of votes given by people to Obama.

Step 6: Let us create the vote chart of the 2012 elections with the mentioned rate card.

map with regions that are colored

The plot defines the range of votes which is shared among the population of the mentioned regions.

Step 7: Let us create a world map with the associated coordinates to create the world data records.

Step 8: Select the regions of Europe. We need to create a subset of countries of Europe.

Step 9: Now let us create the geographical plots which define each region with a specific color.

geographical plots which define each region with specific color


Sometimes, we want to know the trends and behaviors of people in different countries or states. For example, we might want to see the shopping behaviors of people in different states. The maps package is useful for this purpose. In this section, we will look at how to draw and display information with maps. We saw various strategies through which we can plot dynamic maps using ggmap and maps packages which are included in R.

In the next section, we will explore various ways of creating dynamic maps using ggplot2 in R language. These maps will include different types of scatter plots, jitter plot, bar plot, histogram, density plot, box plot, dot plot, violin plot, bubble plot & others.

Read More:


Please enter your comment!
Please enter your name here