tmaptools
tmaptools copied to clipboard
Advice on reproducible map_coloring?
Hi
I was wondering whether it is possible to have a replicable output with map_coloring, i.e. that is constant across calls? This is particularly useful when one is trying to use testhat snapshots.
I realize that setting minimize=TRUE avoids the call to sample(), so that is already a workaround. Should one want more colors than the strict minimum, the output seems to change each time. Do you see an easy way to add maybe a seed argument?
Thanks!
library(tmaptools)
packageVersion("tmap")
#> [1] '3.3.2'
packageVersion("tmaptools")
#> [1] '3.1.1'
library(tmap)
data(World)
color <- map_coloring(World, palette="Pastel2")
color2 <- map_coloring(World, palette="Pastel2")
all.equal(color, color2)
#> [1] "152 string mismatches"
color <- map_coloring(World, palette="Pastel2", minimize=TRUE)
color2 <- map_coloring(World, palette="Pastel2", minimize=TRUE)
all.equal(color, color2)
#> [1] TRUE
As a solution/workaround, the user can also set the seed:
set.seed(123)
color <- map_coloring(World, palette="Pastel2")
set.seed(123)
color2 <- map_coloring(World, palette="Pastel2")
all.equal(color, color2)
#> [1] TRUE
strange, I had tried this, but if doesn't work for me...
library(tmaptools)
packageVersion("tmap")
#> [1] '3.3.2'
packageVersion("tmaptools")
#> [1] '3.1.1'
library(tmap)
data(World)
set.seed(123)
color <- map_coloring(World, palette="Pastel2")
set.seed(123)
color2 <- map_coloring(World, palette="Pastel2")
head(color)
#> [1] "#F1E2CC" "#FDCDAC" "#FFF2AE" "#FDCDAC" "#F4CAE4" "#B3E2CD"
head(color2)
#> [1] "#FFF2AE" "#F4CAE4" "#B3E2CD" "#FDCDAC" "#B3E2CD" "#FFF2AE"
all.equal(color, color2)
#> [1] "161 string mismatches"
Created on 2022-02-24 by the reprex package (v2.0.1)