@@ -17,85 +17,57 @@ tmap_style_catalogue <- function(path="./tmap_style_previews", styles=NA) {
1717 if (! file.exists(path )) dir.create(path , recursive = TRUE )
1818
1919 # load and process data
20- World <- metro <- World_rivers <- land <- NULL
21- data(World , metro , World_rivers , land , envir = environment())
22- metro $ growth <- (metro $ pop2020 - metro $ pop2010 ) / (metro $ pop2010 * 10 ) * 100
20+ pb <- txtProgressBar()
2321
24- rivers <- sf :: st_transform(World_rivers , st_crs(World ))
22+ tm1 = tm_shape(NLD_prov ) +
23+ tm_polygons() +
24+ tm_title_out(" Constant polygon fill" )
25+
26+ NLD_muni $ letter = factor (substr(NLD_muni $ name , 1 , 1 ))
27+ tm2 = tm_shape(NLD_muni ) +
28+ tm_polygons(lwd = .5 , fill = " letter" , fill.scale = tm_scale_categorical(n.max = 7 )) +
29+ tm_shape(NLD_prov ) +
30+ tm_borders(lwd = 2 ) +
31+ tm_title_out(" Categorical color scale" ) +
32+ tm_compass() +
33+ tm_scalebar()
34+
35+
36+ tm3 = tm_shape(NLD_muni ) +
37+ tm_polygons(lwd = 0.5 , fill = " income_high" ) +
38+ tm_shape(NLD_prov ) +
39+ tm_borders(lwd = 2 ) +
40+ tm_title_out(" Sequential color scale" )
41+
42+ tm4 = tm_shape(NLD_muni ) +
43+ tm_polygons(lwd = 0.5 , fill = " urbanity" , fill.scale = tm_scale_categorical(values = " div" )) +
44+ tm_shape(NLD_prov ) +
45+ tm_borders(lwd = 2 ) +
46+ tm_title_out(" Diverging color scale" )
2547
26- pb <- txtProgressBar()
2748
2849 print_style <- function (style , i ) {
2950 on.exit(dev.off())
30- png(file.path(path , paste0(style , " .png" )), width = 1920 , height = 1080 )
51+ png(file.path(path , paste0(style , " .png" )), width = 1920 , height = 1920 )
3152 grid.newpage()
32- pushViewport(viewport(layout = grid.layout(3 , 3 )))
53+ pushViewport(viewport(layout = grid.layout(2 , 2 )))
3354
3455 # set progress bar init
3556 pbi <- (i - 1 )/ ns
36- pbii <- function (i ) pbi + (i / 9 ) * (1 / ns )
37-
38- # first column
39- print(tm_shape(World ) +
40- tm_polygons() +
41- tm_text(" iso_a3" , size = " AREA" ) +
42- tm_symbols() +
43- tm_shape(World_rivers ) +
44- tm_lines() +
45- tm_compass() +
46- tm_scale_bar() +
47- tm_style(style ) + tm_format(" World" , title = " Fixed aesthetics" ),
57+ pbii <- function (i ) pbi + (i / 4 ) * (1 / ns )
58+
59+ print(tm1 + tm_style(style ) + tm_layout(scale = 2 ),
4860 vp = viewport(layout.pos.row = 1 , layout.pos.col = 1 ))
4961 setTxtProgressBar(pb , pbii(1 ))
50- print(tm_shape(World ) +
51- tm_borders() +
52- tm_text(" name" , size = " AREA" ) +
53- tm_style(style ) + tm_format(" World" , title = " Polygon borders with text" ),
54- vp = viewport(layout.pos.row = 2 , layout.pos.col = 1 ))
62+ print(tm2 + tm_style(style ) + tm_layout(scale = 2 ),
63+ vp = viewport(layout.pos.row = 1 , layout.pos.col = 2 ))
5564 setTxtProgressBar(pb , pbii(2 ))
56- print(tm_shape(World_rivers , bbox = bb(World )) +
57- tm_lines() +
58- tm_style(style ) + tm_format(" World" , title = " Lines only" ),
59- vp = viewport(layout.pos.row = 3 , layout.pos.col = 1 ))
65+ print(tm3 + tm_style(style ) + tm_layout(scale = 2 ),
66+ vp = viewport(layout.pos.row = 2 , layout.pos.col = 1 ))
6067 setTxtProgressBar(pb , pbii(3 ))
61-
62- # second column
63- print(tm_shape(World ) +
64- tm_polygons(" MAP_COLORS" , ncols = 5 ) +
65- tm_style(style ) + tm_format(" World" , title = " Map coloring" ),
66- vp = viewport(layout.pos.row = 1 , layout.pos.col = 2 ))
67- setTxtProgressBar(pb , pbii(4 ))
68- print(tm_shape(World ) +
69- tm_polygons() +
70- tm_shape(metro ) +
71- tm_dots() +
72- tm_grid(projection = 4326 ) +
73- tm_style(style ) + tm_format(" World" , title = " Dot map" ),
68+ print(tm4 + tm_style(style ) + tm_layout(scale = 2 ),
7469 vp = viewport(layout.pos.row = 2 , layout.pos.col = 2 ))
75- setTxtProgressBar(pb , pbii(5 ))
76- print(tm_shape(World ) +
77- tm_polygons() +
78- tm_shape(metro ) +
79- tm_symbols(size = " pop2010" , col = " growth" , breaks = c(- Inf , - 2 , - 1 , - .5 , .5 , 1 , 2 , Inf ), midpoint = 0 ) + tm_style(style ) + tm_format(" World" , title = " symbol map" ),
80- vp = viewport(layout.pos.row = 3 , layout.pos.col = 2 ))
81- setTxtProgressBar(pb , pbii(6 ))
82-
83- # third column
84- print(tm_shape(World ) +
85- tm_polygons(" gdp_cap_est" , breaks = c(0 , 10000 , 20000 , 30000 , 40000 , 50000 , Inf )) +
86- tm_text(" iso_a3" , size = " AREA" ) + tm_style(style ) + tm_format(" World" , title = " Choropleth" ),
87- vp = viewport(layout.pos.row = 1 , layout.pos.col = 3 ))
88- setTxtProgressBar(pb , pbii(7 ))
89- print(tm_shape(World ) +
90- tm_polygons(" HPI" , palette = " div" , n = 9 ) +
91- tm_text(" iso_a3" , size = " AREA" ) + tm_style(style ) + tm_format(" World" , title = " Choropleth (diverging)" ),
92- vp = viewport(layout.pos.row = 2 , layout.pos.col = 3 ))
93- setTxtProgressBar(pb , pbii(8 ))
94- print(tm_shape(World ) +
95- tm_polygons(" economy" ) +
96- tm_text(" iso_a3" , size = " AREA" ) + tm_style(style ) + tm_format(" World" , title = " Categorical map" ),
97- vp = viewport(layout.pos.row = 3 , layout.pos.col = 3 ))
98- setTxtProgressBar(pb , pbii(9 ))
70+ setTxtProgressBar(pb , pbii(4 ))
9971 upViewport()
10072 }
10173
0 commit comments