Browse Source

colors

master
boB Rudis 7 years ago
parent
commit
6da02a0a00
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 1
      DESCRIPTION
  2. 1
      NAMESPACE
  3. 10
      R/aaa.R
  4. 7
      R/statebins-continuous.R
  5. 7
      R/statebins-manual.R
  6. 2
      R/statebins-package.R
  7. 35
      R/statebins.R
  8. 20
      man/statebins.Rd
  9. 7
      man/statebins_continuous.Rd
  10. 7
      man/statebins_manual.Rd

1
DESCRIPTION

@ -23,5 +23,6 @@ Depends:
Imports: Imports:
ggplot2 (>= 2.2.0), ggplot2 (>= 2.2.0),
scales (>= 0.5.0), scales (>= 0.5.0),
viridis,
RColorBrewer (>= 1.1-2) RColorBrewer (>= 1.1-2)
RoxygenNote: 6.0.1 RoxygenNote: 6.0.1

1
NAMESPACE

@ -4,6 +4,7 @@ export(statebins)
export(statebins_continuous) export(statebins_continuous)
export(statebins_manual) export(statebins_manual)
import(RColorBrewer) import(RColorBrewer)
import(viridis)
importFrom(ggplot2,aes) importFrom(ggplot2,aes)
importFrom(ggplot2,aes_string) importFrom(ggplot2,aes_string)
importFrom(ggplot2,coord_equal) importFrom(ggplot2,coord_equal)

10
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", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", "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, 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, 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, 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, row = c(7L, 7L,
6L, 6L, 5L, 5L, 4L, 6L, 5L, 8L, 7L, 8L, 3L, 3L, 4L, 4L, 6L, 5L, 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, 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)), 3L, 4L, 7L, 4L, 4L, 4L, 6L, 4L, 6L, 8L, 5L, 2L, 5L, 3L, 5L, 2L, 4L,
.Names = c("abbrev", "state", "col", "row"), class = "data.frame", row.names = c(NA, -53L)) 8L, 7L, 3L)),
.Names = c("abbrev", "state", "col", "row"),
class = "data.frame", row.names = c(NA, -54L))

7
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{""}) #' 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}" #' 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 #' will cause \code{statebins} to use the X axis title placeholder.
#' return a frame grob instead of a ggplot2 object.
#' #'
#' @param state_data data frame of states and values to plot #' @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 #' @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}") #' 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 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 font_size font size (default = \code{3})
#' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes #' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes
#' @param legend_title title for the legend #' @param legend_title title for the legend

7
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{""}) #' 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}" #' 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 #' will cause \code{statebins} to use the X axis title placeholder.
#' return a frame grob instead of a ggplot2 object.
#' #'
#' @param state_data data frame of states and values to plot #' @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 #' @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}") #' 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 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 font_size font size (default = \code{3})
#' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes #' @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 #' @param labels labels for the legend (should be the same number as distinct colors in

2
R/statebins-package.R

@ -4,7 +4,7 @@
#' @name statebins-package #' @name statebins-package
#' @docType package #' @docType package
#' @author Bob Rudis (bob@@rud.is) #' @author Bob Rudis (bob@@rud.is)
#' @import RColorBrewer #' @import RColorBrewer viridis
#' @importFrom scales alpha #' @importFrom scales alpha
#' @importFrom ggplot2 ggplot geom_tile scale_fill_manual guides geom_tile ggplotGrob #' @importFrom ggplot2 ggplot geom_tile scale_fill_manual guides geom_tile ggplotGrob
#' @importFrom ggplot2 geom_point geom_text scale_color_manual guides theme labs #' @importFrom ggplot2 geom_point geom_text scale_color_manual guides theme labs

35
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{""}) #' 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}" #' 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 #' will cause \code{statebins} to use the X axis title as the title.
#' return a frame grob instead of a ggplot2 object.
#' #'
#' @param state_data data frame of states and values to plot #' @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 #' @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}") #' 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 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 font_size font size (default = \code{3})
#' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes #' @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 #' @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_title title for the legend
#' @param legend_position "\code{none}", "\code{top}", "\code{left}", "\code{right}" or #' @param legend_position "\code{none}", "\code{top}", "\code{left}", "\code{right}" or
#' "\code{bottom}" (defaults to "\code{top}") #' "\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 plot_title title for the plot
#' @param title_position where to put the title ("\code{bottom}" or "\code{top}" or "" #' @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 #' 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", statebins <- function(state_data, state_col="state", value_col="value",
text_color="black", font_size=3, 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", 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") 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) size=2, show.legend=FALSE)
gg <- gg + geom_text(color=text_color, size=font_size) gg <- gg + geom_text(color=text_color, size=font_size)
gg <- gg + scale_y_reverse() 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 + coord_equal()
gg <- gg + labs(x=NULL, y=NULL, title=NULL) gg <- gg + labs(x=NULL, y=NULL, title=NULL)
gg <- gg + theme_bw() gg <- gg + theme_bw()

20
man/statebins.Rd

@ -7,7 +7,8 @@
statebins(state_data, state_col = "state", value_col = "value", statebins(state_data, state_col = "state", value_col = "value",
text_color = "black", font_size = 3, state_border_col = "white", text_color = "black", font_size = 3, state_border_col = "white",
breaks = 5, labels = 1:5, legend_title = "Legend", 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") title_position = "bottom")
} }
\arguments{ \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{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})} \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 \item{legend_position}{"\code{none}", "\code{top}", "\code{left}", "\code{right}" or
"\code{bottom}" (defaults to "\code{top}")} "\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} \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{""}) 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}" 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 will cause \code{statebins} to use the X axis title as the title.
return a frame grob instead of a ggplot2 object.
} }
\examples{ \examples{
\dontrun{ \dontrun{

7
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{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})} \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{""}) 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}" 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 will cause \code{statebins} to use the X axis title placeholder.
return a frame grob instead of a ggplot2 object.
} }
\examples{ \examples{
\dontrun{ \dontrun{

7
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{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})} \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{""}) 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}" 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 will cause \code{statebins} to use the X axis title placeholder.
return a frame grob instead of a ggplot2 object.
} }
\examples{ \examples{
\dontrun{ \dontrun{

Loading…
Cancel
Save