<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>spatial | Tengku Hanis</title>
    <link>https://tengkuhanis.netlify.app/tag/spatial/</link>
      <atom:link href="https://tengkuhanis.netlify.app/tag/spatial/index.xml" rel="self" type="application/rss+xml" />
    <description>spatial</description>
    <generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><copyright>©Tengku Hanis 2020-2025 Made with [blogdown](https://github.com/rstudio/blogdown)</copyright><lastBuildDate>Wed, 22 Feb 2023 00:00:00 +0000</lastBuildDate>
    <image>
      <url>https://tengkuhanis.netlify.app/images/icon_hua2ec155b4296a9c9791d015323e16eb5_11927_512x512_fill_lanczos_center_2.png</url>
      <title>spatial</title>
      <link>https://tengkuhanis.netlify.app/tag/spatial/</link>
    </image>
    
    <item>
      <title>Mapping the states in Malaysia</title>
      <link>https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/</link>
      <pubDate>Wed, 22 Feb 2023 00:00:00 +0000</pubDate>
      <guid>https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/</guid>
      <description>


&lt;p&gt;I have written two blog posts about making map in R:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;a href=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/&#34;&gt;Making maps with R (my first attempt ever!)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/&#34;&gt;My first interactive map with {leaflet}&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This post is sort of a continuation to the &lt;a href=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/&#34;&gt;first blog post&lt;/a&gt;. I have shown how to plot a coordinate to a map in that post specifically for Malaysia.&lt;/p&gt;
&lt;p&gt;However, using the two approaches in the previous blog post, we cannot plot the coordinate to a certain states in Malaysia. At least I am not unable to find how to do that after googling around. But, we can plot the borneo or peninsular of Malaysia using the two approaches.&lt;/p&gt;
&lt;div id=&#34;plot-the-peninsular-of-malaysia-not-the-best-way&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Plot the peninsular of Malaysia (not the best way)&lt;/h2&gt;
&lt;p&gt;Load the necessary packages.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(rworldmap) 
library(tidyverse)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, we get the data. The data is about desa clinic (klinik desa) in Malaysia.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;clinicDesa &amp;lt;- read.csv(&amp;quot;https://raw.githubusercontent.com/tengku-hanis/clinic-data/main/clinicdesa.csv&amp;quot;)
head(clinicDesa)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   id facilities_id                     name              address postcode
## 1  1    KD01010019  KLINIK DESA ASSAM BUBOK     Jalan Batu Pahat    86400
## 2  2    KD01010020   KLINIK DESA BATU PUTIH    Jalan Behor Temak    83000
## 3  3    KD01010021      KLINIK DESA BEROLEH    Jalan Parit Besar    83300
## 4  4    KD01010022        KLINIK DESA BINDU Jalan Tongkang Pecah    83010
## 5  5    KD01010023 KLINIK DESA KAMPUNG BARU   Jalan Parit Kemang    83710
## 6  6    KD01010024 KLINIK DESA KANGKAR BARU      Jalan Meng Seng    85400
##             city   district  state tel fax website email image latitude
## 1     Ayer Hitam Batu Pahat Johor       NA      NA    NA    NA 1.933330
## 2          Bagan Batu Pahat Johor       NA      NA    NA    NA 1.889100
## 3     Sri Gading Batu Pahat Johor       NA      NA    NA    NA 1.877890
## 4 Tongkang Pecah Batu Pahat Johor       NA      NA    NA    NA 1.901515
## 5    Parit Yaani Batu Pahat Johor       NA      NA    NA    NA 1.905120
## 6      Yong Peng Batu Pahat Johor       NA      NA    NA    NA 2.065310
##   longitude likes rating status
## 1  103.1167     0      0    NEW
## 2  102.8778     0      0    NEW
## 3  102.9858     0      0    NEW
## 4  102.9665     0      0    NEW
## 5  103.0372     0      0    NEW
## 6  103.1248     0      0    NEW&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First we plot the data.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(clinicDesa, aes(longitude, latitude)) +
  geom_point() +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-3-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Remove the two points.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;clinicDesa2 &amp;lt;- clinicDesa %&amp;gt;% filter(longitude &amp;gt; 25)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, plot the updated data.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(clinicDesa2, aes(longitude, latitude)) +
  geom_point() +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-5-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;From the plot, we already know the left side consists of the coordinates in the peninsular of Malaysia. So, we can limit our plot by limit the longitude &amp;lt; 105 and longitude &amp;gt; 97.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Get base map
global &amp;lt;- map_data(&amp;quot;world&amp;quot;) 

# Plot
ggplot() + 
  geom_polygon(data = global %&amp;gt;% filter(region == &amp;quot;Malaysia&amp;quot;), aes(x=long, y = lat, group = group), 
               fill = &amp;quot;gray85&amp;quot;) + 
  coord_fixed(1.3) +
  geom_point(data = clinicDesa2, aes(x = longitude, y = latitude)) +
  theme_minimal() + 
  xlab(&amp;quot;Longitude&amp;quot;) +
  ylab(&amp;quot;Latitude&amp;quot;) +
  labs(title = &amp;quot;Desa clinic in the peninsular of Malaysia&amp;quot;, 
       subtitle = &amp;quot;(Data last updated: Klinik Desa - 9 Mac 2021)&amp;quot;,
       caption = expression(paste(italic(&amp;quot;Sumber data: https://www.data.gov.my/data/ms_MY/group/pemetaan&amp;quot;)))) +
  xlim(97, 105) #limit overall map to peninsular of Malaysia&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-6-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;I am not going to re-explain the above and below codes as I have explain it in &lt;a href=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/&#34;&gt;the previous blog post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This approach also works with &lt;code&gt;rworldmap&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Get base map
world &amp;lt;- getMap(resolution = &amp;quot;low&amp;quot;)
msia &amp;lt;- world[world@data$ADMIN == &amp;quot;Malaysia&amp;quot;, ]

# Plot
ggplot() +
  geom_polygon(data = msia, aes(x = long, y = lat, group = group), fill = NA, colour = &amp;quot;black&amp;quot;) +
  geom_point(data = clinicDesa2, aes(x = longitude, y = latitude)) +
  coord_quickmap() + 
  theme_minimal() + 
  xlab(&amp;quot;Longitude&amp;quot;) +
  ylab(&amp;quot;Latitude&amp;quot;) +
  labs(title = &amp;quot;Desa clinic in the peninsular of Malaysia&amp;quot;, 
       subtitle = &amp;quot;(Data last updated: Klinik Desa - 9 Mac 2021)&amp;quot;,
       caption = expression(paste(italic(&amp;quot;Sumber data: https://www.data.gov.my/data/ms_MY/group/pemetaan&amp;quot;)))) +
  xlim(97, 105) #limit overall map to peninsular of Malaysia&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-7-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;As we can see using the two approaches, we can plot the borne and peninsular sides of Malaysia. But, at least to my knowledge we cannot apply this approach if we want to plot a coordinate to certain states in Malaysia.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;plot-the-states-in-malaysia&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Plot the states in Malaysia&lt;/h2&gt;
&lt;p&gt;Load the necessary package.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(geodata)
library(tidyterra)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see from the package, we going to use a &lt;code&gt;geodata&lt;/code&gt; package. &lt;code&gt;tidyterra&lt;/code&gt; is used to supplements the ggplot. First, let’s limit the data into desa clinics in Terengganu only.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;clinic_trg &amp;lt;- 
  clinicDesa %&amp;gt;% 
  filter(state == &amp;quot;Terengganu&amp;quot;) %&amp;gt;% 
  dplyr::select(latitude, longitude) 
head(clinic_trg)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   latitude longitude
## 1  5.48533  102.4914
## 2  5.81578  102.5778
## 3  5.70886  102.4892
## 4  5.75722  102.5303
## 5  5.67444  102.6289
## 6  5.69875  102.5430&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we get the map from the &lt;code&gt;geodata&lt;/code&gt; package with the boundaries at the district level.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;Malaysia &amp;lt;- gadm(country = &amp;quot;MYS&amp;quot;, level = 2, path=tempdir())&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can use the below information to limit the map to Terengganu state only.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;Malaysia$NAME_1&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   [1] &amp;quot;Johor&amp;quot;           &amp;quot;Johor&amp;quot;           &amp;quot;Johor&amp;quot;           &amp;quot;Johor&amp;quot;          
##   [5] &amp;quot;Johor&amp;quot;           &amp;quot;Johor&amp;quot;           &amp;quot;Johor&amp;quot;           &amp;quot;Johor&amp;quot;          
##   [9] &amp;quot;Johor&amp;quot;           &amp;quot;Johor&amp;quot;           &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;          
##  [13] &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;          
##  [17] &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;          
##  [21] &amp;quot;Kedah&amp;quot;           &amp;quot;Kedah&amp;quot;           &amp;quot;Kelantan&amp;quot;        &amp;quot;Kelantan&amp;quot;       
##  [25] &amp;quot;Kelantan&amp;quot;        &amp;quot;Kelantan&amp;quot;        &amp;quot;Kelantan&amp;quot;        &amp;quot;Kelantan&amp;quot;       
##  [29] &amp;quot;Kelantan&amp;quot;        &amp;quot;Kelantan&amp;quot;        &amp;quot;Kelantan&amp;quot;        &amp;quot;Kelantan&amp;quot;       
##  [33] &amp;quot;Kuala Lumpur&amp;quot;    &amp;quot;Labuan&amp;quot;          &amp;quot;Melaka&amp;quot;          &amp;quot;Melaka&amp;quot;         
##  [37] &amp;quot;Melaka&amp;quot;          &amp;quot;Negeri Sembilan&amp;quot; &amp;quot;Negeri Sembilan&amp;quot; &amp;quot;Negeri Sembilan&amp;quot;
##  [41] &amp;quot;Negeri Sembilan&amp;quot; &amp;quot;Negeri Sembilan&amp;quot; &amp;quot;Negeri Sembilan&amp;quot; &amp;quot;Negeri Sembilan&amp;quot;
##  [45] &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;         
##  [49] &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;         
##  [53] &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;          &amp;quot;Pahang&amp;quot;          &amp;quot;Perak&amp;quot;          
##  [57] &amp;quot;Perak&amp;quot;           &amp;quot;Perak&amp;quot;           &amp;quot;Perak&amp;quot;           &amp;quot;Perak&amp;quot;          
##  [61] &amp;quot;Perak&amp;quot;           &amp;quot;Perak&amp;quot;           &amp;quot;Perak&amp;quot;           &amp;quot;Perak&amp;quot;          
##  [65] &amp;quot;Perak&amp;quot;           &amp;quot;Perlis&amp;quot;          &amp;quot;Pulau Pinang&amp;quot;    &amp;quot;Pulau Pinang&amp;quot;   
##  [69] &amp;quot;Pulau Pinang&amp;quot;    &amp;quot;Pulau Pinang&amp;quot;    &amp;quot;Pulau Pinang&amp;quot;    &amp;quot;Putrajaya&amp;quot;      
##  [73] &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;          
##  [77] &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;          
##  [81] &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;          
##  [85] &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;          
##  [89] &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;          
##  [93] &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;           &amp;quot;Sabah&amp;quot;          
##  [97] &amp;quot;Sabah&amp;quot;           &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [101] &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [105] &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [109] &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [113] &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [117] &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [121] &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [125] &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;         &amp;quot;Sarawak&amp;quot;        
## [129] &amp;quot;Selangor&amp;quot;        &amp;quot;Selangor&amp;quot;        &amp;quot;Selangor&amp;quot;        &amp;quot;Selangor&amp;quot;       
## [133] &amp;quot;Selangor&amp;quot;        &amp;quot;Selangor&amp;quot;        &amp;quot;Selangor&amp;quot;        &amp;quot;Selangor&amp;quot;       
## [137] &amp;quot;Selangor&amp;quot;        &amp;quot;Trengganu&amp;quot;       &amp;quot;Trengganu&amp;quot;       &amp;quot;Trengganu&amp;quot;      
## [141] &amp;quot;Trengganu&amp;quot;       &amp;quot;Trengganu&amp;quot;       &amp;quot;Trengganu&amp;quot;       &amp;quot;Trengganu&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, this is the plot for Terengganu.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;Trg &amp;lt;- Malaysia[138:144,]
plot(Trg)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-12-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;We going to the map this in ggplot, and stacked the map layer with the coordinate layer.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot() +
  geom_spatvector(data = Trg, color = &amp;quot;grey&amp;quot;, fill = NA) +
  geom_point(data = clinic_trg, aes(x = longitude, y = latitude, color = &amp;quot;red&amp;quot;)) +
  theme_minimal() +
  theme(legend.position = &amp;quot;none&amp;quot;) +
  xlab(&amp;quot;Longitude&amp;quot;) +
  ylab(&amp;quot;Latitude&amp;quot;) +
  labs(title = &amp;quot;Desa clinic in Terengganu, Malaysia&amp;quot;, 
       subtitle = &amp;quot;(Data last updated: Klinik Desa - 9 Mac 2021)&amp;quot;,
       caption = expression(paste(italic(&amp;quot;Sumber data: https://www.data.gov.my/data/ms_MY/group/pemetaan&amp;quot;)))) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-13-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;geom_spatvector&lt;/code&gt; is from &lt;code&gt;tidyterra&lt;/code&gt; package. Alternatively, we can plot using &lt;code&gt;geom_sf&lt;/code&gt;but we need to convert the &lt;code&gt;SpatVector&lt;/code&gt; data into &lt;code&gt;sf&lt;/code&gt; object using &lt;code&gt;sf::st_as_sf&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(data = sf::st_as_sf(Trg)) +
  geom_sf(color = &amp;quot;grey&amp;quot;, fill = NA) +
  geom_point(data = clinic_trg, aes(x = longitude, y = latitude, color = &amp;quot;red&amp;quot;)) +
  theme_minimal() +
  theme(legend.position = &amp;quot;none&amp;quot;) +
  xlab(&amp;quot;Longitude&amp;quot;) +
  ylab(&amp;quot;Latitude&amp;quot;) +
  labs(title = &amp;quot;Desa clinic in Terengganu, Malaysia&amp;quot;, 
       subtitle = &amp;quot;(Data last updated: Klinik Desa - 9 Mac 2021)&amp;quot;,
       caption = expression(paste(italic(&amp;quot;Sumber data: https://www.data.gov.my/data/ms_MY/group/pemetaan&amp;quot;)))) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-14-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Both approaches produce the same plot.&lt;/p&gt;
&lt;p&gt;We can further add district labels to the plots. For example, using the &lt;code&gt;geom_sf&lt;/code&gt;, we can stack it with &lt;code&gt;geom_sf_label&lt;/code&gt; layer. We can alternatively use &lt;code&gt;theme_void&lt;/code&gt; to remove the background and the map axis.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(data = sf::st_as_sf(Trg)) +
  geom_sf(color = &amp;quot;grey&amp;quot;, fill = NA) +
  geom_sf_label(aes(label = NAME_2)) +
  geom_point(data = clinic_trg, aes(x = longitude, y = latitude, color = &amp;quot;red&amp;quot;)) +
  theme_void() +
  theme(legend.position = &amp;quot;none&amp;quot;) +
  xlab(&amp;quot;Longitude&amp;quot;) +
  ylab(&amp;quot;Latitude&amp;quot;) +
  labs(title = &amp;quot;Desa clinic in Terengganu, Malaysia&amp;quot;, 
       subtitle = &amp;quot;(Data last updated: Klinik Desa - 9 Mac 2021)&amp;quot;,
       caption = expression(paste(italic(&amp;quot;Sumber data: https://www.data.gov.my/data/ms_MY/group/pemetaan&amp;quot;)))) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/mapping-the-states-in-malaysia/index.en_files/figure-html/unnamed-chunk-15-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>My first interactive map with {leaflet}</title>
      <link>https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/</link>
      <pubDate>Sun, 28 Nov 2021 00:00:00 +0000</pubDate>
      <guid>https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/</guid>
      <description>
&lt;script src=&#34;https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/index.en_files/header-attrs/header-attrs.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/index.en_files/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/index.en_files/pymjs/pym.v1.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/index.en_files/widgetframe-binding/widgetframe.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;I have tried creating a map with ggplot2 &lt;a href=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/&#34;&gt;previously&lt;/a&gt;. In this post, I will try to create an interactive map using &lt;code&gt;leaflet&lt;/code&gt; package in R.&lt;/p&gt;
&lt;p&gt;These are the required packages.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(tidyverse)
library(tidygeocoder)
library(leaflet)
library(htmltools)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, I’m going to use a clinics location data in Malaysia. I already uploaded this data tomy &lt;a href=&#34;https://github.com/tengku-hanis/clinic-data&#34;&gt;GitHub repo&lt;/a&gt;. I will skip the explanation for the pre-processing part, but it is the same pre-processing as my &lt;a href=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/&#34;&gt;previous post&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Read the data
clinic1m &amp;lt;- read.csv(&amp;quot;https://raw.githubusercontent.com/tengku-hanis/clinic-data/main/clinic1m.csv&amp;quot;)
clinicDesa &amp;lt;- read.csv(&amp;quot;https://raw.githubusercontent.com/tengku-hanis/clinic-data/main/clinicdesa.csv&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;details&gt;
&lt;summary&gt;
Show code for pre-processing
&lt;/summary&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Get the missing coordinate based on postal codes
clinic1m2 &amp;lt;- 
  clinic1m %&amp;gt;%
  mutate(country = &amp;quot;malaysia&amp;quot;) %&amp;gt;% 
  select(name, postcode, country) %&amp;gt;% 
  mutate(postcode = ifelse(nchar(postcode) == 4, paste0(0, postcode), postcode)) %&amp;gt;%
  geocode(postalcode = postcode, country = country, method = &amp;quot;osm&amp;quot;)

# Add coordinate from external sources for the still missing coordinates
add_coord &amp;lt;- 
  read.table(header = T, text = &amp;quot;
postal_code    latitude   longitude
16070            6.0334    102.3499
26060            3.6228    102.3926
90700            5.8456    118.0571
26060            3.6228    102.3926&amp;quot;)

# Drop clinics with the still missing coordinate
clinic1m2 &amp;lt;- 
  clinic1m2 %&amp;gt;% 
  mutate(lat = ifelse(postcode %in% add_coord$postal_code, add_coord$latitude, lat), 
         long = ifelse(postcode %in% add_coord$postal_code, add_coord$longitude, long)) %&amp;gt;% 
  drop_na() #drop 2 clinic1m

# Bind the 2 data
all_clinic &amp;lt;- 
  clinic1m2 %&amp;gt;% 
  mutate(Type = &amp;quot;1Malaysia&amp;quot;) %&amp;gt;% 
  select(name, Type, lat, long) %&amp;gt;% 
  bind_rows(clinicDesa %&amp;gt;% 
              mutate(Type = &amp;quot;Desa&amp;quot;, 
                     lat = latitude, 
                     long = longitude) %&amp;gt;% 
              select(name, Type, lat, long)) %&amp;gt;% 
  mutate(name = str_to_title(name))&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;p&gt;First, we going to plot the coordinates to see if there is anything strange.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(all_clinic, aes(long, lat, color = Type)) +
  geom_point() +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/my-first-interactive-map-with-leaflet/index.en_files/figure-html/unnamed-chunk-4-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;So, we are going to remove the two isolated points as seen from the plot.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;all_clinic2 &amp;lt;- all_clinic %&amp;gt;% filter(long &amp;gt; 25)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once we have our data ready, we can supply to &lt;code&gt;leaflet&lt;/code&gt;. We can choose the type of map from &lt;code&gt;addProviderTiles()&lt;/code&gt;. Some need an api, but the one we choose here does not. We supply the longitude and latitude of our data to &lt;code&gt;addCircleMarkers()&lt;/code&gt;, and &lt;code&gt;clusterOptions&lt;/code&gt; to cluster our data.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;leaflet(all_clinic2) %&amp;gt;% 
  addProviderTiles(providers$Stamen.Watercolor) %&amp;gt;%
  addProviderTiles(providers$Stamen.TerrainLabels) %&amp;gt;%
  addCircleMarkers(~long, ~lat, 
                   clusterOptions = markerClusterOptions())&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;htmlwidget-1&#34; style=&#34;width:100%;height:480px;&#34; class=&#34;widgetframe html-widget&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-1&#34;&gt;{&#34;x&#34;:{&#34;url&#34;:&#34;index.en_files/figure-html//widgets/widget_unnamed-chunk-7.html&#34;,&#34;options&#34;:{&#34;xdomain&#34;:&#34;*&#34;,&#34;allowfullscreen&#34;:false,&#34;lazyload&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;Next, we can add a label.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;labels &amp;lt;- 
  sprintf(&amp;quot;&amp;lt;strong&amp;gt;%s&amp;lt;/strong&amp;gt;&amp;quot;, all_clinic$name) %&amp;gt;% 
  lapply(htmltools::HTML)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Also, we can add a mini map to our map. Here, I change the type of map to a more appropriate one.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;leaflet(all_clinic2) %&amp;gt;% 
  addProviderTiles(providers$OpenStreetMap) %&amp;gt;%
  addCircleMarkers(~long, ~lat, popup = ~labels, # popup add the label
                   clusterOptions = markerClusterOptions()) %&amp;gt;% 
    # add a mini map
  addMiniMap(tiles = providers$OpenStreetMap, zoomLevelOffset = -3)&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;htmlwidget-2&#34; style=&#34;width:100%;height:480px;&#34; class=&#34;widgetframe html-widget&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-2&#34;&gt;{&#34;x&#34;:{&#34;url&#34;:&#34;index.en_files/figure-html//widgets/widget_unnamed-chunk-10.html&#34;,&#34;options&#34;:{&#34;xdomain&#34;:&#34;*&#34;,&#34;allowfullscreen&#34;:false,&#34;lazyload&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;Notice that the coordinates look more accurate as compared to the map I created with &lt;code&gt;ggplot2&lt;/code&gt; previously.&lt;/p&gt;
&lt;p&gt;References:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lauriebaker.rbind.io/post/where_work/&#34; class=&#34;uri&#34;&gt;https://lauriebaker.rbind.io/post/where_work/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://laurielbaker.github.io/DSCA_leaflet_mapping_in_r/&#34; class=&#34;uri&#34;&gt;https://laurielbaker.github.io/DSCA_leaflet_mapping_in_r/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Making maps with R (my first attempt ever!)</title>
      <link>https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/</link>
      <pubDate>Fri, 12 Nov 2021 00:00:00 +0000</pubDate>
      <guid>https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/</guid>
      <description>
&lt;script src=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/index.en_files/header-attrs/header-attrs.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;As written in the title of the post, this is my first try ever in making a map with R. I found a great data on the distribution of the clinics in Malaysia. The two types of clinic that we have here:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Klinik 1Malaysia (1Malaysia clinic)&lt;/li&gt;
&lt;li&gt;Klinik Desa (Desa clinic)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Originally, these two data are a separated data. Both of the data can be downloaded from &lt;a href=&#34;https://www.data.gov.my/data/ms_MY/group/pemetaan&#34;&gt;here&lt;/a&gt;. Also, I have uploaded the data into my &lt;a href=&#34;https://github.com/tengku-hanis/clinic-data&#34;&gt;GitHub repo&lt;/a&gt; for those interested. Klinik Desa data have a latitude and longitude information, but Klinik 1Malaysia data does not.&lt;/p&gt;
&lt;p&gt;These are the required packages.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(rworldmap) #to get a Malaysia map
library(tidyverse)
library(tidygeocoder) #to get latitude and logitude&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Read the data.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;clinic1m &amp;lt;- read.csv(&amp;quot;https://raw.githubusercontent.com/tengku-hanis/clinic-data/main/clinic1m.csv&amp;quot;)
clinicDesa &amp;lt;- read.csv(&amp;quot;https://raw.githubusercontent.com/tengku-hanis/clinic-data/main/clinicdesa.csv&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, we need to get a latitude and longitude information for Klinik 1Malaysia data. So, we going to retrieve the coordinates based on the postal code, though this is not very accurate. We can use &lt;code&gt;tidygeocoder&lt;/code&gt; for this.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;clinic1m2 &amp;lt;- 
  clinic1m %&amp;gt;%
  mutate(country = &amp;quot;malaysia&amp;quot;) %&amp;gt;% 
  select(name, postcode, country) %&amp;gt;% 
  mutate(postcode = ifelse(nchar(postcode) == 4, paste0(0, postcode), postcode)) %&amp;gt;%
  geocode(postalcode = postcode, country = country, method = &amp;quot;osm&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Further checking on the data, we notice that 5 clinics have no coordinate info.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;clinic1m2 %&amp;gt;% filter(is.na(lat) | is.na(long))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 5 x 5
##   name                                     postcode country    lat  long
##   &amp;lt;chr&amp;gt;                                    &amp;lt;chr&amp;gt;    &amp;lt;chr&amp;gt;    &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt;
## 1 Klinik 1 Malaysia Bandar Lela            90700    malaysia    NA    NA
## 2 Klinik 1 Malaysia Batu Melintang         17250    malaysia    NA    NA
## 3 Klinik 1 Malaysia Cakerapurnama          45010    malaysia    NA    NA
## 4 Klinik 1 Malaysia Jelawat                16070    malaysia    NA    NA
## 5 Klinik 1 Malaysia Taman Kempadang Makmur 26060    malaysia    NA    NA&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;some-data-pre-processing&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Some data pre-processing&lt;/h2&gt;
&lt;p&gt;So, I found this &lt;a href=&#34;https://www.listendata.com/2020/11/zip-code-to-latitude-and-longitude.html&#34;&gt;data&lt;/a&gt; after some googling time, which give coordinate based on the postal code. So, we going to add in the missing coordinate based on this online data.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;add_coord &amp;lt;- 
  read.table(header = T, text = &amp;quot;
postal_code    latitude   longitude
16070            6.0334    102.3499
26060            3.6228    102.3926
90700            5.8456    118.0571
26060            3.6228    102.3926&amp;quot;)

clinic1m2 &amp;lt;- 
  clinic1m2 %&amp;gt;% 
  mutate(lat = ifelse(postcode %in% add_coord$postal_code, add_coord$latitude, lat), 
         long = ifelse(postcode %in% add_coord$postal_code, add_coord$longitude, long)) %&amp;gt;% 
  drop_na() #drop 2 clinic1m&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even after add in the missing coordinate, we still missing 2 coordinates. So, we going to drop those 2 clinics. Next, we combine both data.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;all_clinic &amp;lt;- 
  clinic1m2 %&amp;gt;% 
  mutate(Type = &amp;quot;1Malaysia&amp;quot;) %&amp;gt;% 
  select(Type, lat, long) %&amp;gt;% 
  bind_rows(clinicDesa %&amp;gt;% 
              mutate(Type = &amp;quot;Desa&amp;quot;, 
                     lat = latitude, 
                     long = longitude) %&amp;gt;% 
              select(Type, lat, long))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s try plotting the data first.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(all_clinic, aes(long, lat, color = Type)) +
  geom_point() +
  theme_minimal() #should remove the isolated two data&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/index.en_files/figure-html/unnamed-chunk-7-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;We have 2 isolated points from Klinik Desa data. We will drop these 2 points as well.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;all_clinic2 &amp;lt;- all_clinic %&amp;gt;% filter(long &amp;gt; 25)&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;plotting-the-map&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Plotting the map&lt;/h2&gt;
&lt;p&gt;There are 2 ways to plot our data to Malaysia map, that we going to cover in this post.&lt;/p&gt;
&lt;div id=&#34;map-from-ggplot2&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;1) map from &lt;code&gt;ggplot2&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;First, we need to get the map.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;global &amp;lt;- map_data(&amp;quot;world&amp;quot;) #get map&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once, we retrieved the map, we need to filter the region to Malaysia. The rest of the codes are &lt;code&gt;ggplot2&lt;/code&gt; function as we know it.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot() + 
  geom_polygon(data = global %&amp;gt;% filter(region == &amp;quot;Malaysia&amp;quot;), aes(x=long, y = lat, group = group), 
               fill = &amp;quot;gray85&amp;quot;) + 
  coord_fixed(1.3) +
  geom_point(data = all_clinic2, aes(x = long, y = lat, group = Type, color = Type, shape = Type)) +
  theme_void() + 
  xlab(&amp;quot;Longitude&amp;quot;) +
  ylab(&amp;quot;Latitude&amp;quot;) +
  labs(title = &amp;quot;Klinik 1Malaysia dan Klinik Desa di Malaysia&amp;quot;, 
       subtitle = &amp;quot;(Data dikemaskini: Klinik 1Malaysia - 16 Mac 2021, Klinik Desa - 9 Mac 2021)&amp;quot;,
       caption = expression(paste(italic(&amp;quot;Sumber data: https://www.data.gov.my/data/ms_MY/group/pemetaan&amp;quot;))), 
       color = &amp;quot;Jenis klinik:&amp;quot;, 
       shape = &amp;quot;Jenis klinik:&amp;quot;) +
  theme(plot.title = element_text(hjust = 0.5), 
        plot.subtitle = element_text(hjust = 0.5), 
        legend.position = &amp;quot;bottom&amp;quot;) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/index.en_files/figure-html/unnamed-chunk-10-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;map-from-rworldmap&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;2) map from &lt;code&gt;rworldmap&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The flow is similar, we need to get the map first. Then, restrict the map to Malaysia region.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;world &amp;lt;- getMap(resolution = &amp;quot;low&amp;quot;) #get map
msia &amp;lt;- world[world@data$ADMIN == &amp;quot;Malaysia&amp;quot;, ]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rest of the codes are similar to the first approach. But, we going to change the theme a bit.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot() +
  geom_polygon(data = msia, aes(x = long, y = lat, group = group), fill = NA, colour = &amp;quot;black&amp;quot;) +
  geom_point(data = all_clinic2, aes(x = long, y = lat, group = Type, color = Type, shape = Type)) +
  coord_quickmap() + 
  theme_minimal() + 
  xlab(&amp;quot;Longitude&amp;quot;) +
  ylab(&amp;quot;Latitude&amp;quot;) +
  labs(title = &amp;quot;Klinik 1Malaysia dan Klinik Desa di Malaysia&amp;quot;, 
       subtitle = &amp;quot;(Data dikemaskini: Klinik 1Malaysia - 16 Mac 2021, Klinik Desa - 9 Mac 2021)&amp;quot;,
       caption = expression(paste(italic(&amp;quot;Sumber data: https://www.data.gov.my/data/ms_MY/group/pemetaan&amp;quot;))), 
       color = &amp;quot;Jenis klinik:&amp;quot;, 
       shape = &amp;quot;Jenis klinik:&amp;quot;) +
  theme(plot.title = element_text(hjust = 0.5), 
        plot.subtitle = element_text(hjust = 0.5), 
        legend.position = &amp;quot;bottom&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://tengkuhanis.netlify.app/post/making-maps-with-r-my-first-attempt-ever/index.en_files/figure-html/unnamed-chunk-12-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The coordinates that we have are not as accurate as it should, or maybe there is something wrong that I miss along the way. As we can see, we have clinics on the ocean. As far as I know, we Malaysian are not that advanced yet. Also, noticed that we severely lacking clinics in Sarawak, given that our data is correct.&lt;/p&gt;
&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>
