Skip to main content

How to Create Indonesia Map in R

Creating the Map

In this article, I will try to explain how to make Indonesia Map in R. I will assume that you are already familiar with the basic codes in R.

First, we need the required libraries :
require(maps)  #loading maps package
require(mapdata) #loading mapdata package
library(ggplot2) #ggplot2 package
library(readxl) #package for read .xlsx file
library(ggthemes) #package for ggplot2 theme
library(ggrepel) #extendig the plotting package ggplot2 for maps

Then, we prepare the data that contains the information of provinces name, latitude, and longitude of every province in Indonesia, e.g. :


You can download the data in here: Data

Now open the file and create the polygon:
setwd("your file's path") #set your own directory
mydata<- read_xlsx("dummy.xlsx") #assign the data to "mydata"

View(mydata) #view the data, notice the column of "latitude","longitude", "woe_label"
global <- map_data("world") #World longitude and latitude data 

View(global) #view the data and notice the column of long, lat, and group

gg1 <- ggplot() + 
  geom_polygon(data = global, aes(x=long, y = lat, group = group), 
               fill = "gray85", color = "gray80") + 
  coord_fixed(1.3) 
#you can change the "fill" (fill color) and the "color" (line color)

print(gg1) #show the plot


You will get the world map polygon:



What we want is Indonesia Map then we have to remove the other parts of the world map (in other words we will zoom the map) using xlim() and ylim() command. Choosing the right values for xlim() and ylim() is a trial and error process.
Indo<-gg1 + xlim(94,142) + ylim(-11,7.5)

#try to change the values of xlim and ylim and see what you get

print(Indo)     
We are doing a great job in generating map of Indonesia. Next, we can change the theme of the plot since we already call ggtheme package, e.g.
Indo<-gg1 + xlim(94,142) + ylim(-11,7.5) + theme_map()

#I am using command theme_map() to change the plot theme. You can check the other themes by reading more about ggtheme package 

print(Indo) 
#now, you can see the difference

We also need some points on the map that can help us distinguish the provinces. This step requires information of latitude and longitude of each province. The idea is we add another layer on top of the map.
Indo<-gg1 + xlim(94,142) + ylim(-11,7.5) + geom_point(data = mydata,aes(x = longitude, y = latitude), color = "purple", size=2, alpha = 0.5, show.legend = F) + theme_map()

#geom_point will add a layer that will show the point based on latitude and longitude of each province. Pay attention to the value inside aes(). 
#You can change the color, size, and alpha of the point. Alpha is the transparancy of the points.

print(Indo) 
#now, you can see the difference
At the final step, we want to add more information in the map which is the label of each province. Ggrepel package will help us to do that.
Indo<-gg1 + xlim(94,142) + ylim(-11,7.5) +
  geom_point(data = mydata, aes(x = longitude, y = latitude), 
  color = "purple", size=2, alpha = 0.5, show.legend = F) +
  geom_text_repel(data = mydata, aes(x = longitude, y = latitude, 
  label= woe_label), color = "grey30",show.legend=F, size=2.5) + 
  ggtitle ("Map of Indonesia") + theme_map()

print(Indo)

#pay attention for values in aes() in geom_text_repel(). We add label = woe_label which means the information of "label" will be taken from the column of woe_label.
#you can change the color and the size of the label.
#adding the title using ggtitle.

The final map is:
Code Summary:
require(maps)  #loading maps package
require(mapdata) #loading mapdata package
library(ggplot2) #ggplot2 package
library(readxl) #package for read .xlxs file
library(ggthemes) #package for ggplot2 theme
library(ggrepel) #extendig the plotting package ggplot2 for maps

setwd("your file's path") #set your own directory
mydata<- read_xlsx("dummy.xlsx") #assign the data to "mydata"

View(mydata) #"if you want to see data, notice the column of "latitude","longitude", "woe_label"

global <- map_data("world") #World longitude and latitude data 

View(global) #view the data and notice the column of long, lat, and group

gg1 <- ggplot() + 
  geom_polygon(data = global, aes(x=long, y = lat, group = group), 
               fill = "gray85", color = "gray80") + 
  coord_fixed(1.3) 
#you can change the "fill" (fill color) and the "color" (line color)

print(gg1) #show the plot

#map of indonesia
Indo<-gg1 + xlim(94,142) + ylim(-11,7.5) +
  geom_point(data = mydata,aes(x = longitude, y = latitude), 
  color = "purple", size=2, alpha = 0.5, show.legend = F) +
  geom_text_repel(data = mydata, aes(x = longitude, y = latitude, 
  label= woe_label), color = "grey30",show.legend=F, size=2.5) + 
  ggtitle ("Map of Indonesia") + theme_map()

print(Indo)


Data Visualisation


The next step is how we can put specific information into the map, for example if we want to compare the population, households, or the GDP contribution of each province. The idea is simple, we can use geom_point to visualise those data by defining the size of the points based on the specific data of each province (we can get official data from www.bps.go.id)
Indo<-gg1 + xlim(94,142)+11,7.5) +
  geom_point(data = mydata,aes(x = longitude, y = latitude, size = population), 
  color = "purple", alpha = 0.5, show.legend = F)

#notice now the function of "size = " is inside aes()
#You can change the value of size, for example size = population
#in this case, your data should have a column containing total of population for each province
#the size of each point will be differ and depend on the size of population
This map below is an example of simple data visualisation, it shows the total number of households (in thousands) of each province in Indonesia in 2015. Using this map, we can see clearly which province has the highest total number of households.



Comments

  1. mas, boleh minta upload lagi data latitude-longitude indonesia nya? link nya sudah tidak bisa diakses... terima kasih sebelumnya

    ReplyDelete

Post a Comment

Popular posts from this blog

Empirical Evidence of Engel’s Law Among Social Grant Recievers

Engel's law is an observation in economics stating that as income increases, the proportion of income spent on food decreases, even if absolute expenditure on food increases. The law was named after the statistician  Ernst Engel (1821–1896). One application of this statistic is treating it as a reflection of the living standard of a country. As this proportion — or "Engel coefficient" — increases, the country is by nature poorer; conversely a low Engel coefficient indicates a higher standard of living. Engel's Law image source: Wikipedia Using data collected through National Social and Economic Survey (NSES) by BPS-Statistics Indonesia, I tried to examine the existence of Engel's Law among households that received social grants in West Papua-Indonesia. Some studies found that giving additional money to the low-income households resulted in an increase in overall expenditure on food (on absolute) but the proportion  of income spent on food would decrea

What Can We Learn from Greek Debt Dramas?

Greek Debt Dramas Before the Global Financial Crisis (GFC) in 2008, the Greek had positive economic growth and it was considered high among countries in eurozone. Average economic growth reached almost four per cent between 1999 and 2007. Then the crisis hit in 2007 where housing bubble burst and made the subprime mortgage market in the United State collapsed. The crisis in the U.S. created a chain reaction which causing global banking crisis and credit crunch that lasts through 2009. The crisis made Lehman Brothers, big financial company, collapsed and the government in the United States and Europe prepared to bail out their banks. Greece failed to pay their huge debt since borrowing costs rose and financing dried up.  The financial crisis affected the Greek economy by reducing financial liquidity and business activity. Greece had been fortunate enough to face the crisis with the euro instead of its national currency, if they were using their national currency the crisis would