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
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
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.
mas, boleh minta upload lagi data latitude-longitude indonesia nya? link nya sudah tidak bisa diakses... terima kasih sebelumnya
ReplyDelete