From 6da02a0a00529c371d770a4c8bde76abc26f9b3a Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Tue, 14 Nov 2017 13:37:53 -0500 Subject: [PATCH] colors --- DESCRIPTION | 1 + NAMESPACE | 1 + R/aaa.R | 10 ++++++---- R/statebins-continuous.R | 7 ++++--- R/statebins-manual.R | 7 ++++--- R/statebins-package.R | 2 +- R/statebins.R | 35 +++++++++++++++++++++++++++-------- man/statebins.Rd | 20 +++++++++++++++----- man/statebins_continuous.Rd | 7 ++++--- man/statebins_manual.Rd | 7 ++++--- 10 files changed, 67 insertions(+), 30 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cf190b0..175b055 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,5 +23,6 @@ Depends: Imports: ggplot2 (>= 2.2.0), scales (>= 0.5.0), + viridis, RColorBrewer (>= 1.1-2) RoxygenNote: 6.0.1 diff --git a/NAMESPACE b/NAMESPACE index 06425be..9158b6e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(statebins) export(statebins_continuous) export(statebins_manual) import(RColorBrewer) +import(viridis) importFrom(ggplot2,aes) importFrom(ggplot2,aes_string) importFrom(ggplot2,coord_equal) diff --git a/R/aaa.R b/R/aaa.R index 1ef8667..598ad7f 100644 --- a/R/aaa.R +++ b/R/aaa.R @@ -14,14 +14,16 @@ state_coords <- structure(list(abbrev = c("AL", "AK", "AZ", "AR", "CA", "CO", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", - "Puerto Rico", "Virgin Islands"), + "Puerto Rico", "Virgin Islands", "New York City"), col = c(8L, 1L, 3L, 6L, 2L, 4L, 11L, 10L, 11L, 10L, 9L, 1L, 3L, 7L, 7L, 6L, 5L, 7L, 6L, 12L, 10L, 11L, 8L, 6L, 7L, 6L, 4L, 5L, 3L, 12L, 10L, 4L, 10L, 8L, 5L, 8L, 5L, 2L, 9L, 12L, - 9L, 5L, 7L, 5L, 3L, 11L, 9L, 2L, 8L, 7L, 4L, 12L, 0L), + 9L, 5L, 7L, 5L, 3L, 11L, 9L, 2L, 8L, 7L, 4L, 12L, 12L, 12), row = c(7L, 7L, 6L, 6L, 5L, 5L, 4L, 6L, 5L, 8L, 7L, 8L, 3L, 3L, 4L, 4L, 6L, 5L, 7L, 1L, 5L, 3L, 3L, 3L, 7L, 5L, 3L, 5L, 4L, 2L, 4L, 6L, 3L, 6L, - 3L, 4L, 7L, 4L, 4L, 4L, 6L, 4L, 6L, 8L, 5L, 2L, 5L, 3L, 5L, 2L, 4L, 8L, 6L)), - .Names = c("abbrev", "state", "col", "row"), class = "data.frame", row.names = c(NA, -53L)) + 3L, 4L, 7L, 4L, 4L, 4L, 6L, 4L, 6L, 8L, 5L, 2L, 5L, 3L, 5L, 2L, 4L, + 8L, 7L, 3L)), + .Names = c("abbrev", "state", "col", "row"), + class = "data.frame", row.names = c(NA, -54L)) diff --git a/R/statebins-continuous.R b/R/statebins-continuous.R index a061053..c32fb16 100644 --- a/R/statebins-continuous.R +++ b/R/statebins-continuous.R @@ -20,14 +20,15 @@ #' #' To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) #' and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" -#' will cause \code{statebins} to use \code{arrangeGrob} to position the title via \code{sub} and -#' return a frame grob instead of a ggplot2 object. +#' will cause \code{statebins} to use the X axis title placeholder. #' #' @param state_data data frame of states and values to plot #' @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 text_color default "\code{black}" +#' @param text_color default "\code{black}". Size 1 for global color across all tiles or +#' a vector of colors the same length as the number of states you passed in. +#' Use the sort order for the states as they are sorted before being plotted. #' @param font_size font size (default = \code{3}) #' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes #' @param legend_title title for the legend diff --git a/R/statebins-manual.R b/R/statebins-manual.R index f35aae5..ee64a38 100644 --- a/R/statebins-manual.R +++ b/R/statebins-manual.R @@ -19,14 +19,15 @@ #' #' To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) #' and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" -#' will cause \code{statebins} to use \code{arrangeGrob} to position the title via \code{sub} and -#' return a frame grob instead of a ggplot2 object. +#' will cause \code{statebins} to use the X axis title placeholder. #' #' @param state_data data frame of states and values to plot #' @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 color_col column name in \code{state_data} that holds the colors to be used -#' @param text_color default "\code{black}" +#' @param text_color default "\code{black}". Size 1 for global color across all tiles or +#' a vector of colors the same length as the number of states you passed in. +#' Use the sort order for the states as they are sorted before being plotted. #' @param font_size font size (default = \code{3}) #' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes #' @param labels labels for the legend (should be the same number as distinct colors in diff --git a/R/statebins-package.R b/R/statebins-package.R index 8fe987d..adcc326 100644 --- a/R/statebins-package.R +++ b/R/statebins-package.R @@ -4,7 +4,7 @@ #' @name statebins-package #' @docType package #' @author Bob Rudis (bob@@rud.is) -#' @import RColorBrewer +#' @import RColorBrewer viridis #' @importFrom scales alpha #' @importFrom ggplot2 ggplot geom_tile scale_fill_manual guides geom_tile ggplotGrob #' @importFrom ggplot2 geom_point geom_text scale_color_manual guides theme labs diff --git a/R/statebins.R b/R/statebins.R index c7930cc..f44a614 100644 --- a/R/statebins.R +++ b/R/statebins.R @@ -19,14 +19,15 @@ #' #' To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) #' and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" -#' will cause \code{statebins} to use \code{arrangeGrob} to position the title via \code{sub} and -#' return a frame grob instead of a ggplot2 object. +#' will cause \code{statebins} to use the X axis title as the title. #' #' @param state_data data frame of states and values to plot #' @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 text_color default "\code{black}" +#' @param text_color default "\code{black}". Size 1 for global color across all tiles or +#' a vector of colors the same length as the number of states you passed in. +#' Use the sort order for the states as they are sorted before being plotted. #' @param font_size font size (default = \code{3}) #' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes #' @param breaks a single number (greater than or equal to 2) giving the number of intervals @@ -35,7 +36,13 @@ #' @param legend_title title for the legend #' @param legend_position "\code{none}", "\code{top}", "\code{left}", "\code{right}" or #' "\code{bottom}" (defaults to "\code{top}") -#' @param brewer_pal which named \code{RColorBrewer} palette to use (defaults to "PuBu") +#' @param palette either "`brewer`" or "`viridis`". Choose `viridis` if you have +#' 10 or more levels (more than 10 is not recommended). You can choose which +#' viridis palette option (e.g. "magma") with `viridis_pal`. +#' @param brewer_pal which named \code{RColorBrewer} palette to use (defaults to "PuBu"); +#' used when `palette` is `brewer`. +#' @param viridis_pal which named \code{viridis} palette option to use (default if `NULL`); +#' used when `palette` is `viridis`. #' @param plot_title title for the plot #' @param title_position where to put the title ("\code{bottom}" or "\code{top}" or "" #' for none); if "\code{bottom}", you get back a grob vs a ggplot object @@ -50,11 +57,16 @@ #' } statebins <- function(state_data, state_col="state", value_col="value", text_color="black", font_size=3, - state_border_col="white", breaks=5, labels=1:5, + state_border_col="white", + breaks=5, labels=1:5, legend_title="Legend", legend_position="top", - brewer_pal="PuBu", plot_title="", title_position="bottom") { + palette=c("brewer", "viridis"), viridis_pal=NULL, + brewer_pal="palette", + plot_title="", title_position="bottom") { - if (breaks <= 0 | breaks >= 10) { + palette <- match.arg(trimws(tolower(palette)), c("brewer", "viridis")) + + if (breaks <= 0 | breaks > 10) { stop("'breaks' must be between 0 & 10") } @@ -82,7 +94,14 @@ statebins <- function(state_data, state_col="state", value_col="value", size=2, show.legend=FALSE) gg <- gg + geom_text(color=text_color, size=font_size) gg <- gg + scale_y_reverse() - gg <- gg + scale_fill_brewer(palette=brewer_pal, name=legend_title) + + if (palette == "brewer") { + gg <- gg + scale_fill_brewer(palette=brewer_pal, name=legend_title, drop=FALSE) + } else if (palette == "viridis") { + if (is.null(viridis_pal)) viridis_pal <- "D" + gg <- gg + scale_fill_viridis(discrete=TRUE, option=viridis_pal, name=legend_title, drop=FALSE) + } + gg <- gg + coord_equal() gg <- gg + labs(x=NULL, y=NULL, title=NULL) gg <- gg + theme_bw() diff --git a/man/statebins.Rd b/man/statebins.Rd index 578e7b4..30de2c9 100644 --- a/man/statebins.Rd +++ b/man/statebins.Rd @@ -7,7 +7,8 @@ statebins(state_data, state_col = "state", value_col = "value", text_color = "black", font_size = 3, state_border_col = "white", breaks = 5, labels = 1:5, legend_title = "Legend", - legend_position = "top", brewer_pal = "PuBu", plot_title = "", + legend_position = "top", palette = c("brewer", "viridis"), + viridis_pal = NULL, brewer_pal = "palette", plot_title = "", title_position = "bottom") } \arguments{ @@ -18,7 +19,9 @@ 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{text_color}{default "\code{black}"} +\item{text_color}{default "\code{black}". Size 1 for global color across all tiles or +a vector of colors the same length as the number of states you passed in. +Use the sort order for the states as they are sorted before being plotted.} \item{font_size}{font size (default = \code{3})} @@ -34,7 +37,15 @@ into which data values are to be cut.} \item{legend_position}{"\code{none}", "\code{top}", "\code{left}", "\code{right}" or "\code{bottom}" (defaults to "\code{top}")} -\item{brewer_pal}{which named \code{RColorBrewer} palette to use (defaults to "PuBu")} +\item{palette}{either "`brewer`" or "`viridis`". Choose `viridis` if you have +10 or more levels (more than 10 is not recommended). You can choose which +viridis palette option (e.g. "magma") with `viridis_pal`.} + +\item{viridis_pal}{which named \code{viridis} palette option to use (default if `NULL`); +used when `palette` is `viridis`.} + +\item{brewer_pal}{which named \code{RColorBrewer} palette to use (defaults to "PuBu"); +used when `palette` is `brewer`.} \item{plot_title}{title for the plot} @@ -65,8 +76,7 @@ and \code{value_col} accordingly. To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" -will cause \code{statebins} to use \code{arrangeGrob} to position the title via \code{sub} and -return a frame grob instead of a ggplot2 object. +will cause \code{statebins} to use the X axis title as the title. } \examples{ \dontrun{ diff --git a/man/statebins_continuous.Rd b/man/statebins_continuous.Rd index ee80409..111460b 100644 --- a/man/statebins_continuous.Rd +++ b/man/statebins_continuous.Rd @@ -17,7 +17,9 @@ 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{text_color}{default "\code{black}"} +\item{text_color}{default "\code{black}". Size 1 for global color across all tiles or +a vector of colors the same length as the number of states you passed in. +Use the sort order for the states as they are sorted before being plotted.} \item{font_size}{font size (default = \code{3})} @@ -60,8 +62,7 @@ and \code{value_col} accordingly. To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" -will cause \code{statebins} to use \code{arrangeGrob} to position the title via \code{sub} and -return a frame grob instead of a ggplot2 object. +will cause \code{statebins} to use the X axis title placeholder. } \examples{ \dontrun{ diff --git a/man/statebins_manual.Rd b/man/statebins_manual.Rd index 200924a..0fe4481 100644 --- a/man/statebins_manual.Rd +++ b/man/statebins_manual.Rd @@ -17,7 +17,9 @@ and can be names (e.g. "\code{Maine}") or abbreviatons (e.g. "\code{ME}")} \item{color_col}{column name in \code{state_data} that holds the colors to be used} -\item{text_color}{default "\code{black}"} +\item{text_color}{default "\code{black}". Size 1 for global color across all tiles or +a vector of colors the same length as the number of states you passed in. +Use the sort order for the states as they are sorted before being plotted.} \item{font_size}{font size (default = \code{3})} @@ -60,8 +62,7 @@ and \code{color_col} accordingly. To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" -will cause \code{statebins} to use \code{arrangeGrob} to position the title via \code{sub} and -return a frame grob instead of a ggplot2 object. +will cause \code{statebins} to use the X axis title placeholder. } \examples{ \dontrun{