这是indexloc提供的服务,不要输入任何密码
Skip to content

get_asp_ratio ignores outside map components #39

@johnbaums

Description

@johnbaums

Is it intentional that get_asp_ratio calculates the ratio based only on the map region, and ignores legends, facet titles, map titles, etc. that are placed outside? Is it possible to include those components in the calculation of aspect ratio?

Consider the following examples:

library(tmap)
data(land, World)
p <- tm_shape(land) + tm_raster("cover") + tm_layout(outer.bg.color = "mistyrose")
asp <- tmaptools::get_asp_ratio(p) # asp = 2

With the calculated aspect ratio of 2:1, there is excessive white space around the map.

tmap_save(p, filename = "test.png", width = 8, height = 8/asp, dpi = 300)

Image

With the legend suppressed, the aspect ratio of 2:1 is perfect.

tmap_save(
  p + tm_layout(legend.show = FALSE), 
  filename = "test-noLegend.png", width = 8, height = 8/asp, dpi = 300
)

Image

We can use asp = 0 to stretch the map frame out to the edges of the image, but this causes excessive margins within the map frame, and leads to a suboptimal legend layout.

tmap_save(p, filename = "test-asp0.png", width = 8, height = 8/asp, dpi = 300, asp=0)

Image

Session Info
sessioninfo::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.1 (2024-06-14)
 os       macOS Sonoma 14.7.4
 system   x86_64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 date     2025-03-21
 pandoc   3.6.2 @ /usr/local/bin/pandocPackages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package           * version   date (UTC) lib source
 abind               1.4-8     2024-09-12 [1] CRAN (R 4.4.1)
 base64enc           0.1-3     2015-07-28 [1] CRAN (R 4.4.0)
 class               7.3-22    2023-05-03 [2] CRAN (R 4.4.1)
 classInt            0.4-10    2023-09-05 [1] CRAN (R 4.4.1)
 cli                 3.6.3     2024-06-21 [1] CRAN (R 4.4.0)
 codetools           0.2-20    2024-03-31 [2] CRAN (R 4.4.1)
 colorspace          2.1-1     2024-07-26 [1] CRAN (R 4.4.0)
 cols4all            0.8       2024-10-16 [1] CRAN (R 4.4.1)
 crosstalk           1.2.1     2023-11-23 [1] CRAN (R 4.4.0)
 data.table          1.16.2    2024-10-10 [1] CRAN (R 4.4.1)
 DBI                 1.2.3     2024-06-02 [1] CRAN (R 4.4.1)
 dichromat           2.0-0.1   2022-05-02 [1] CRAN (R 4.4.0)
 digest              0.6.37    2024-08-19 [1] CRAN (R 4.4.1)
 e1071               1.7-16    2024-09-16 [1] CRAN (R 4.4.1)
 fastmap             1.2.0     2024-05-15 [1] CRAN (R 4.4.0)
 htmltools           0.5.8.1   2024-04-04 [1] CRAN (R 4.4.0)
 htmlwidgets         1.6.4     2023-12-06 [1] CRAN (R 4.4.0)
 KernSmooth          2.23-24   2024-05-17 [2] CRAN (R 4.4.1)
 lattice             0.22-6    2024-03-20 [2] CRAN (R 4.4.1)
 leafem              0.2.3     2023-09-17 [1] CRAN (R 4.4.0)
 leaflegend          1.2.1     2024-05-09 [1] CRAN (R 4.4.0)
 leaflet             2.2.2     2024-03-26 [1] CRAN (R 4.4.0)
 leaflet.providers   2.0.0     2023-10-17 [1] CRAN (R 4.4.0)
 leafsync            0.1.0     2019-03-05 [1] CRAN (R 4.4.0)
 lwgeom              0.2-14    2024-02-21 [1] CRAN (R 4.4.0)
 magrittr            2.0.3     2022-03-30 [1] CRAN (R 4.4.1)
 microbenchmark      1.5.0     2024-09-04 [1] CRAN (R 4.4.1)
 png                 0.1-8     2022-11-29 [1] CRAN (R 4.4.0)
 proxy               0.4-27    2022-06-09 [1] CRAN (R 4.4.1)
 R6                  2.5.1     2021-08-19 [1] CRAN (R 4.4.0)
 raster              3.6-30    2024-10-02 [1] CRAN (R 4.4.1)
 RColorBrewer        1.1-3     2022-04-03 [1] CRAN (R 4.4.0)
 Rcpp                1.0.13-1  2024-11-02 [1] CRAN (R 4.4.1)
 rlang               1.1.4     2024-06-04 [1] CRAN (R 4.4.0)
 s2                  1.1.7     2024-07-17 [1] CRAN (R 4.4.1)
 sessioninfo         1.2.2     2021-12-06 [1] CRAN (R 4.4.0)
 sf                  1.0-19    2024-11-05 [1] CRAN (R 4.4.1)
 sp                  2.1-4     2024-04-30 [1] CRAN (R 4.4.0)
 spacesXYZ           1.3-0     2024-01-23 [1] CRAN (R 4.4.0)
 stars               0.6-6     2024-07-16 [1] CRAN (R 4.4.0)
 terra               1.8-5     2024-12-12 [1] CRAN (R 4.4.1)
 tmap              * 4.0       2025-01-27 [1] CRAN (R 4.4.1)
 tmaptools           3.2       2025-01-13 [1] CRAN (R 4.4.1)
 units               0.8-5     2023-11-28 [1] CRAN (R 4.4.1)
 viridisLite         0.4.2     2023-05-02 [1] CRAN (R 4.4.0)
 wk                  0.9.3     2024-09-06 [1] CRAN (R 4.4.1)
 XML                 3.99-0.17 2024-06-25 [1] CRAN (R 4.4.0)

 [1] /Users/me/Library/R/x86_64/4.4/library
 [2] /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions