diff --git a/DESCRIPTION b/DESCRIPTION index de7c308..abf7710 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -11,7 +11,9 @@ Authors@R: c( person("Brian", "Adams", email = "btadams478@gmail.com", role = "ctb", comment = "theme testing & feedback"), person("Thomas", "Wood", role = "ctb", - comment = "Significant suggestions & testing that made new features possible") + comment = "Significant suggestions & testing that made new features possible"), + person("Mathew", "Kiang", role = "ctb", + comment = "Minor fix for NA handling") ) Maintainer: Bob Rudis Description: Cartogram heatmaps are an alternative to choropleth maps for 'USA' States @@ -36,7 +38,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/geom-statebins.r b/R/geom-statebins.r index 7d79a9d..4fe94c9 100644 --- a/R/geom-statebins.r +++ b/R/geom-statebins.r @@ -30,7 +30,7 @@ #' @param border_col border color of the state squares, default "`white`" #' @param border_size thickness of the square state borders #' @param lbl_size font size (relative) of the label text -#' @param dark_lbl,light_lbl colrs to be uses when the label should be dark or light. +#' @param dark_lbl,light_lbl,na_lbl colors to be uses when the label should be dark, light, or NA. #' The function automagically computes when this should be. #' @param radius the corner radius #' @param na.rm If `FALSE`, the default, missing values are removed with @@ -72,7 +72,7 @@ geom_statebins <- function( mapping = NULL, data = NULL, border_col = "white", border_size = 2, - lbl_size = 3, dark_lbl = "black", light_lbl = "white", + lbl_size = 3, dark_lbl = "black", light_lbl = "white", na_lbl = "white", radius = grid::unit(6, "pt"), ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { @@ -147,6 +147,7 @@ GeomStatebins <- ggplot2::ggproto("GeomStatebins", ggplot2::Geom, draw_panel = function(self, data, panel_params, coord, border_col = "white", border_size = 2, lbl_size = 3, dark_lbl = "black", light_lbl = "white", + na_lbl = "white", radius = grid::unit(6, "pt")) { tile_data <- data @@ -158,7 +159,7 @@ GeomStatebins <- ggplot2::ggproto("GeomStatebins", ggplot2::Geom, text_data$label <- data$abbrev text_data$fill <- NA text_data$size <- lbl_size - text_data$colour <- .sb_invert(data$fill, dark_lbl, light_lbl) + text_data$colour <- .sb_invert(data$fill, dark_lbl, light_lbl, na_lbl) coord <- coord_equal() diff --git a/R/statebins.R b/R/statebins.R index 5e31ecb..1fd60bb 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,9 +43,10 @@ #' 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"), + 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..9af4620 100644 --- a/R/util.R +++ b/R/util.R @@ -1,4 +1,5 @@ -.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,8 +9,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) + ) + ) } # sanity checks for state values diff --git a/man/geom_statebins.Rd b/man/geom_statebins.Rd index 4d77dd0..73172ce 100644 --- a/man/geom_statebins.Rd +++ b/man/geom_statebins.Rd @@ -5,12 +5,12 @@ \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", - light_lbl = "white", radius = grid::unit(6, "pt"), ..., na.rm = FALSE, - show.legend = NA, inherit.aes = TRUE) + light_lbl = "white", na_lbl = "white", radius = grid::unit(6, "pt"), + ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) GeomStatebins } @@ -40,7 +40,7 @@ will be used as the layer data.} \item{lbl_size}{font size (relative) of the label text} -\item{dark_lbl, light_lbl}{colrs to be uses when the label should be dark or light. +\item{dark_lbl, light_lbl, na_lbl}{colors to be uses when the label should be dark, light, or NA. The function automagically computes when this should be.} \item{radius}{the corner radius} 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 0000000..a1f14fc Binary files /dev/null and b/tests/.DS_Store differ