Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advice on reproducible map_coloring? #32

Open
MatthieuStigler opened this issue Feb 15, 2022 · 2 comments
Open

Advice on reproducible map_coloring? #32

MatthieuStigler opened this issue Feb 15, 2022 · 2 comments

Comments

@MatthieuStigler
Copy link

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
@mtennekes
Copy link
Member

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

@MatthieuStigler
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants