r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
868 stars 121 forks source link

Reversing color palettes #964

Closed hagertynw closed 2 weeks ago

hagertynw commented 2 weeks ago

In tmap v4, it would be very helpful to be able to reverse the values of a color palette. I thought it could be done with a negative sign in front of the palette name, but it doesn't seem to work. These two lines produce identical maps for me:

tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "br_bg"))
tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "-br_bg"))

I couldn't find another argument in the documentation that would accomplish this. If there is another way to do it that I missed please let me know. Thanks!

Rapsodia86 commented 2 weeks ago

Which version do you have? Mine is on Win10:

> packageVersion("tmap")
[1] ‘3.99.9002’

and it works for me: tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "br_bg")) image tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "-br_bg")) image

hagertynw commented 2 weeks ago
> packageVersion("tmap")
[1] ‘3.99.9003’

on Win10 Enterprise.

Do you know how to install an earlier version from Github?

ar-puuk commented 2 weeks ago

This should do that:

remotes::install_github("r-tmap/tmap@4b394a317de56c2c4605379cd958954246cb5860")
hagertynw commented 2 weeks ago

Thank you. I used that link to install tmap 3.99.9002, updated all package dependencies, and started a new session in which I loaded nothing besides tmap. It still doesn't work. Both lines of code above produce the first map, in which Russia is teal. I also tried tmap_save in case it was an issue with the RStudio viewer, and the output images are also identical.

In view mode I get this error message, in case it is a clue. I don't think I understand enough of how the color values are generated to interpret it. Appreciate the help.


> ttm()
ℹ tmap mode set to "view".
> tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "-br_bg"))
Registered S3 method overwritten by 'jsonlite':
  method     from   
  print.json jsonify
Warning message:
In checkDim(x, data) :
  Length of color vector does not match number of data rows.
  The vector is repeated to match the number of rows.
> tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "br_bg"))
Warning message:
In checkDim(x, data) :
  Length of color vector does not match number of data rows.
  The vector is repeated to match the number of rows.
ar-puuk commented 2 weeks ago

I am sorry, I am not skilled enough to fix the issue. But I checked and found that a commit seems to have broken it.

This should fix your issue for now.

## install the working version of the tmap package
# remotes::install_github("r-tmap/tmap@05bd6c605123cbc773cc09a86b39d60540f51163") # does not work
remotes::install_github("r-tmap/tmap@4b754be3143bbe0819920e28a3bfb227713638f8") # works
library(tmap)
#> 
#> Attaching package: 'tmap'
#> The following object is masked from 'package:datasets':
#> 
#>     rivers

packageVersion("tmap")
#> [1] '3.99.9002'

tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "br_bg"))
#> Multiple palettes called "br_bg" found: "brewer.br_bg", "matplotlib.br_bg". The first one, "brewer.br_bg", is returned.


tm_shape(World) + tm_polygons(fill = "area", fill.scale = tm_scale(values = "-br_bg"))
#> Multiple palettes called "br_bg" found: "brewer.br_bg", "matplotlib.br_bg". The first one, "brewer.br_bg", is returned.

Created on 2024-11-12 with reprex v2.1.1

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.4.2 (2024-10-31 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz America/New_York #> date 2024-11-12 #> pandoc 3.5 @ C:/PROGRA~1/Pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> 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.3.0) #> class 7.3-22 2023-05-03 [2] CRAN (R 4.4.2) #> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.3.1) #> cli 3.6.3 2024-06-21 [1] CRAN (R 4.4.1) #> codetools 0.2-20 2024-03-31 [1] CRAN (R 4.3.3) #> colorspace 2.1-1 2023-07-13 [1] R-Forge (R 4.3.1) #> cols4all 0.8 2024-10-16 [1] CRAN (R 4.4.1) #> crosstalk 1.2.1 2023-11-23 [1] CRAN (R 4.3.2) #> curl 6.0.1 2024-11-12 [1] https://ar-puuk.r-universe.dev (R 4.4.2) #> data.table 1.16.99 2024-09-25 [1] https://ar-puuk.r-universe.dev (R 4.4.1) #> DBI 1.2.3.9015 2024-11-11 [1] https://ar-puuk.r-universe.dev (R 4.4.2) #> dichromat 2.0-1 2016-12-23 [1] R-Forge (R 4.3.1) #> 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) #> evaluate 1.0.1 2024-10-10 [1] CRAN (R 4.4.1) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.4.0) #> fs 1.6.5 2024-10-30 [1] CRAN (R 4.4.1) #> glue 1.8.0 2024-09-30 [1] CRAN (R 4.4.1) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.3) #> htmlwidgets 1.6.4 2023-12-06 [1] CRAN (R 4.3.2) #> KernSmooth 2.23-24 2024-05-17 [1] CRAN (R 4.4.0) #> knitr 1.49 2024-11-08 [1] CRAN (R 4.4.2) #> lattice 0.22-6 2024-03-20 [1] CRAN (R 4.3.3) #> leafem 0.2.3 2023-09-17 [1] CRAN (R 4.3.1) #> leaflegend 1.2.1 2024-05-09 [1] CRAN (R 4.4.0) #> leaflet 2.2.2 2024-03-26 [1] CRAN (R 4.3.3) #> leaflet.providers 2.0.0 2023-10-17 [1] CRAN (R 4.3.1) #> leafsync 0.1.0 2019-03-05 [1] CRAN (R 4.3.1) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) #> lwgeom 0.2-14 2024-02-21 [1] CRAN (R 4.3.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.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.3.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.1) #> raster 3.6-30 2024-10-02 [1] CRAN (R 4.4.1) #> RColorBrewer 1.1-3 2022-04-03 [1] CRAN (R 4.3.0) #> Rcpp 1.0.13-1 2024-11-02 [1] CRAN (R 4.4.2) #> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.4.1) #> rlang 1.1.4 2024-06-04 [1] CRAN (R 4.4.0) #> rmarkdown 2.29 2024-11-04 [1] CRAN (R 4.4.2) #> rstudioapi 0.17.1 2024-10-22 [1] CRAN (R 4.4.1) #> s2 1.1.7 2024-07-17 [1] CRAN (R 4.4.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1) #> sf 1.0-19 2024-11-11 [1] Github (r-spatial/sf@7dd4173) #> sp 2.1-4 2024-04-30 [1] CRAN (R 4.3.3) #> spacesXYZ 1.3-0 2024-01-23 [1] CRAN (R 4.3.3) #> stars 0.6-7 2024-11-07 [1] CRAN (R 4.4.2) #> terra 1.7-83 2024-10-14 [1] CRAN (R 4.4.1) #> tmap * 3.99.9002 2024-11-13 [1] Github (r-tmap/tmap@4b754be) #> tmaptools 3.1-1 2024-09-10 [1] Github (mtennekes/tmaptools@7e42da8) #> units 0.8-5 2023-11-28 [1] CRAN (R 4.3.2) #> viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.3.1) #> withr 3.0.2 2024-10-28 [1] CRAN (R 4.4.1) #> wk 0.9.4 2024-10-11 [1] CRAN (R 4.4.1) #> xfun 0.49 2024-10-31 [1] CRAN (R 4.4.1) #> XML 3.99-0.17 2024-06-25 [1] CRAN (R 4.4.1) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.3.1) #> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.4.1) #> #> [1] C:/Users/pukar/AppData/Local/R/win-library/4.4 #> [2] C:/Program Files/R/R-4.4.2/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
hagertynw commented 2 weeks ago

Thank you! That solved my immediate problem.

mtennekes commented 2 weeks ago

Thx for this bug report @hagertynw Now fixed in the latest dev version.