class: center, middle, inverse, title-slide # Creating an Interactive Map ## Getting Started with tmap ### Conner Surrency, July 2020 --- # What Are We Doing? -- - We are going to walk through the process of creating a simple interactive map using tmap. -- - We will start by walking through the steps of creating a simple standard map. After that, we'll transform it into an interactive one. -- - In this case, we will narrow the data down to only information related to Australia, and then display each city in the nation based on its population. --- ## Set-Up -- We will need the following packages (and data from them) installed: -- - tidyverse -- - tmap --- count: false ## Building a Simple Map .panel1-standard-user[ ```r *library(tidyverse) *library(tmap) ## We will be using data from "world" and "Metro" *data(World, metro) ## We will filter the metro datasets down so that we only have ## incidences within Australia *metro ``` ] .panel2-standard-user[ ``` Simple feature collection with 436 features and 12 fields geometry type: POINT dimension: XY bbox: xmin: -123.1193 ymin: -37.814 xmax: 174.7667 ymax: 60.16925 geographic CRS: WGS 84 First 10 features: name name_long iso_a3 pop1950 pop1960 pop1970 pop1980 2 Kabul Kabul AFG 170784 285352 471891 977824 8 Algiers El Djazair (Algiers) DZA 516450 871636 1281127 1621442 13 Luanda Luanda AGO 138413 219427 459225 771349 16 Buenos Aires Buenos Aires ARG 5097612 6597634 8104621 9422362 17 Cordoba Cordoba ARG 429249 605309 809794 1009521 25 Rosario Rosario ARG 554483 671349 816230 953491 32 Yerevan Yerevan ARM 341432 537759 778158 1041587 33 Adelaide Adelaide AUS 429277 571822 850168 971856 34 Brisbane Brisbane AUS 441718 602999 904777 1134833 37 Melbourne Melbourne AUS 1331966 1851220 2499109 2839019 pop1990 pop2000 pop2010 pop2020 pop2030 geometry 2 1549320 2401109 3722320 5721697 8279607 POINT (69.17246 34.52889) 8 1797068 2140577 2432023 2835218 3404575 POINT (3.04197 36.7525) 13 1390240 2591388 4508434 6836849 10428756 POINT (13.23432 -8.83682) 16 10513284 12406780 14245871 15894307 16956491 POINT (-58.40037 -34.60508) 17 1200168 1347561 1459268 1562509 1718192 POINT (-64.18105 -31.4135) 25 1083819 1152387 1298073 1453814 1606993 POINT (-60.63932 -32.94682) 32 1174524 1111301 1065597 1023703 1057459 POINT (44.51462 40.182) 33 1081618 1141623 1217990 1320783 1505422 POINT (138.5986 -34.92866) 34 1381306 1666203 2033617 2388517 2721325 POINT (153.0281 -27.46794) 37 3154314 3460541 3951216 4500501 5070873 POINT (144.9633 -37.814) ``` ] --- count: false ## Building a Simple Map .panel1-standard-user[ ```r library(tidyverse) library(tmap) ## We will be using data from "world" and "Metro" data(World, metro) ## We will filter the metro datasets down so that we only have ## incidences within Australia metro %>% * filter(iso_a3 == "AUS") ``` ] .panel2-standard-user[ ``` Simple feature collection with 5 features and 12 fields geometry type: POINT dimension: XY bbox: xmin: 115.8525 ymin: -37.814 xmax: 153.0281 ymax: -27.46794 geographic CRS: WGS 84 name name_long iso_a3 pop1950 pop1960 pop1970 pop1980 pop1990 pop2000 1 Adelaide Adelaide AUS 429277 571822 850168 971856 1081618 1141623 2 Brisbane Brisbane AUS 441718 602999 904777 1134833 1381306 1666203 3 Melbourne Melbourne AUS 1331966 1851220 2499109 2839019 3154314 3460541 4 Perth Perth AUS 310759 409131 687856 921623 1194665 1432387 5 Sydney Sydney AUS 1689935 2134673 2892477 3252111 3631940 4052499 pop2010 pop2020 pop2030 geometry 1 1217990 1320783 1505422 POINT (138.5986 -34.92866) 2 2033617 2388517 2721325 POINT (153.0281 -27.46794) 3 3951216 4500501 5070873 POINT (144.9633 -37.814) 4 1697517 2036118 2329184 POINT (115.8525 -31.94784) 5 4364380 4729406 5300933 POINT (151.2073 -33.86785) ``` ] --- count: false ## Building a Simple Map .panel1-standard-user[ ```r library(tidyverse) library(tmap) ## We will be using data from "world" and "Metro" data(World, metro) ## We will filter the metro datasets down so that we only have ## incidences within Australia metro %>% filter(iso_a3 == "AUS") -> * metro_australia ``` ] .panel2-standard-user[ ] --- count: false ## Building a Simple Map .panel1-standard-user[ ```r library(tidyverse) library(tmap) ## We will be using data from "world" and "Metro" data(World, metro) ## We will filter the metro datasets down so that we only have ## incidences within Australia metro %>% filter(iso_a3 == "AUS") -> metro_australia ## Setting the mode as "Plot" will allow us to create a static map *tmap_mode("plot") *World ``` ] .panel2-standard-user[ ``` Simple feature collection with 177 features and 15 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -16656120 ymin: -8460601 xmax: 16656120 ymax: 8375779 CRS: +proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs First 10 features: iso_a3 name sovereignt continent 1 AFG Afghanistan Afghanistan Asia 2 AGO Angola Angola Africa 3 ALB Albania Albania Europe 4 ARE United Arab Emirates United Arab Emirates Asia 5 ARG Argentina Argentina South America 6 ARM Armenia Armenia Asia 7 ATA Antarctica Antarctica Antarctica 8 ATF Fr. S. Antarctic Lands France Seven seas (open ocean) 9 AUS Australia Australia Oceania 10 AUT Austria Austria Europe area pop_est pop_est_dens economy 1 652860.000 [km^2] 28400000 4.350090e+01 7. Least developed region 2 1246700.000 [km^2] 12799293 1.026654e+01 7. Least developed region 3 27400.000 [km^2] 3639453 1.328268e+02 6. Developing region 4 71252.172 [km^2] 4798491 6.734519e+01 6. Developing region 5 2736690.000 [km^2] 40913584 1.495003e+01 5. Emerging region: G20 6 28470.000 [km^2] 2967004 1.042151e+02 6. Developing region 7 12259213.973 [km^2] 3802 3.101341e-04 6. Developing region 8 7257.455 [km^2] 140 1.929051e-02 6. Developing region 9 7682300.000 [km^2] 21262641 2.767744e+00 2. Developed region: nonG7 10 82523.000 [km^2] 8210281 9.949082e+01 2. Developed region: nonG7 income_grp gdp_cap_est life_exp well_being footprint inequality 1 5. Low income 784.1549 59.668 3.8 0.79 0.42655744 2 3. Upper middle income 8617.6635 NA NA NA NA 3 4. Lower middle income 5992.6588 77.347 5.5 2.21 0.16513372 4 2. High income: nonOECD 38407.9078 NA NA NA NA 5 3. Upper middle income 14027.1261 75.927 6.5 3.14 0.16423830 6 4. Lower middle income 6326.2469 74.446 4.3 2.23 0.21664810 7 2. High income: nonOECD 200000.0000 NA NA NA NA 8 2. High income: nonOECD 114285.7143 NA NA NA NA 9 1. High income: OECD 37634.0832 82.052 7.2 9.31 0.08067825 10 1. High income: OECD 40132.6093 81.004 7.4 6.06 0.07129351 HPI geometry 1 20.22535 MULTIPOLYGON (((5310471 451... 2 NA MULTIPOLYGON (((1531585 -77... 3 36.76687 MULTIPOLYGON (((1729835 521... 4 NA MULTIPOLYGON (((4675864 313... 5 35.19024 MULTIPOLYGON (((-5017766 -6... 6 25.66642 MULTIPOLYGON (((3677241 513... 7 NA MULTIPOLYGON (((-3403793 -8... 8 NA MULTIPOLYGON (((5550200 -59... 9 21.22897 MULTIPOLYGON (((12288137 -5... 10 30.47822 MULTIPOLYGON (((1371802 588... ``` ] --- count: false ## Building a Simple Map .panel1-standard-user[ ```r library(tidyverse) library(tmap) ## We will be using data from "world" and "Metro" data(World, metro) ## We will filter the metro datasets down so that we only have ## incidences within Australia metro %>% filter(iso_a3 == "AUS") -> metro_australia ## Setting the mode as "Plot" will allow us to create a static map tmap_mode("plot") World %>% ## Here we will filter the "World" set down to just Australia as well * filter(name == "Australia") ``` ] .panel2-standard-user[ ``` Simple feature collection with 1 feature and 15 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: 10046010 ymin: -5409508 xmax: 13812740 ymax: -1401394 CRS: +proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs iso_a3 name sovereignt continent area pop_est pop_est_dens 1 AUS Australia Australia Oceania 7682300 [km^2] 21262641 2.767744 economy income_grp gdp_cap_est life_exp 1 2. Developed region: nonG7 1. High income: OECD 37634.08 82.052 well_being footprint inequality HPI geometry 1 7.2 9.31 0.08067825 21.22897 MULTIPOLYGON (((12288137 -5... ``` ] --- count: false ## Building a Simple Map .panel1-standard-user[ ```r library(tidyverse) library(tmap) ## We will be using data from "world" and "Metro" data(World, metro) ## We will filter the metro datasets down so that we only have ## incidences within Australia metro %>% filter(iso_a3 == "AUS") -> metro_australia ## Setting the mode as "Plot" will allow us to create a static map tmap_mode("plot") World %>% ## Here we will filter the "World" set down to just Australia as well filter(name == "Australia") %>% ## Here we decide the projection type and create polygon shape to ## represent the country * tm_shape(shp = ., * projection = "+proj=merc") + * tm_polygons("name", * legend.show = F) ``` ] .panel2-standard-user[ <img src="AUS_tmap_files/figure-html/standard_user_06_output-1.png" width="432" /> ] --- count: false ## Building a Simple Map .panel1-standard-user[ ```r library(tidyverse) library(tmap) ## We will be using data from "world" and "Metro" data(World, metro) ## We will filter the metro datasets down so that we only have ## incidences within Australia metro %>% filter(iso_a3 == "AUS") -> metro_australia ## Setting the mode as "Plot" will allow us to create a static map tmap_mode("plot") World %>% ## Here we will filter the "World" set down to just Australia as well filter(name == "Australia") %>% ## Here we decide the projection type and create polygon shape to ## represent the country tm_shape(shp = ., projection = "+proj=merc") + tm_polygons("name", legend.show = F) + ## Finally we add the datapoints representing cities in Australia * tm_shape(metro_australia) + * tm_symbols(col = "orange", * size = "pop2020", * scale = 1, * legend.size.show = T) ``` ] .panel2-standard-user[ <img src="AUS_tmap_files/figure-html/standard_user_07_output-1.png" width="432" /> ] <style> .panel1-standard-user { color: black; width: 49%; float: left; padding-left: 1%; font-size: 80% } .panel2-standard-user { color: black; width: 49%; float: left; padding-left: 1%; font-size: 80% } .panel3-standard-user { color: black; width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Transforming into an Interactive Map -- - Turning a static map into an interactive one is extremely easy with tmap. -- - Simply change the tmap_mode() from "plot" to "view", that's all there is to it. -- - After we've changed the map mode, you can go ahead and explore the new interactive map. Click-and-drag, scroll in-or-out, toggle layers, and click on data points for more information. --- count: false ## Transforming to Interactive .panel1-interactive-rotate[ ```r library(tidyverse) library(tmap) data(World, metro) metro_australia <- metro %>% filter(iso_a3 == "AUS") ## Swapping "plot" for "view" transforms the map from static ## to interactive *tmap_mode("plot") ## Simple as that. Try exploring the map now, drag, scroll, click World %>% filter(name == "Australia") %>% tm_shape(shp = ., projection = "+proj=merc") + tm_polygons("name", legend.show = F) + tm_shape(metro_australia) + tm_symbols(col = "orange", size = "pop2020", scale = 1, legend.size.show = T) ``` ] .panel2-interactive-rotate[ <img src="AUS_tmap_files/figure-html/interactive_rotate_01_output-1.png" width="432" /> ] --- count: false ## Transforming to Interactive .panel1-interactive-rotate[ ```r library(tidyverse) library(tmap) data(World, metro) metro_australia <- metro %>% filter(iso_a3 == "AUS") ## Swapping "plot" for "view" transforms the map from static ## to interactive *tmap_mode("view") ## Simple as that. Try exploring the map now, drag, scroll, click World %>% filter(name == "Australia") %>% tm_shape(shp = ., projection = "+proj=merc") + tm_polygons("name", legend.show = F) + tm_shape(metro_australia) + tm_symbols(col = "orange", size = "pop2020", scale = 1, legend.size.show = T) ``` ] .panel2-interactive-rotate[
] <style> .panel1-interactive-rotate { color: black; width: 49%; float: left; padding-left: 1%; font-size: 80% } .panel2-interactive-rotate { color: black; width: 49%; float: left; padding-left: 1%; font-size: 80% } .panel3-interactive-rotate { color: black; width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Bonus Content: Map Projections -- - By default, the projection type will be defined by the projection used by the shp object itself. However, its easy to manually switch out projections in tmap as an argument of tm_shape. -- - We will rotate through a few good projection types on our first static map. --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator * projection = "+proj=merc") + ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_01_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson * projection = "+proj=robin") + #ROTATE ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_02_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I * projection = "+proj=wag1") + #ROTATE ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_03_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine * projection = "+proj=igh") + #ROTATE ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_04_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular * projection = "+proj=nicol") + #ROTATE ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_05_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange * projection = "+proj=lagrng") + #ROTATE ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_06_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I * projection = "+proj=vandg") + #ROTATE ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_07_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator * projection = "+proj=tobmerc") + #ROTATE ## American Polyconic ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_08_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic * projection = "+proj=poly") + #ROTATE ## Orthographic ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_09_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic * projection = "+proj=ortho") + #ROTATE ## Lambert Equal Area Conic tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_10_output-1.png" width="432" /> ] --- count: false ## Projections .panel1-worldmap-rotate[ ```r library(tidyverse) library(tmap) data(World, rivers) tmap_mode("plot") World %>% tm_shape(shp = ., ## Mercator ## Robinson ## Wagner I ## Interrupted Goode Homolosine ## Nicolosi Globular ## Lagrange ## Van Der Grinten I ## Tobler-Mercator ## American Polyconic ## Orthographic ## Lambert Equal Area Conic * projection = "+proj=leac") + #ROTATE tm_polygons("name", legend.show = F) + tm_shape(rivers) + tm_lines("lightblue", lwd = "strokelwd", scale = 3, legend.lwd.show = F) ``` ] .panel2-worldmap-rotate[ <img src="AUS_tmap_files/figure-html/worldmap_rotate_11_output-1.png" width="432" /> ] <style> .panel1-worldmap-rotate { color: black; width: 49%; float: left; padding-left: 1%; font-size: 80% } .panel2-worldmap-rotate { color: black; width: 49%; float: left; padding-left: 1%; font-size: 80% } .panel3-worldmap-rotate { color: black; width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Additional Information & Options -- - Different projections are better suited to different maps. -- - For example, if producing a map of Alaska then the "Alaska Grid" or "Alaska Series E" projection type would be typical. -- - When presenting a large region or the world at large, however, "Mercator" or "Robinson" are more reliable. -- - Find more projection types at: https://proj.org/operations/projections/index.html