diff --git a/DESCRIPTION b/DESCRIPTION index d5a485f..c0eba95 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,8 @@ Depends: Imports: grDevices, ggplot2, - scales + scales, + palette Suggests: sf, geos, @@ -22,7 +23,7 @@ Suggests: License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 URL: https://github.com/alarm-redist/ggredist, https://alarm-redist.org/ggredist/ BugReports: https://github.com/alarm-redist/ggredist/issues diff --git a/NAMESPACE b/NAMESPACE index 12c4055..d24afc4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,5 @@ # Generated by roxygen2: do not edit by hand -S3method(plot,palette) export(GeomDistrict) export(GeomDistrictText) export(GeomPlaces) @@ -69,10 +68,6 @@ export(stat_district_coordinates) export(stat_interstates) export(stat_places) export(theme_map) -importFrom(graphics,image) -importFrom(graphics,par) -importFrom(graphics,rect) -importFrom(graphics,text) importFrom(stats,approx) importFrom(stats,optim) importFrom(stats,weighted.mean) diff --git a/R/colors.R b/R/colors.R index 2306b54..2762c0a 100644 --- a/R/colors.R +++ b/R/colors.R @@ -21,8 +21,8 @@ #' For details on Wikipedia-based colors, see #' . #' -#' @format list of character vectors of type `palette` (which supports a -#' `plot()` generic for visualization) +#' @format list of character vectors of type [palette::palette] (which supports a +#' `plot()`, `print()`, and `pillar` generics for visualization) #' #' @examples #' plot(ggredist$partisan) @@ -45,54 +45,45 @@ #' @concept colors #' @export ggredist = list( - partisan = structure(c('#A0442C', '#B25D4C', '#C27568', '#D18E84', '#DFA8A0', + partisan = palette::palette(c('#A0442C', '#B25D4C', '#C27568', '#D18E84', '#DFA8A0', '#EBC2BC', '#F6DCD9', '#F9F9F9', '#DAE2F4', '#BDCCEA', - '#9FB6DE', '#82A0D2', '#638BC6', '#3D77BB', '#0063B1'), - class = c("palette", 'character')), - dra = structure(c('#7070ff', '#70B870', '#BE70BE', '#FF7070', '#FFE970', + '#9FB6DE', '#82A0D2', '#638BC6', '#3D77BB', '#0063B1')), + dra = palette::palette(c('#7070ff', '#70B870', '#BE70BE', '#FF7070', '#FFE970', '#70B8B8', '#E6AB80', '#ABA2E3', '#70FFFF', '#FF7BC2', - '#B7FF70', '#A8C4F5', '#F3C4B4', '#BEBE7E'), class = c("palette", 'character')), - penn82 = structure(c('#EDEF3F', '#FED09E', '#A7825A', '#FFF8B7', - '#E2A414', '#ECD166'), class = c("palette", 'character')), - randmcnally = structure(c("#f4c450", "#e79274", "#d3a7ae", "#fcf092", - "#98b791", "#c7d4da", "#e8ba8f"), class = c("palette", 'character')), - natgeo = structure(c("#abafd0", "#e8b3a5", "#fded7e", "#b6c572", - "#efc965", "#fcf3e2"), class = c("palette", 'character')), - coast = structure(c("#D4B46E", "#84AFA2", "#547286", - "#7F7C74", "#B2BCD6", "#526042"), class = c("palette", 'character')), - larch = structure(c("#D0A75F", "#626B5D", "#8C8F9E", - "#858753", "#A4BADA", "#CDB7AA"), class = c("palette", 'character')), - fivethirtyeight = structure(c('#5768AC', '#A1A9ED', '#EAE3EB', - '#FF998A', '#FA5A50'), class = c("palette", 'character')), - wiki_dem_pres = structure(c('#E1EFFF', '#D3E7FF', '#B9D7FF', '#86B6F2', '#4389E3', - '#1666CB', '#0645B4', '#002B84'), - class = c("palette", 'character')), - wiki_rep_pres = structure(c('#FFDFE1', '#FFCCD0', '#F2B3BE', '#E27F90', '#CC2F4A', - '#D40000', '#AA0000', '#800000'), - class = c("palette", 'character')), - wiki_dem = structure(c('#EBF2FF', '#DFEEFF', '#BDD3FF', '#A5B0FF', '#7996E2', - '#6674DE', '#584CDE', '#3933E5', '#0D0596'), - class = c("palette", 'character')), - wiki_rep = structure(c('#FFF0F5', '#FFE0EA', '#FFC8CD', '#FFB2B2', '#E27F7F', - '#D75D5D', '#D72F30', '#C21B18', '#A80000'), - class = c("palette", 'character')), - wiki_proposal = structure(c('#2B2457', '#28497C', '#47729E', '#7D9CBB', '#B6C8D9', - '#EBEEED', '#DEDEBD', '#BCBC83', '#8B8B54', '#5D5D2D', '#32320C'), - class = c("palette", 'character')), - jacksonville = structure(c('#609CA7', '#E4874F', '#6FF87F', '#B770E6', '#5052DD', + '#B7FF70', '#A8C4F5', '#F3C4B4', '#BEBE7E')), + penn82 = palette::palette(c('#EDEF3F', '#FED09E', '#A7825A', '#FFF8B7', + '#E2A414', '#ECD166')), + randmcnally = palette::palette(c("#f4c450", "#e79274", "#d3a7ae", "#fcf092", + "#98b791", "#c7d4da", "#e8ba8f")), + natgeo = palette::palette(c("#abafd0", "#e8b3a5", "#fded7e", "#b6c572", + "#efc965", "#fcf3e2")), + coast = palette::palette(c("#D4B46E", "#84AFA2", "#547286", + "#7F7C74", "#B2BCD6", "#526042")), + larch = palette::palette(c("#D0A75F", "#626B5D", "#8C8F9E", + "#858753", "#A4BADA", "#CDB7AA")), + fivethirtyeight = palette::palette(c('#5768AC', '#A1A9ED', '#EAE3EB', + '#FF998A', '#FA5A50')), + wiki_dem_pres = palette::palette(c('#E1EFFF', '#D3E7FF', '#B9D7FF', '#86B6F2', '#4389E3', + '#1666CB', '#0645B4', '#002B84')), + wiki_rep_pres = palette::palette(c('#FFDFE1', '#FFCCD0', '#F2B3BE', '#E27F90', '#CC2F4A', + '#D40000', '#AA0000', '#800000')), + wiki_dem = palette::palette(c('#EBF2FF', '#DFEEFF', '#BDD3FF', '#A5B0FF', '#7996E2', + '#6674DE', '#584CDE', '#3933E5', '#0D0596')), + wiki_rep = palette::palette(c('#FFF0F5', '#FFE0EA', '#FFC8CD', '#FFB2B2', '#E27F7F', + '#D75D5D', '#D72F30', '#C21B18', '#A80000')), + wiki_proposal = palette::palette(c('#2B2457', '#28497C', '#47729E', '#7D9CBB', '#B6C8D9', + '#EBEEED', '#DEDEBD', '#BCBC83', '#8B8B54', '#5D5D2D', '#32320C')), + jacksonville = palette::palette(c('#609CA7', '#E4874F', '#6FF87F', '#B770E6', '#5052DD', '#92B340', '#93EDCA', '#83516A', '#85A6F7', '#448047', - '#3F3E80', '#CA4D6D', '#DCE592', '#918259'), - class = c('palette', 'character')), - florida = structure(c('#F7FDB1', '#D1FDBC', '#FEBBD5', '#C6FEE0', '#C5CAFF', + '#3F3E80', '#CA4D6D', '#DCE592', '#918259')), + florida = palette::palette(c('#F7FDB1', '#D1FDBC', '#FEBBD5', '#C6FEE0', '#C5CAFF', '#F0C2FD', '#FEC6C9', '#B4FDBF', '#FDF4C4', '#BAB5FD', '#FFB5B3', '#BDF8FE', '#FFBEF3', '#B6FDCF', '#FDF1D5', '#C7D8FE', '#FED5E2', '#E4B3FD', '#E0FDD6', '#FDE3B1', '#FDCFBB', '#DEC8FE', '#D7EFFF', '#DFFDF2', '#FED5FC', - '#B5FFF1', '#B4E5FE', '#EFFDC6'), - class = c('palette', 'character')), - washington = structure(c('#993A5B', '#1E5769', '#D98B79', '#D9CC46', '#2AA99C', + '#B5FFF1', '#B4E5FE', '#EFFDC6')), + washington = palette::palette(c('#993A5B', '#1E5769', '#D98B79', '#D9CC46', '#2AA99C', '#F1C1B8', '#533742', '#ABD2C5', '#976C81', '#239C78', - '#A36D02', '#2A3944'), - class = c('palette', 'character')) + '#A36D02', '#2A3944')) ) diff --git a/R/dra.R b/R/dra.R index 980c9d7..eb76e0d 100644 --- a/R/dra.R +++ b/R/dra.R @@ -18,7 +18,7 @@ #' @export scale_fill_dra <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'dra', - palette = rot_pal(ggredist$dra), ...) + palette = palette::palette_function(ggredist$dra), ...) } @@ -27,7 +27,7 @@ scale_fill_dra <- function(...) { #' @export scale_color_dra <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'dra', - palette = rot_pal(ggredist$dra), ...) + palette = palette::palette_function(ggredist$dra), ...) } #' @rdname scale_dra #' @concept colors diff --git a/R/florida.R b/R/florida.R index ffa4c4a..ed69992 100644 --- a/R/florida.R +++ b/R/florida.R @@ -23,7 +23,7 @@ #' @export scale_fill_jacksonville <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'jacksonville', - palette = rot_pal(ggredist$jacksonville), ...) + palette = palette::palette_function(ggredist$jacksonville), ...) } #' @rdname scale_florida @@ -31,7 +31,7 @@ scale_fill_jacksonville <- function(...) { #' @export scale_color_jacksonville <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'jacksonville', - palette = rot_pal(ggredist$jacksonville), ...) + palette = palette::palette_function(ggredist$jacksonville), ...) } #' @rdname scale_florida #' @concept colors @@ -44,7 +44,7 @@ scale_colour_jacksonville = scale_color_jacksonville #' @export scale_fill_florida <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'florida', - palette = rot_pal(ggredist$florida), ...) + palette = palette::palette_function(ggredist$florida), ...) } #' @rdname scale_florida @@ -52,7 +52,7 @@ scale_fill_florida <- function(...) { #' @export scale_color_florida <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'florida', - palette = rot_pal(ggredist$florida), ...) + palette = palette::palette_function(ggredist$florida), ...) } #' @rdname scale_florida #' @concept colors diff --git a/R/nature.R b/R/nature.R index 30f1207..72ee268 100644 --- a/R/nature.R +++ b/R/nature.R @@ -23,7 +23,7 @@ #' @export scale_fill_coast <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'coast', - palette = rot_pal(ggredist$coast), ...) + palette = palette::palette_function(ggredist$coast), ...) } #' @rdname scale_nature @@ -31,7 +31,7 @@ scale_fill_coast <- function(...) { #' @export scale_color_coast <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'coast', - palette = rot_pal(ggredist$coast), ...) + palette = palette::palette_function(ggredist$coast), ...) } #' @rdname scale_nature #' @concept colors @@ -43,7 +43,7 @@ scale_colour_coast = scale_color_coast #' @export scale_fill_larch <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'larch', - palette = rot_pal(ggredist$larch), ...) + palette = palette::palette_function(ggredist$larch), ...) } #' @rdname scale_nature @@ -51,7 +51,7 @@ scale_fill_larch <- function(...) { #' @export scale_color_larch <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'larch', - palette = rot_pal(ggredist$larch), ...) + palette = palette::palette_function(ggredist$larch), ...) } #' @rdname scale_nature #' @concept colors diff --git a/R/penn82.R b/R/penn82.R index 7c04688..b27b12a 100644 --- a/R/penn82.R +++ b/R/penn82.R @@ -18,7 +18,7 @@ #' @export scale_fill_penn82 <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'penn82', - palette = rot_pal(ggredist$penn82), ...) + palette = palette::palette_function(ggredist$penn82), ...) } #' @rdname scale_penn82 @@ -26,7 +26,7 @@ scale_fill_penn82 <- function(...) { #' @export scale_color_penn82 <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'penn82', - palette = rot_pal(ggredist$penn82), ...) + palette = palette::palette_function(ggredist$penn82), ...) } #' @rdname scale_penn82 #' @concept colors diff --git a/R/polimap.R b/R/polimap.R index 3dc7818..b5898b9 100644 --- a/R/polimap.R +++ b/R/polimap.R @@ -23,7 +23,7 @@ #' @export scale_fill_randmcnally <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'randmcnally', - palette = rot_pal(ggredist$randmcnally), ...) + palette = palette::palette_function(ggredist$randmcnally), ...) } #' @rdname scale_polimap @@ -31,7 +31,7 @@ scale_fill_randmcnally <- function(...) { #' @export scale_color_randmcnally <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'randmcnally', - palette = rot_pal(ggredist$randmcnally), ...) + palette = palette::palette_function(ggredist$randmcnally), ...) } #' @rdname scale_polimap #' @concept colors @@ -44,7 +44,7 @@ scale_colour_randmcnally = scale_color_randmcnally #' @export scale_fill_natgeo <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'natgeo', - palette = rot_pal(ggredist$natgeo), ...) + palette = palette::palette_function(ggredist$natgeo), ...) } #' @rdname scale_polimap @@ -52,7 +52,7 @@ scale_fill_natgeo <- function(...) { #' @export scale_color_natgeo <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'natgeo', - palette = rot_pal(ggredist$natgeo), ...) + palette = palette::palette_function(ggredist$natgeo), ...) } #' @rdname scale_polimap #' @concept colors diff --git a/R/utils.R b/R/utils.R index 32b3e13..33e41a5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -2,31 +2,3 @@ make_bins <- function(v, pal, where) { force(v) pal[as.integer(cut(v, where))] } - -rot_pal <- function(pal) { - function(n) { - if (n <= length(pal)) { - pal[seq_len(n)] - } else { - rep(pal, ceiling(n / length(pal)))[seq_len(n)] - } - } -} - -# `plot.palette` modified from that in `wesanderson` (c) 2016 Karthik Ram -#' @export -#' @importFrom graphics rect par image text -plot.palette = function(x, ...) { - n <- length(x) - old <- par(mar=c(0.5, 0.5, 0.5, 0.5)) - on.exit(par(old)) - - image(1:n, 1, as.matrix(1:n), col=x, - ylab="", xaxt="n", yaxt="n", bty="n") - - rect(0, 0.9, n + 1, 1.1, col=grDevices::rgb(1, 1, 1, 0.8), border=NA) - text((n + 1) / 2, 1, labels=deparse(substitute(x)), col="black", cex=1, font=2) - - if (!is.null(names(x))) - text(1:n, 1.25, labels=names(x), col="black", cex=1) -} diff --git a/R/washington.R b/R/washington.R index 29c6fbb..f03583b 100644 --- a/R/washington.R +++ b/R/washington.R @@ -18,7 +18,7 @@ #' @export scale_fill_washington <- function(...) { ggplot2::discrete_scale(aesthetics = 'fill', scale_name = 'washington', - palette = rot_pal(ggredist$washington), ...) + palette = palette::palette_function(ggredist$washington), ...) } #' @rdname scale_washington @@ -26,7 +26,7 @@ scale_fill_washington <- function(...) { #' @export scale_color_washington <- function(...) { ggplot2::discrete_scale(aesthetics = 'color', scale_name = 'washington', - palette = rot_pal(ggredist$washington), ...) + palette = palette::palette_function(ggredist$washington), ...) } #' @rdname scale_washington #' @concept colors diff --git a/man/ggredist.Rd b/man/ggredist.Rd index f8e7ca7..3cd8d89 100644 --- a/man/ggredist.Rd +++ b/man/ggredist.Rd @@ -5,8 +5,8 @@ \alias{ggredist} \title{Redistricting Color Palettes} \format{ -list of character vectors of type \code{palette} (which supports a -\code{plot()} generic for visualization) +list of character vectors of type \link[palette:palette]{palette::palette} (which supports a +\code{plot()}, \code{print()}, and \code{pillar} generics for visualization) } \usage{ ggredist