R tmap ::使标题成为图例的一部分

时间:2019-02-05 09:14:23

标签: r legend subtitle tmap

在下面的示例中:如何使标题“主标题”成为图例的一部分? col和size的标题应保持不变,并用作字幕。

据我了解的文档,它应该可以设置title.snap.to.legend = TRUE,但是不起作用。最小示例:

library(tmap)
data(World, metro) 

tm_shape(World) + tm_fill()+
  tm_shape(metro) + 
  tm_symbols(
    size = "pop2020",
    col = "pop2020",
    title.size = "title 1", 
    title.col =  "title 2", 
    legend.size.is.portrait=TRUE) +
  tm_layout(legend.bg.color = "gray", 
            legend.frame = "black", 
            title="Main title", 
            title.snap.to.legend = TRUE)

给出: enter image description here

我希望将主标题放置在图例框架内的图例元素上方。

1 个答案:

答案 0 :(得分:0)

不知道为什么它不起作用,但是下面是一个草率的,快速的技巧,在标题前面添加了几行新的\n,直到将其推到图例顶部为止。其他人可能会给出更好的答案。这可能是一个问题,因此请考虑打开一个here

library(tmap)
#> Warning: package 'tmap' was built under R version 3.5.2
data(World, metro) 

tm_shape(World) + tm_fill()+
  tm_shape(metro) + 
  tm_symbols(
    size = "pop2020",
    col = "pop2020",
    title.size = "title 1", 
    title.col =  "title 2", 
    legend.size.is.portrait=TRUE) +
  tm_layout(legend.bg.color = "gray", 
            legend.frame = "black", 
            title="\n\n\n Main title") # add \n to push the title down

reprex package(v0.2.1)于2019-02-06创建

devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  os       Windows 7 x64 SP 1          
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252              
#>  date     2019-02-06                  
#> 
#> - Packages --------------------------------------------------------------
#>  package      * version   date       lib source        
#>  assertthat     0.2.0     2017-04-11 [1] CRAN (R 3.5.1)
#>  backports      1.1.2     2017-12-13 [1] CRAN (R 3.5.0)
#>  base64enc      0.1-3     2015-07-28 [1] CRAN (R 3.5.0)
#>  callr          3.0.0     2018-08-24 [1] CRAN (R 3.5.1)
#>  class          7.3-14    2015-08-30 [2] CRAN (R 3.5.1)
#>  classInt       0.2-3     2018-04-16 [1] CRAN (R 3.5.1)
#>  cli            1.0.1     2018-09-25 [1] CRAN (R 3.5.1)
#>  codetools      0.2-15    2016-10-05 [2] CRAN (R 3.5.1)
#>  colorspace     1.3-2     2016-12-14 [1] CRAN (R 3.5.1)
#>  crayon         1.3.4     2017-09-16 [1] CRAN (R 3.5.1)
#>  crosstalk      1.0.0     2016-12-21 [1] CRAN (R 3.5.1)
#>  curl           3.2       2018-03-28 [1] CRAN (R 3.5.1)
#>  DBI            1.0.0     2018-05-02 [1] CRAN (R 3.5.1)
#>  desc           1.2.0     2018-05-01 [1] CRAN (R 3.5.1)
#>  devtools       2.0.1     2018-10-26 [1] CRAN (R 3.5.1)
#>  dichromat      2.0-0     2013-01-24 [1] CRAN (R 3.5.0)
#>  digest         0.6.18    2018-10-10 [1] CRAN (R 3.5.1)
#>  e1071          1.7-0     2018-07-28 [1] CRAN (R 3.5.1)
#>  evaluate       0.12      2018-10-09 [1] CRAN (R 3.5.1)
#>  fs             1.2.6     2018-08-23 [1] CRAN (R 3.5.1)
#>  glue           1.3.0     2018-07-17 [1] CRAN (R 3.5.1)
#>  htmltools      0.3.6     2017-04-28 [1] CRAN (R 3.5.1)
#>  htmlwidgets    1.3       2018-09-30 [1] CRAN (R 3.5.1)
#>  httpuv         1.4.5     2018-07-19 [1] CRAN (R 3.5.1)
#>  httr           1.3.1     2017-08-20 [1] CRAN (R 3.5.1)
#>  KernSmooth     2.23-15   2015-06-29 [2] CRAN (R 3.5.1)
#>  knitr          1.20      2018-02-20 [1] CRAN (R 3.5.1)
#>  later          0.7.5     2018-09-18 [1] CRAN (R 3.5.1)
#>  lattice        0.20-35   2017-03-25 [2] CRAN (R 3.5.1)
#>  leaflet        2.0.2     2018-08-27 [1] CRAN (R 3.5.1)
#>  lwgeom         0.1-5     2018-12-07 [1] CRAN (R 3.5.1)
#>  magrittr       1.5       2014-11-22 [1] CRAN (R 3.5.1)
#>  mapview        2.6.0     2018-09-16 [1] CRAN (R 3.5.1)
#>  memoise        1.1.0     2017-04-21 [1] CRAN (R 3.5.1)
#>  mime           0.6       2018-10-05 [1] CRAN (R 3.5.1)
#>  munsell        0.5.0     2018-06-12 [1] CRAN (R 3.5.1)
#>  pkgbuild       1.0.2     2018-10-16 [1] CRAN (R 3.5.1)
#>  pkgload        1.0.2     2018-10-29 [1] CRAN (R 3.5.1)
#>  png            0.1-7     2013-12-03 [1] CRAN (R 3.5.0)
#>  prettyunits    1.0.2     2015-07-13 [1] CRAN (R 3.5.1)
#>  processx       3.2.0     2018-08-16 [1] CRAN (R 3.5.1)
#>  promises       1.0.1     2018-04-13 [1] CRAN (R 3.5.1)
#>  ps             1.2.1     2018-11-06 [1] CRAN (R 3.5.1)
#>  R6             2.3.0     2018-10-04 [1] CRAN (R 3.5.1)
#>  raster         2.8-4     2018-11-03 [1] CRAN (R 3.5.1)
#>  RColorBrewer   1.1-2     2014-12-07 [1] CRAN (R 3.5.0)
#>  Rcpp           1.0.0     2018-11-07 [1] CRAN (R 3.5.1)
#>  remotes        2.0.2     2018-10-30 [1] CRAN (R 3.5.1)
#>  rgdal          1.3-6     2018-10-16 [1] CRAN (R 3.5.1)
#>  rgeos          0.4-2     2018-11-08 [1] CRAN (R 3.5.1)
#>  rlang          0.3.0.1   2018-10-25 [1] CRAN (R 3.5.1)
#>  rmarkdown      1.10      2018-06-11 [1] CRAN (R 3.5.1)
#>  rprojroot      1.3-2     2018-01-03 [1] CRAN (R 3.5.1)
#>  satellite      1.0.1     2017-10-18 [1] CRAN (R 3.5.1)
#>  scales         1.0.0     2018-08-09 [1] CRAN (R 3.5.1)
#>  sessioninfo    1.1.1     2018-11-05 [1] CRAN (R 3.5.1)
#>  sf             0.7-1     2018-10-24 [1] CRAN (R 3.5.1)
#>  shiny          1.2.0     2018-11-02 [1] CRAN (R 3.5.1)
#>  sp             1.3-1     2018-06-05 [1] CRAN (R 3.5.1)
#>  spData         0.2.9.4   2018-09-15 [1] CRAN (R 3.5.1)
#>  stringi        1.2.4     2018-07-20 [1] CRAN (R 3.5.1)
#>  stringr        1.3.1     2018-05-10 [1] CRAN (R 3.5.1)
#>  tmap         * 2.2       2019-01-05 [1] CRAN (R 3.5.2)
#>  tmaptools      2.0-1     2018-08-07 [1] CRAN (R 3.5.1)
#>  units          0.6-1     2018-09-21 [1] CRAN (R 3.5.1)
#>  usethis        1.4.0     2018-08-14 [1] CRAN (R 3.5.1)
#>  viridisLite    0.3.0     2018-02-01 [1] CRAN (R 3.5.1)
#>  webshot        0.5.1     2018-09-28 [1] CRAN (R 3.5.1)
#>  withr          2.1.2     2018-03-15 [1] CRAN (R 3.5.1)
#>  XML            3.98-1.16 2018-08-19 [1] CRAN (R 3.5.1)
#>  xml2           1.2.0     2018-01-24 [1] CRAN (R 3.5.1)
#>  xtable         1.8-3     2018-08-29 [1] CRAN (R 3.5.1)
#>  yaml           2.2.0     2018-07-25 [1] CRAN (R 3.5.1)