Skip to content

Commit

Permalink
palette names v3 -> v4 transition #950
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Nov 7, 2024
1 parent 4b754be commit 05bd6c6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
39 changes: 27 additions & 12 deletions R/messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,60 @@ message_reg = function(id) {

message_comp_scale = function() {
if (!message_thrown("comp_scale")) {
message("[plot mode] fit legend/component: Some legend items or map compoments do not fit well, and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable rescaling.")
cli::cli_inform("{.field [plot mode]} fit legend/component: Some legend items or map compoments do not fit well, and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable rescaling.",
.frequency_id = "comp_scale")
message_reg("comp_scale")
}
NULL
}

message_comp_high_wide = function(stack) {
if (!message_thrown("comp_scale")) {
message("[plot mode] legend/component: Some components or legends are too ", ifelse(stack == "vertical", "high", "wide"), " and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable rescaling.")
cli::cli_inform("{.field [plot mode]} legend/component: Some components or legends are too {.val ifelse(stack == 'vertical', 'high', 'wide')} and are therefore rescaled. Set the tmap option {.code component.autoscale} to {.code FALSE} to disable rescaling.",
.frequency_id = "comp_scale")
message_reg("comp_scale")
}
NULL
}

message_c4a = function(old_palette_name, info) {
message_c4a = function(old_palette_name, info, fullname = FALSE) {
new1 = info$fullname
new2 = info$name
mess = paste0("c4a_", old_palette_name)

if (!message_thrown(mess)) {
message(paste0("[cols4all] color palettes: use palettes from the R package cols4all. Run 'cols4all::c4a_gui()' to explore them. The old palette name \"", old_palette_name, "\" is named \"", new2, "\" (in long format \"", new1, "\")"))
message_reg(mess)
}
if (fullname) {
cli::cli_inform(
"{.field [cols4all]} color palettes: use palettes from the R package cols4all. Run {.code cols4all::c4a_gui()} to explore them. The old palette name {.str {old_palette_name}} is named {.str {new1}}",
.frequency_id = "cols4all"
)
} else {
cli::cli_inform(
"{.field [cols4all]} color palettes: use palettes from the R package cols4all. Run {.code cols4all::c4a_gui()} to explore them. The old palette name {.str {old_palette_name}} is named {.str {new2}} (in long format {.str {new1}})",
.frequency_id = "cols4all"
)

}
message_reg(mess)
}
}

message_nothing_to_show = function(any_groups) {
if (any_groups) {
message("[nothing to show] no data layers defined after tm_shape")
cli::cli_inform("{.field [nothing to show]} no data layers defined after {.code tm_shape}",
.frequency_id = "nothing")
} else {
message("[nothing to show] no layers defined")
cli::cli_inform("{.field [nothing to show]} no data layers defined",
.frequency_id = "nothing")
}
NULL
}

message_wrapstack = function(horizontal = TRUE) {
if (horizontal) {
message("[facets] use tm_facets_hstack() instead of tm_facets_wrap() to put the legends next to and aligned with the facets")
cli::cli_inform("{.field [facets]} use {.code tm_facets_hstack()} instead of {.code tm_facets_wrap()} to put the legends next to and aligned with the facets")
} else {
message("[facets] use tm_facets_vstack() instead of tm_facets_wrap() to put the legends next to and aligned with the facets")
cli::cli_inform("{.field [facets]} use {.code tm_facets_vstack()} instead of {.code tm_facets_wrap()} to put the legends next to and aligned with the facets")
}
NULL
}
Expand All @@ -61,10 +76,10 @@ message_pos_auto = function(type) {
if (!message_thrown("pos_auto")) {
fun = if (type == "autoout") "tm_pos_auto_out()" else "tm_pos_auto_in()"
fun2 = if (type == "autoout") "tm_pos_out()" else "tm_pos_in()"
message("[position] ", "use ", fun2, " instead of ", fun, ". The latter should be used with tmap_options().")
cli::cli_inform("{.field [position]} use {.val fun2} instead of {.val fun}. The latter should be used with {.code tmap_options()}.")
message_reg("pos_auto")
}
NULL
NULL
}

error_dimvalues = function() {
Expand Down
2 changes: 1 addition & 1 deletion R/tmapScale_defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ tmapValuesVV_fill = function(x, value.na, isdiv, n, dvalues, are_breaks, midpoin

if (!is.null(m)) {
if (x[1] != tolower(x[1])) message_c4a(x[1], info = m)
vvalues = getPal(x, n = ntot, range = range)[ids]
vvalues = getPal(m$fullname, n = ntot, range = range)[ids]
} else {
pal =colorRampPalette(x)
if (is.na(range[1])) range = c(0, 1)
Expand Down
29 changes: 28 additions & 1 deletion R/tmap_palettes.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#rm = rownames(tmaptools::tmap.pal.info)
#dput(sapply(rm, cols4all:::c4a_name_convert))

pals_v3 = c(BrBG = "brewer.br_bg", PiYG = "brewer.pi_yg", PRGn = "brewer.prgn",
PuOr = "brewer.pu_or", RdBu = "brewer.rd_bu", RdGy = "brewer.rd_gy",
RdYlBu = "brewer.rd_yl_bu", RdYlGn = "brewer.rd_yl_gn", Spectral = "brewer.spectral",
Accent = "brewer.accent", Dark2 = "brewer.dark2", Paired = "brewer.paired",
Pastel1 = "brewer.pastel1", Pastel2 = "brewer.pastel2", Set1 = "brewer.set1",
Set2 = "brewer.set2", Set3 = "brewer.set3", Blues = "brewer.blues",
BuGn = "brewer.bu_gn", BuPu = "brewer.bu_pu", GnBu = "brewer.gn_bu",
Greens = "brewer.greens", Greys = "brewer.greys", Oranges = "brewer.oranges",
OrRd = "brewer.or_rd", PuBu = "brewer.pu_bu", PuBuGn = "brewer.pu_bu_gn",
PuRd = "brewer.pu_rd", Purples = "brewer.purples", RdPu = "brewer.rd_pu",
Reds = "brewer.reds", YlGn = "brewer.yl_gn", YlGnBu = "brewer.yl_gn_bu",
YlOrBr = "brewer.yl_or_br", YlOrRd = "brewer.yl_or_rd", viridis = "matplotlib.viridis",
magma = "matplotlib.magma", plasma = "matplotlib.plasma", inferno = "matplotlib.inferno",
cividis = "matplotlib.cividis")


getPal = function(name, n = NA, rep = TRUE, range = NA) {
if (name %in% c("cat", "seq", "div")) {
name = cols4all::c4a_options("defaults")$defaults[[name]]
Expand All @@ -14,5 +33,13 @@ getPalMeta = function(name) {
if (name %in% c("cat", "seq", "div")) {
name = cols4all::c4a_options("defaults")$defaults[[name]]
}
cols4all::c4a_info(name, no.match = "null", verbose = FALSE)
if (name %in% names(pals_v3)) {
oldname = name
name = unname(pals_v3[oldname])
info = cols4all::c4a_info(name, verbose = FALSE)
message_c4a(oldname, info, fullname = info$series == "brewer")
} else {
info = cols4all::c4a_info(name, no.match = "null", verbose = FALSE)
}
info
}

0 comments on commit 05bd6c6

Please sign in to comment.