From 15f7a09c38150fbe46455665f7a2b89800c79132 Mon Sep 17 00:00:00 2001 From: Mathew Kiang Date: Wed, 11 Apr 2018 16:01:44 -0400 Subject: [PATCH] Print NA states --- DESCRIPTION | 2 +- R/statebins.R | 7 ++++--- R/util.R | 9 +++++++-- man/geom_statebins.Rd | 2 +- man/statebins.Rd | 8 ++++---- tests/.DS_Store | Bin 0 -> 6148 bytes 6 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 tests/.DS_Store diff --git a/DESCRIPTION b/DESCRIPTION index de7c308..094ca81 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,7 +36,7 @@ Imports: ggplot2 (>= 2.2.1), scales (>= 0.5.0), grid -RoxygenNote: 6.0.1 +RoxygenNote: 6.0.1.9000 Collate: 'aaa.R' 'geom-rrect.r' diff --git a/R/statebins.R b/R/statebins.R index 5e31ecb..5433122 100644 --- a/R/statebins.R +++ b/R/statebins.R @@ -22,7 +22,7 @@ #' @param state_col column name in \code{state_data} that has the states. no duplicates #' and can be names (e.g. "\code{Maine}") or abbreviatons (e.g. "\code{ME}") #' @param value_col column name in \code{state_data} that holds the values to be plotted -#' @param dark_label,light_label dark/light label colors. The specified color will be used +#' @param dark_label,light_label,na_label dark/light/NA label colors. The specified color will be used #' when the algorithm determines labels should be inverted. #' @param font_size font size (default = \code{3}) #' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes @@ -43,7 +43,8 @@ #' theme_statebins(legend_position="right") statebins <- function(state_data, state_col="state", value_col="value", - dark_label = "black", light_label = "white", font_size=3, + dark_label = "black", light_label = "white", na_label = "white", + font_size=3, state_border_col="white", state_border_size=2, round = FALSE, radius = grid::unit(6, "pt"), ggplot2_scale_function=ggplot2::scale_fill_distiller, @@ -84,7 +85,7 @@ statebins <- function(state_data, gg <- gg + geom_text(data = st.dat, aes_string(x = "col", y = "row", label = "abbrev"), angle = 0, - color = .sb_invert(gb$data[[1]]$fill, dark_label, light_label), + color = .sb_invert(gb$data[[1]]$fill, dark_label, light_label, na_label), size = font_size) gg diff --git a/R/util.R b/R/util.R index 35096d2..a818050 100644 --- a/R/util.R +++ b/R/util.R @@ -1,4 +1,4 @@ -.sb_invert <- function(hex_color, dark_color="black", light_color="white") { +.sb_invert <- function(hex_color, dark_color="black", light_color="white", na_color = "white") { hex_color <- gsub("#", "", hex_color) @@ -8,7 +8,12 @@ YIQ <- ((R*299) + (G*587) + (B*114)) / 1000 - return(ifelse(YIQ >= 128, dark_color, light_color)) + return( + ifelse(is.na(YIQ), na_color, + ifelse( + YIQ >= 128, dark_color, light_color) + ) + ) } diff --git a/man/geom_statebins.Rd b/man/geom_statebins.Rd index 4d77dd0..7ea0cec 100644 --- a/man/geom_statebins.Rd +++ b/man/geom_statebins.Rd @@ -5,7 +5,7 @@ \alias{geom_statebins} \alias{GeomStatebins} \title{A statebins Geom} -\format{An object of class \code{GeomStatebins} (inherits from \code{Geom}, \code{ggproto}, \code{gg}) of length 7.} +\format{An object of class \code{GeomStatebins} (inherits from \code{Geom}, \code{ggproto}) of length 7.} \usage{ geom_statebins(mapping = NULL, data = NULL, border_col = "white", border_size = 2, lbl_size = 3, dark_lbl = "black", diff --git a/man/statebins.Rd b/man/statebins.Rd index 664e75c..e3601fe 100644 --- a/man/statebins.Rd +++ b/man/statebins.Rd @@ -5,9 +5,9 @@ \title{Create a new ggplot-based "statebin" chart for USA states/territories} \usage{ statebins(state_data, state_col = "state", value_col = "value", - dark_label = "black", light_label = "white", font_size = 3, - state_border_col = "white", state_border_size = 2, round = FALSE, - radius = grid::unit(6, "pt"), + dark_label = "black", light_label = "white", na_label = "white", + font_size = 3, state_border_col = "white", state_border_size = 2, + round = FALSE, radius = grid::unit(6, "pt"), ggplot2_scale_function = ggplot2::scale_fill_distiller, ...) } \arguments{ @@ -18,7 +18,7 @@ and can be names (e.g. "\code{Maine}") or abbreviatons (e.g. "\code{ME}")} \item{value_col}{column name in \code{state_data} that holds the values to be plotted} -\item{dark_label, light_label}{dark/light label colors. The specified color will be used +\item{dark_label, light_label, na_label}{dark/light/NA label colors. The specified color will be used when the algorithm determines labels should be inverted.} \item{font_size}{font size (default = \code{3})} diff --git a/tests/.DS_Store b/tests/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a1f14fc996c94393571c403f477527957a961f64 GIT binary patch literal 6148 zcmeHKJ8Hu~5S_7I2;8W2xmU;y7UP^i7f8S%DS{1-lUkL}m80d&2NSVMV{jARz|7m7 zowq`-&}c+Nx1ak5kzPbPxS?Duv}Wh#jV&^xKsfF=%bPsQ`D-{Gn(Fz4aXau{sQ?wA0#^m>`%vJ9HE|5|PX~gJ0KgT} zZdm&)0W6jP*2FOo8JGqY7*x#>LxYZd$-0_21_oU;hY!ssYfdQYPsjbm%SCG-BNd?XCYm!vC26rzGyE02TOG3g~J#?6!EN?5)kqS+6bdC%Dyo!_BaE3WB#|ptoae ftQ~K>DC&x>ab6S0K&K<`bRd5QOcxpzxVHj7>hcx4 literal 0 HcmV?d00001