Title: | Create United States Uniform Cartogram Heatmaps |
---|---|
Description: | The cartogram heatmaps generated by the included methods are an alternative to choropleth maps for the United States and are based on work by the Washington Post graphics department in their report on "The states most threatened by trade" (<http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/>). "State bins" preserve as much of the geographic placement of the states as possible but have the look and feel of a traditional heatmap. Functions are provided that allow for use of a binned, discrete scale, a continuous scale or manually specified colors depending on what is needed for the underlying data. |
Authors: | Bob Rudis [aut, cre] , Harold Gil [ctb] (fix for show.guide), Brian Adams [ctb] (theme testing & feedback), Thomas Wood [ctb] (Significant suggestions & testing that made new features possible), Mathew Kiang [ctb] (Minor fix for NA handling) |
Maintainer: | Bob Rudis <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.4.0 |
Built: | 2024-10-14 02:45:57 UTC |
Source: | https://gitlab.com/hrbrmstr/statebins |
The cartogram heatmaps generated by the included methods are an alternative to choropleth maps for the United States and are based on work by the Washington Post graphics department in their report on "The states most threatened by trade" (http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/). "State bins" preserve as much of the geographic placement of the states as possible but have the look and feel of a traditional heatmap. Functions are provided that allow for use of a binned, discrete scale, a continuous scale or manually specified colors depending on what is needed for the underlying data.
Bob Rudis ([email protected])
Pass in a data frame of states and values and let this do the work. It enables
easy faceting and makes it simpler to have a uniform legend across all the
plots.
There are two special/critical aes()
mappings:
state
(so the geom knows which column to map the state names/abbrevs to)
fill
(which column you're mapping the filling for the squares with)
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 ) GeomStatebins
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 ) GeomStatebins
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
border_col |
border color of the state squares, default " |
border_size |
thickness of the square state borders |
lbl_size |
font size (relative) of the label text |
dark_lbl , light_lbl
|
colrs to be uses when the label should be dark or light. The function automagically computes when this should be. |
radius |
the corner radius |
... |
other arguments passed on to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
An object of class GeomStatebins
(inherits from Geom
, ggproto
, gg
) of length 7.
## Not run: library(statebins) library(cdcfluview) library(hrbrthemes) library(tidyverse) flu <- ili_weekly_activity_indicators(2017) ggplot(flu, aes(state=statename, fill=activity_level)) + geom_statebins() + coord_equal() + viridis::scale_fill_viridis( name = "ILI Activity Level ", limits=c(0,10), breaks=0:10, option = "magma", direction = -1 ) + facet_wrap(~weekend) + labs(title="2017-18 Flu Season ILI Activity Level") + theme_statebins(base_family = font_ps) + theme(plot.title=element_text(size=16, hjust=0)) + theme(plot.margin = margin(30,30,30,30)) ## End(Not run)
## Not run: library(statebins) library(cdcfluview) library(hrbrthemes) library(tidyverse) flu <- ili_weekly_activity_indicators(2017) ggplot(flu, aes(state=statename, fill=activity_level)) + geom_statebins() + coord_equal() + viridis::scale_fill_viridis( name = "ILI Activity Level ", limits=c(0,10), breaks=0:10, option = "magma", direction = -1 ) + facet_wrap(~weekend) + labs(title="2017-18 Flu Season ILI Activity Level") + theme_statebins(base_family = font_ps) + theme(plot.title=element_text(size=16, hjust=0)) + theme(plot.margin = margin(30,30,30,30)) ## End(Not run)
Pass in a data frame and get back a square choropleth.
statebins( state_data, state_col = "state", value_col = "value", 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, ... )
statebins( state_data, state_col = "state", value_col = "value", 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, ... )
state_data |
data frame of states and values to plot |
state_col |
column name in |
value_col |
column name in |
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. |
font_size |
font size (default = |
state_border_col |
default " |
state_border_size |
border size |
round |
rounded corners (default: |
radius |
if |
ggplot2_scale_function |
ggplot2 scale function to use. Defaults to |
... |
additional parameters to the scale function |
The state_col
and value_col
parameters default to state
and value
. That means
if you name the columns you want to plot with those names, you can forego passing them
in. Othersise, use "strings"
.
A handy feature of this function is that you can specify a dark_label
color
and a light_label
color. What does that mean? Well, you also pass in the
color scale function you're going to use and statebins
will apply it and use
that information to determine what the tile color is and — if it's "dark" it will
use the light_label
and if it's "light" it will use the dark_label
color. That
means the labels will never blend in to the background (as long as you specify
decent label colors).
You can customize the scale function you pass in by using name parameters. All named
parameters not used by statebins()
itself get passed to the scale function.
ggplot2 object
data(USArrests) USArrests$state <- rownames(USArrests) statebins(USArrests, value_col="Assault", name = "Assault") + theme_statebins(legend_position="right")
data(USArrests) USArrests$state <- rownames(USArrests) statebins(USArrests, value_col="Assault", name = "Assault") + theme_statebins(legend_position="right")
Clears out most of the cruft. Builds off of theme_bw()
theme_statebins(legend_position = "bottom", base_size = 11, base_family = "")
theme_statebins(legend_position = "bottom", base_size = 11, base_family = "")
legend_position |
fills in |
base_family , base_size
|
same as |