<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jakub Nowosad</title>
    <description>The latest articles on DEV Community by Jakub Nowosad (@jakub_nowosad).</description>
    <link>https://dev.to/jakub_nowosad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F289457%2F0272e8f9-46be-4a41-8458-f5ee2539d1e1.jpg</url>
      <title>DEV Community: Jakub Nowosad</title>
      <link>https://dev.to/jakub_nowosad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jakub_nowosad"/>
    <language>en</language>
    <item>
      <title>Inset maps with ggplot2</title>
      <dc:creator>Jakub Nowosad</dc:creator>
      <pubDate>Sun, 08 Dec 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/jakub_nowosad/inset-maps-with-ggplot2-5178</link>
      <guid>https://dev.to/jakub_nowosad/inset-maps-with-ggplot2-5178</guid>
      <description>&lt;p&gt;Inset maps enable multiple places to be shown in the same geographic data visualisation, as described in the &lt;a href="https://geocompr.robinlovelace.net/adv-map.html#inset-maps"&gt;Inset maps section (8.2.7)&lt;/a&gt; of our open source book Geocomputation with R. The topic of inset maps has gained attention and recently &lt;a href="https://twitter.com/espinielli/"&gt;Enrico Spinielli&lt;/a&gt; asked inset maps could be created for data in unusual coordinate systems:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Speaking of insets, do you know of any ggplot2 examples with an inset for showing where the (bbox of the) map is in an orthographic/satellite proj? It is usually done to provide geographic context... NYT/WP have fantastic maps like that&lt;/p&gt;

&lt;p&gt;— espinielli (@espinielli) &lt;a href="https://twitter.com/espinielli/status/1191452410394361863?ref_src=twsrc%5Etfw"&gt;November 4, 2019&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;R’s flexibility allows inset maps to be created in various ways, using different approaches and packages. However, the main idea stays the same: we need to create at least two maps: a larger one, called the main map, that shows the central story and a smaller one, called the inset map, that puts the main map in context.&lt;/p&gt;

&lt;p&gt;This blog post shows how to create inset maps with &lt;strong&gt;ggplot2&lt;/strong&gt; for visualization. The approach also uses the &lt;strong&gt;sf&lt;/strong&gt; package for spatial data reading and handling, &lt;strong&gt;cowplot&lt;/strong&gt; to arrange inset maps, and &lt;strong&gt;rcartocolor&lt;/strong&gt; for additional color palettes. To reproduce the results on your own computer, after installing them, these packages can be attached as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;library(sf)
library(ggplot2)
library(cowplot)
library(rcartocolor)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Basic inset map
&lt;/h1&gt;

&lt;p&gt;Let’s start by creating a basic inset map.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data preparation
&lt;/h2&gt;

&lt;p&gt;The first step is to read and prepare the data we want to visualize. We use the &lt;code&gt;us_states&lt;/code&gt; data from the &lt;strong&gt;spData&lt;/strong&gt; package as the source of the inset map, and &lt;code&gt;north_carolina&lt;/code&gt; from the &lt;strong&gt;sf&lt;/strong&gt; package as the source of the main map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;library(spData)
data("us_states", package = "spData")
north_carolina = read_sf(system.file("shape/nc.shp", package = "sf"))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Both objects should have the same coordinate reference system (&lt;code&gt;crs&lt;/code&gt;). Here, we use &lt;code&gt;crs = 2163&lt;/code&gt;, which represents the US National Atlas Equal Area projection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;us_states_2163 = st_transform(us_states, crs = 2163)
north_carolina_2163 = st_transform(north_carolina, crs = 2163)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We also need to have the borders of the area we want to highlight (use in the main map). This can be done by extracting the bounding box of our &lt;code&gt;north_carolina_2163&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;north_carolina_2163_bb = st_as_sfc(st_bbox(north_carolina_2163))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Maps creation
&lt;/h2&gt;

&lt;p&gt;The second step is to create both inset and main maps independently. The inset map should show the context (larger area) and highlight the area of interest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ggm1 = ggplot() + 
  geom_sf(data = us_states_2163, fill = "white") + 
  geom_sf(data = north_carolina_2163_bb, fill = NA, color = "red", size = 1.2) +
  theme_void()

ggm1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K59QSu9Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-6-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K59QSu9Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-6-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main map’s role is to tell the story. Here we show the number of births between 1974 and 1978 in the North Carolina counties (the &lt;code&gt;BIR74&lt;/code&gt; variable) using the Mint color palette from the &lt;strong&gt;rcartocolor&lt;/strong&gt; palette. We also customize the legend position and size - this way, the legend is a part of the map, instead of being somewhere outside the map frame.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ggm2 = ggplot() + 
  geom_sf(data = north_carolina_2163, aes(fill = BIR74)) +
  scale_fill_carto_c(palette = "Mint") +
  theme_void() +
  theme(legend.position = c(0.4, 0.05),
        legend.direction = "horizontal",
        legend.key.width = unit(10, "mm"))

ggm2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--433Jc3xL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-7-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--433Jc3xL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-7-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Maps joining
&lt;/h2&gt;

&lt;p&gt;The final step is to join two maps. This can be done using functions from the &lt;strong&gt;cowplot&lt;/strong&gt; package. We create an empty ggplot layer using &lt;code&gt;ggdraw()&lt;/code&gt;, fill it with out main map (&lt;code&gt;draw_plot(ggm2)&lt;/code&gt;), and add an inset map by specifing its position and size:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gg_inset_map1 = ggdraw() +
  draw_plot(ggm2) +
  draw_plot(ggm1, x = 0.05, y = 0.65, width = 0.3, height = 0.3)

gg_inset_map1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J5oi7kVZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-8-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J5oi7kVZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-8-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final map can be saved using the &lt;code&gt;ggsave()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ggsave(filename = "01_gg_inset_map.png", 
       plot = gg_inset_map1,
       width = 8, 
       height = 4,
       dpi = 150)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Advanced inset map
&lt;/h1&gt;

&lt;p&gt;Let’s expand the idea of the inset map in &lt;strong&gt;ggplot2&lt;/strong&gt; based on the previous example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data preparation
&lt;/h2&gt;

&lt;p&gt;This map will use the US states borders (&lt;code&gt;states()&lt;/code&gt;) as the source of the inset map and the Kentucky Senate legislative districts (&lt;code&gt;state_legislative_districts()&lt;/code&gt;) as the main map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;library(tigris)
us_states = states(cb = FALSE, class = "sf")
ky_districts = state_legislative_districts("KY", house = "upper",
                                           cb = FALSE, class = "sf")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;states()&lt;/code&gt; function, in addition to the 50 states, also returns the District of Columbia, Puerto Rico, American Samoa, the Commonwealth of the Northern Mariana Islands, Guam, and the US Virgin Islands. For our purpose, we are interested in the continental 48 states and the District of Columbia only; therefore, we remove the rest of the divisions using &lt;code&gt;subset()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;us_states = subset(us_states, 
                   !NAME %in% c(
                     "United States Virgin Islands",
                     "Commonwealth of the Northern Mariana Islands",
                     "Guam",
                     "American Samoa",
                     "Puerto Rico",
                     "Alaska",
                     "Hawaii"
                   ))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The same as in the example above, we transform both objects to have the same projection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ky_districts_2163 = st_transform(ky_districts, crs = 2163)
us_states_2163 = st_transform(us_states, crs = 2163)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We also extract the bounding box of the main object here. However, instead of using it directly, we add a buffer of 10,000 meters around it. This output will be handy in both inset and main maps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ky_districts_2163_bb = st_as_sfc(st_bbox(ky_districts_2163))
ky_districts_2163_bb = st_buffer(ky_districts_2163_bb, dist = 10000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ky_districts_2163&lt;/code&gt; object does not have any interesting variables to visualize, so we create some random values here. However, we could also join the districts’ data with another dataset in this step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ky_districts_2163$values = runif(nrow(ky_districts_2163))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Map creation
&lt;/h2&gt;

&lt;p&gt;The inset map should be as clear and simple as possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ggm3 = ggplot() + 
  geom_sf(data = us_states_2163, fill = "white", size = 0.2) + 
  geom_sf(data = ky_districts_2163_bb, fill = NA, color = "blue", size = 1.2) +
  theme_void()

ggm3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cHWicosV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-15-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cHWicosV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-15-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, the main map looks better when we provide some additional context to our data. One of the ways to achieve it is to add the borders of the neighboring states.&lt;/p&gt;

&lt;p&gt;Importantly, we also need to limit the extent of our main map to the range of the frame in the inset map. This can be done with the &lt;code&gt;coord_sf()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ggm4 = ggplot() + 
  geom_sf(data = us_states_2163, fill = "#F5F5DC") +
  geom_sf(data = ky_districts_2163, aes(fill = values)) +
  scale_fill_carto_c(palette = "Sunset") +
  theme_void() +
  theme(legend.position = c(0.5, 0.07),
        legend.direction = "horizontal",
        legend.key.width = unit(10, "mm"),
        plot.background = element_rect(fill = "#BFD5E3")) +
  coord_sf(xlim = st_bbox(ky_districts_2163_bb)[c(1, 3)],
           ylim = st_bbox(ky_districts_2163_bb)[c(2, 4)])

ggm4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fniENmi1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-16-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fniENmi1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-16-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we draw two maps together, trying to find the best location and size for the inset map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gg_inset_map2 = ggdraw() +
  draw_plot(ggm4) +
  draw_plot(ggm3, x = 0.02, y = 0.65, width = 0.35, height = 0.35)

gg_inset_map2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E5KG75H9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-17-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E5KG75H9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/ggplot2-inset-maps_files/figure-html/unnamed-chunk-17-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final map can be saved using the &lt;code&gt;ggsave()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ggsave(filename = "02_gg_inset_map.png", 
       plot = gg_inset_map2,
       width = 7.05, 
       height = 4,
       dpi = 150)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;The above examples can be adjusted to any spatial data and location. It is also possible to put more context on the map, including adding main cities’ names, neighboring states’ names, and annotations (using &lt;code&gt;geom_text()&lt;/code&gt;, &lt;code&gt;geom_label()&lt;/code&gt;). The main map can also be enhanced with the north arrow and scale bar using the &lt;a href="https://github.com/oswaldosantos/ggsn"&gt;&lt;strong&gt;ggsn&lt;/strong&gt;&lt;/a&gt; package.&lt;/p&gt;

&lt;p&gt;As always with R, there are many possible options to create inset maps. You can find two examples of inset maps created using the &lt;strong&gt;tmap&lt;/strong&gt; package in &lt;a href="https://geocompr.robinlovelace.net/adv-map.html#inset-maps"&gt;the Geocomputation with R book&lt;/a&gt;. The second example is a classic map of the United States, which consists of the contiguous United States, Hawaii, and Alaska. However, Hawaii and Alaska are displayed at different geographic scales than the main map there. This problem can also be solved in R, which you can see in &lt;a href="https://nowosad.github.io/post/making-alternative-inset-maps-of-the-usa/"&gt;the Making maps of the USA with R: alternative layout&lt;/a&gt; blogpost and the &lt;a href="https://github.com/Nowosad/us-map-alternative-layout"&gt;Alternative layout for maps of the United States&lt;/a&gt; repository.&lt;/p&gt;

&lt;p&gt;The presented approaches also apply to other areas. For example, you can find three ways on how to create an inset map of Spain in the &lt;a href="https://github.com/Nowosad/spain-map-layout"&gt;Alternative layout for maps of Spain&lt;/a&gt; repository. Other examples of inset maps with &lt;strong&gt;ggplot2&lt;/strong&gt; can be found in the &lt;a href="https://ryanpeek.github.io/mapping-in-R-workshop/vig_making_inset_maps.html"&gt;Inset Maps&lt;/a&gt; vignette by &lt;a href="https://twitter.com/riverpeek"&gt;Ryan Peek&lt;/a&gt; and the blog post &lt;a href="https://www.r-spatial.org/r/2018/10/25/ggplot2-sf-3.html"&gt;Drawing beautiful maps programmatically with R, sf and ggplot2&lt;/a&gt; by &lt;a href="https://twitter.com/melimore86"&gt;Mel Moreno&lt;/a&gt; and &lt;a href="https://twitter.com/MabLabUF"&gt;Mathieu Basille&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The decision which option to use depends on the expected map type preferred R packages, etc. Try different approaches on your own data and decide what works best for you!&lt;/p&gt;

</description>
      <category>rstats</category>
      <category>spatial</category>
      <category>ggplot2</category>
      <category>maps</category>
    </item>
    <item>
      <title>Map coloring: the color scale styles available in the tmap package</title>
      <dc:creator>Jakub Nowosad</dc:creator>
      <pubDate>Thu, 17 Oct 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/jakub_nowosad/map-coloring-the-color-scale-styles-available-in-the-tmap-package-hp3</link>
      <guid>https://dev.to/jakub_nowosad/map-coloring-the-color-scale-styles-available-in-the-tmap-package-hp3</guid>
      <description>&lt;p&gt;This vignette builds on the &lt;a href="https://geocompr.robinlovelace.net/adv-map.html"&gt;making maps chapter&lt;/a&gt; of &lt;a href="https://geocompr.github.io/"&gt;the Geocomputation with R book&lt;/a&gt;. Its goal is to demonstrate all possible map styles available in the &lt;strong&gt;tmap&lt;/strong&gt; package.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;The examples below assume the following packages are attached:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;library(spData) # example datasets
library(tmap) # map creation
library(sf) # spatial data reprojection
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;world&lt;/code&gt; object containing a world map data from &lt;a href="https://www.naturalearthdata.com/"&gt;Natural Earth&lt;/a&gt; and information about countries’ names, regions, and subregions they belong to, areas, life expectancies, and populations. This object is in geographical coordinates using the WGS84 datum, however, for mapping purposes, the Mollweide projection is a better alternative (learn more in &lt;a href="https://geocompr.robinlovelace.net/reproj-geo-data.html#modifying-map-projections"&gt;the modifying map projections section&lt;/a&gt;). The &lt;code&gt;st_tranform&lt;/code&gt; function from the &lt;strong&gt;sf&lt;/strong&gt; package allows for quick reprojection to the selected coordinate reference system (e.g., &lt;code&gt;"+proj=moll"&lt;/code&gt; represents the Mollweide projection).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;world_moll = st_transform(world, crs = "+proj=moll")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  One color
&lt;/h1&gt;

&lt;p&gt;Let’s start with the basics. To create a simple world map, we need to specify the data object (&lt;code&gt;world_moll&lt;/code&gt;) inside the &lt;code&gt;tm_shape()&lt;/code&gt; function, and the way we want to visualize it. The &lt;strong&gt;tmap&lt;/strong&gt; package offers several visualisation possibilities for polygons, including &lt;code&gt;tm_borders()&lt;/code&gt;, &lt;code&gt;tm_fill()&lt;/code&gt;, and &lt;code&gt;tm_polygons()&lt;/code&gt;. The last one draws the filled polygons with borders, where the fill color can be specified with the &lt;code&gt;col&lt;/code&gt; argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lightblue")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--65p3V2kJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-3-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--65p3V2kJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-3-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output is a map of world countries, where each country is filled with a light blue color.&lt;/p&gt;

&lt;h1&gt;
  
  
  Coloring of adjacent polygons
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;col&lt;/code&gt; argument is very flexible, and its action depends on the value provided. In the previous example, we provided a single color value resulting in a map with one color. To create a map, where adjacent polygons do not get the same color, we need to provide a keyword &lt;code&gt;"MAP_COLORS"&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "MAP_COLORS")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wGM6za7X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-4-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wGM6za7X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-4-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The default color can be changed using the &lt;code&gt;palette&lt;/code&gt; argument - run the &lt;code&gt;tmaptools::palette_explorer()&lt;/code&gt; function to see possible palettes’ names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "MAP_COLORS",
              palette = "Pastel1")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--76WwalPr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-5-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--76WwalPr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-5-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, in this case, it is possible to use the &lt;code&gt;minimize&lt;/code&gt; argument, which triggers the internal algorithm to search for a minimal number of colors for visualization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "MAP_COLORS",
              minimize = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r1i4qAFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-6-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r1i4qAFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-6-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The new map uses five colors&amp;lt;!-- except for Angola (blue color)--&amp;gt;. On a side note, in theory, no more than four colors are required to color the polygons of the map so that no two adjacent polygons have the same color (learn more about the four color map theorem on &lt;a href="https://en.wikipedia.org/wiki/Four_color_theorem"&gt;Wikipedia&lt;/a&gt;).&lt;/p&gt;

&lt;h1&gt;
  
  
  Categorical maps
&lt;/h1&gt;

&lt;p&gt;The third use of the &lt;code&gt;col&lt;/code&gt; argument is by providing the variable (column) name. In this case, the map will represent the given variable. By default, &lt;strong&gt;tmap&lt;/strong&gt; behaves differently depending on the input variable type. For example, it will create a categorical map when the provided variable contains characters or factors. The &lt;code&gt;tm_polygons(col = "subregion", style = "cat")&lt;/code&gt; code will be run automatically in this case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "subregion")+
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XJd_r1kc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-7-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XJd_r1kc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-7-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Discrete maps
&lt;/h1&gt;

&lt;p&gt;Discrete maps represents continuous numerical variables using discrete class intervals. There are several ways to convert continuous variables to discrete ones implemented in &lt;strong&gt;tmap&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pretty
&lt;/h2&gt;

&lt;p&gt;When the variable provided as the &lt;code&gt;col&lt;/code&gt; argument is numeric, &lt;strong&gt;tmap&lt;/strong&gt; will use the &lt;code&gt;"pretty"&lt;/code&gt; style as a default. In other words, it runs &lt;code&gt;tm_polygons(col = "lifeExp", style = "pretty")&lt;/code&gt; invisibly to the user. This style rounds breaks into whole numbers where possible and spaces them evenly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6CW4WiTd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-8-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6CW4WiTd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-8-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A histogram is added using &lt;code&gt;legend.hist = TRUE&lt;/code&gt; in this and several next examples to show how the selected map style relates to the distribution of values.&lt;/p&gt;

&lt;p&gt;It is possible to indicate a preferred number of classes using the &lt;code&gt;n&lt;/code&gt; argument. Importantly, not every &lt;code&gt;n&lt;/code&gt; is possible depending on the range of the values in the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              legend.hist = TRUE,
              n = 4) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NM3MnX7V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-9-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NM3MnX7V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-9-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixed
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"jenks"&lt;/code&gt; style allows for a manual selection of the breaks in conjunction with the &lt;code&gt;breaks&lt;/code&gt; argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp", 
              style = "fixed",
              breaks = c(45, 60, 75, 90),
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YNMjXgDU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-10-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YNMjXgDU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-10-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, the default labels can be overwritten using the &lt;code&gt;labels&lt;/code&gt; argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp", 
              style = "fixed",
              breaks = c(45, 60, 75, 90),
              labels = c("low", "medium", "high"),
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jFJOmgN2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-11-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jFJOmgN2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-11-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaks based on the standard deviation value
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"sd"&lt;/code&gt; style calculates a standard deviation of a given variable, and next use this value as the break width.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp", 
              style = "sd",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4Qm8FsjW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-12-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4Qm8FsjW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-12-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Fisher algorithm
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"fisher"&lt;/code&gt; style creates groups with maximalized homogeneity.&lt;sup&gt;1&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              style = "fisher",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sGuZG8OQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-13-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sGuZG8OQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-13-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Jenks natural breaks
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"jenks"&lt;/code&gt; style identifies groups of similar values in the data and maximizes the differences between categories.&lt;sup&gt;2&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              style = "jenks",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AjVg_D7D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-14-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AjVg_D7D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-14-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hierarchical clustering
&lt;/h2&gt;

&lt;p&gt;In the &lt;code&gt;"hclust"&lt;/code&gt; style, breaks are created using hierarchical clustering.&lt;sup&gt;3&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              style = "hclust",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ysiRxRoy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-15-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ysiRxRoy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-15-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bagged clustering
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"bclust"&lt;/code&gt; style uses the &lt;code&gt;bclust&lt;/code&gt; function to generate the breaks using bagged clustering.&lt;sup&gt;4&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              style = "bclust",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5_LkldaC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-16-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5_LkldaC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-16-1.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## Committee Member: 1(1) 2(1) 3(1) 4(1) 5(1) 6(1) 7(1) 8(1) 9(1) 10(1)
## Computing Hierarchical Clustering
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  k-means clustering
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"kmeans"&lt;/code&gt; style uses the &lt;code&gt;kmeans&lt;/code&gt; function to generate the breaks.&lt;sup&gt;5&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp", 
              style = "kmeans",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RS7Wxx5i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-17-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RS7Wxx5i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-17-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Quantile breaks
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"quantile"&lt;/code&gt; style creates breaks with an equal number of features (polygons).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp", 
              style = "quantile",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6qW0poZ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-18-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6qW0poZ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-18-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Equal breaks
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"equal"&lt;/code&gt; style divides input values into bins of equal range and is appropriate for variables with a uniform distribution. It is not recommended for variables with a skewed distribution as the resulting map may end-up having little color diversity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp", 
              style = "equal",
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ENGCkr9Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-19-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ENGCkr9Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-19-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Learn more about the implementation of discrete scales in &lt;a href="https://r-spatial.github.io/classInt/index.html"&gt;the &lt;strong&gt;classInt&lt;/strong&gt; package’s documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Continuous maps
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;tmap&lt;/strong&gt; package also allows for creating continuous maps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"cont"&lt;/code&gt; style presents a large number of colors over the continuous color field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              style = "cont") +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YsrrXHU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-20-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YsrrXHU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-20-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Order
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"order"&lt;/code&gt; style also presents a large number of colors over the continuous color field. However, this style is suited to visualize skewed distributions; notice that the values on the legend do not change linearly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "lifeExp",
              style = "order") +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GvAodynj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-21-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GvAodynj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-21-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Logarithmic scales
&lt;/h1&gt;

&lt;p&gt;The default numeric style, &lt;code&gt;pretty&lt;/code&gt;, is easy to understand, but it is not proper for maps of variables with skewed distributions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "pop") +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sFFWKjXn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-22-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sFFWKjXn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-22-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another possible style, &lt;code&gt;order&lt;/code&gt; works better in this case; however, it is not easy to interpret.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "pop", 
              style = "order") +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K8hr8888--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-23-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K8hr8888--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-23-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A better alternative, in this case, is to use a common logarithm (the logarithm to base 10) scale. The &lt;strong&gt;tmap&lt;/strong&gt; package gives two possibilities in this case - &lt;code&gt;"log10_pretty"&lt;/code&gt; and &lt;code&gt;"log10"&lt;/code&gt;. The &lt;code&gt;"log10_pretty"&lt;/code&gt; style is a common logarithmic version of the regular &lt;code&gt;pretty&lt;/code&gt; style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "pop", 
              style = "log10_pretty") +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bVFgeZlp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-24-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bVFgeZlp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-24-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, the &lt;code&gt;"log10"&lt;/code&gt; style is a version of a continuous scale.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world_moll) +
  tm_polygons(col = "pop", 
              style = "log10") +
  tm_layout(legend.outside = TRUE)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1DN1XRaG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-25-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1DN1XRaG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-styles_files/figure-html/unnamed-chunk-25-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusions
&lt;/h1&gt;

&lt;p&gt;Selecting a color scale style is not an easy task. It depends on the type of input variable and its distribution, but also the intended audience. Therefore, it is worth to spend some time and think about your readers (e.g., would they be able to understand the logarithmic scale or should you use the manual breaks instead?) and your data (e.g., how many breaks should there be to show different subgroups?). Now you know different color scale styles implemented in &lt;strong&gt;tmap&lt;/strong&gt; , so let’s try using them for your own projects!&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.tandfonline.com/doi/abs/10.1080/01621459.1958.10501479"&gt;https://www.tandfonline.com/doi/abs/10.1080/01621459.1958.10501479&lt;/a&gt;↩&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Jenks_natural_breaks_optimization"&gt;https://en.wikipedia.org/wiki/Jenks_natural_breaks_optimization&lt;/a&gt;↩&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;See the &lt;code&gt;?hclust&lt;/code&gt; documentation for more details.↩&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;See the &lt;code&gt;?bclust&lt;/code&gt; documentation for more details.↩&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;See the &lt;code&gt;?kmeans&lt;/code&gt; documentation for more details.↩&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>rstats</category>
      <category>spatial</category>
      <category>tmap</category>
      <category>maps</category>
    </item>
    <item>
      <title>Grids and graticules in the tmap package</title>
      <dc:creator>Jakub Nowosad</dc:creator>
      <pubDate>Wed, 04 Sep 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/jakub_nowosad/grids-and-graticules-in-the-tmap-package-2kif</link>
      <guid>https://dev.to/jakub_nowosad/grids-and-graticules-in-the-tmap-package-2kif</guid>
      <description>&lt;p&gt;This vignette builds on the &lt;a href="https://geocompr.robinlovelace.net/adv-map.html"&gt;making maps chapter&lt;/a&gt; of &lt;a href="https://geocompr.github.io/"&gt;the Geocomputation with R book&lt;/a&gt;. Its goal is to demonstrate how to set and modify grids and graticules in the &lt;strong&gt;tmap&lt;/strong&gt; package.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;The examples below assume the following packages are attached:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;library(spData) # example datasets
library(tmap) # map creation (&amp;gt;=2.3)
library(sf) # spatial data classes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Grids and graticules
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;tmap&lt;/strong&gt; package offers two ways to draws coordinate lines - &lt;code&gt;tm_grid()&lt;/code&gt; and &lt;code&gt;tm_graticules()&lt;/code&gt;. The role of &lt;code&gt;tm_grid()&lt;/code&gt; is to represent the input data’s coordinates. For example, the &lt;code&gt;nz&lt;/code&gt; object uses the New Zealand Transverse Mercator projection, with meters as its units.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) + 
  tm_polygons() +
  tm_grid()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L4iQp-6P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-3-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L4iQp-6P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-3-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tm_graticules()&lt;/code&gt; shows longitude lines (meridians) and latitude lines (parallels), with degrees as units (note the degree sign in the example below).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) + 
  tm_polygons() +
  tm_graticules()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xzTo_l8o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-4-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xzTo_l8o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-4-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Layers order
&lt;/h1&gt;

&lt;p&gt;Both, &lt;code&gt;tm_grid()&lt;/code&gt; and &lt;code&gt;tm_graticules()&lt;/code&gt; could be placed above or below the main spatial data. Its position on the map depends on its place in the code. When &lt;code&gt;tm_grid()&lt;/code&gt; or &lt;code&gt;tm_graticules()&lt;/code&gt; are placed after the code drawing geometry (e.g. &lt;code&gt;tm_polygons()&lt;/code&gt;), the grids or graticules are ploted on the top of the map. On the other hand, when &lt;code&gt;tm_grid()&lt;/code&gt; or &lt;code&gt;tm_graticules()&lt;/code&gt; are placed before the code drawing geometry (e.g. &lt;code&gt;tm_polygons()&lt;/code&gt;), the grids or graticules are plotted behind the spatial data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) +
  tm_graticules() + 
  tm_polygons()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iADnVcc8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-6-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iADnVcc8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-6-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Customization
&lt;/h1&gt;

&lt;p&gt;Grids and graticules can be easily customized in &lt;strong&gt;tmap&lt;/strong&gt; using several arguments. The first one, &lt;code&gt;labels.inside.frame&lt;/code&gt; moves the labels inside the map grid (it is set to &lt;code&gt;FALSE&lt;/code&gt; as the default).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) +
  tm_grid(labels.inside.frame = TRUE) + 
  tm_polygons()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ECHjIyz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-7-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ECHjIyz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-7-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The number of horizontal (&lt;code&gt;x&lt;/code&gt;) and vertical (&lt;code&gt;y&lt;/code&gt;) lines can be set using the &lt;code&gt;n.x&lt;/code&gt; and &lt;code&gt;n.y&lt;/code&gt; arguments. Importantly, &lt;strong&gt;tmap&lt;/strong&gt; rounds coordinate values to equally spaced “round” values, so the number of actual labels may be slightly different than set with &lt;code&gt;n.x&lt;/code&gt; and &lt;code&gt;n.y&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) +
  tm_grid(n.x = 4, n.y = 3) + 
  tm_polygons()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xd6600QF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-8-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xd6600QF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-8-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, &lt;code&gt;tm_grid()&lt;/code&gt; and &lt;code&gt;tm_graticules()&lt;/code&gt; shows ticks and lines. They can be disabled using &lt;code&gt;ticks = FALSE&lt;/code&gt; and &lt;code&gt;lines = FALSE&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) +
  tm_grid(ticks = FALSE) +
  tm_polygons()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nlXZ6Rzt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-9-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nlXZ6Rzt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-9-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Especially, &lt;code&gt;lines = FALSE&lt;/code&gt; could be useful when presenting raster data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) +
  tm_grid(lines = FALSE) +
  tm_polygons()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ak1CWcM3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-10-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ak1CWcM3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-10-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is also possible to customize &lt;code&gt;tm_grid()&lt;/code&gt; and &lt;code&gt;tm_graticules()&lt;/code&gt; apperance, for example by chaning the lines colors (&lt;code&gt;col&lt;/code&gt;), width (&lt;code&gt;lwd&lt;/code&gt;) or labels size (&lt;code&gt;labels.size&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(nz) +
  tm_grid(col = "red", lwd = 3, labels.size = 0.4) +
  tm_polygons()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1jLZmgQi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-11-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1jLZmgQi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-11-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above examples uses &lt;code&gt;tm_grid()&lt;/code&gt;, but the same arguments apply to the &lt;code&gt;tm_graticules()&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Layout settings
&lt;/h1&gt;

&lt;p&gt;By default, &lt;strong&gt;tmap&lt;/strong&gt; adds small inner margins between the presented data and the map frame. It works well in many cases, for example, see the map of New Zealand above. However, it does not look perfect for world maps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world) + 
  tm_graticules() + 
  tm_polygons()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y4J_mLOF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-12-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y4J_mLOF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-12-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The way to fix this is to use the &lt;code&gt;tm_layout()&lt;/code&gt; function and set its &lt;code&gt;inner.margins&lt;/code&gt; argument to &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tm_shape(world) + 
  tm_graticules() + 
  tm_polygons() +
  tm_layout(inner.margins = 0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EUr4LFJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-13-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EUr4LFJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://geocompr.github.io/post/2019/tmap-grid_files/figure-html/unnamed-chunk-13-1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
