Skip to content

Commit

Permalink
Merge pull request #3 from alarm-redist/palette
Browse files Browse the repository at this point in the history
use palette
  • Loading branch information
CoryMcCartan committed May 15, 2024
2 parents adb3660 + 5305a60 commit ef0168f
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 98 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ Depends:
Imports:
grDevices,
ggplot2,
scales
scales,
palette
Suggests:
sf,
geos,
redist
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
Expand Down
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,palette)
export(GeomDistrict)
export(GeomDistrictText)
export(GeomPlaces)
Expand Down Expand Up @@ -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)
Expand Down
77 changes: 34 additions & 43 deletions R/colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#' For details on Wikipedia-based colors, see
#' <https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Elections_and_Referendums/USA_legend_colors>.
#'
#' @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)
Expand All @@ -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'))
)

4 changes: 2 additions & 2 deletions R/dra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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), ...)
}


Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions R/florida.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#' @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
#' @concept colors
#' @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
Expand All @@ -44,15 +44,15 @@ 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
#' @concept colors
#' @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
Expand Down
8 changes: 4 additions & 4 deletions R/nature.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#' @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
#' @concept colors
#' @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
Expand All @@ -43,15 +43,15 @@ 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
#' @concept colors
#' @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
Expand Down
4 changes: 2 additions & 2 deletions R/penn82.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
#' @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
#' @concept colors
#' @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
Expand Down
8 changes: 4 additions & 4 deletions R/polimap.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#' @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
#' @concept colors
#' @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
Expand All @@ -44,15 +44,15 @@ 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
#' @concept colors
#' @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
Expand Down
28 changes: 0 additions & 28 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions R/washington.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
#' @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
#' @concept colors
#' @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
Expand Down
4 changes: 2 additions & 2 deletions man/ggredist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef0168f

Please sign in to comment.