Skip to contents

This function allows you to create facet column charts based on ggplot2's facet_wrap().

Usage

static_facet_column_chart(
  t,
  x,
  y,
  fill,
  pos = "dodge",
  facet,
  moe = NULL,
  est = NULL,
  ncol = 3,
  scales = "free",
  dec = 0,
  color = "pgnobgy_5",
  title = NULL,
  subtitle = NULL,
  source = "",
  alt = NULL,
  ...
)

Arguments

t

A tibble or dataframe in long form for plotting

x

The name of the variable you want plotted on the X-Axis

y

The name of the variable you want plotted on the Y-Axis

fill

The name of the variable you want the fill color of the bars to be based on

pos

The position of the columns, either "dodge" or "stack" - defaults to "dodge"

facet

The name of the variable to be the facets

moe

The name of the variable to be used for error bars, if desired - default to "NULL"

est

Type for the numeric values - enter "percent", "currency" or "number", defaults to "percent"

ncol

Value for the number of columns in your facet - defaults to 3

scales

Value for axis in facets, either "fixed" or "free" - defaults to "free"

dec

Number of decimal points in labels - defaults to 0

color

Name of color palette to use - generally defaults to "pgnobgy_5"

title

Title to be used for chart, if desired - defaults to "NULL"

subtitle

Sub-title to be used for chart, if desired - defaults to "NULL"

source

Source reference as character string, if desired - defaults to blank

alt

Text to be used for alt-text, if desired - defaults to "NULL"

...

additional arguments passed to generic_column_bar

Value

A static facet column (vertical bar) chart; based on facet_wrap()

Author

Christy Lam

Examples


library(dplyr)
# Read in the example data and filter to 2020 Population by Race by County
df <- mode_share_example_data %>%
      filter(Category=="Population by Race" & Year==2020) %>%
      filter(Race !="Total")

my_facet <- static_facet_column_chart(t = df,
                x = "Geography",
                y = "share",
                fill = "Geography",
                facet = "Race",
                moe = 'share_moe',
                ncol = 4,
                scales = "fixed",
                color = "pgnobgy_5",
                title = "Population by Race 2020",
                subtitle = "For counties in the Central Puget Sound Region",
                source = paste("Source: ACS 5-Year Estimates, table B03002",
                               "for King, Kitsap, Pierce and Snohomish counties.",
                               sep = "\n"))

 df2 <- mode_share_example_data %>%
      filter(Category=="Population by Race" & Year==2020) %>%
      filter(Geography != "Region", Race !="Total")

 my_facet2 <- static_facet_column_chart(t = df2,
                x = "Race",
                y = "share",
                fill = "Race",
                facet = "Geography",
                ncol = 2,
                moe = 'share_moe',
                scales = "fixed",
                color = "psrc_light",
                title = "Population by Race 2020",
                subtitle = "For counties in the Central Puget Sound Region",
                source = paste("Source: ACS 5-Year Estimates, table B03002",
                               "for King, Kitsap, Pierce and Snohomish counties.",
                               sep = "\n"))
                               
df3 <- mode_share_example_data %>%
     filter(Category == "Population by Race" & Year %in% c(2010, 2020)) %>%
     mutate(Year = as.character(Year)) %>%
     filter(Race !="Total")
 
my_facet3 <- static_facet_column_chart(t = df3,
                x = "Race",
                y = "share",
                fill = "Year",
                facet = "Geography",
                ncol = 2,
                moe = 'share_moe',
                scales = "fixed",
                color = "psrc_light",
                title = "Population by Race 2020",
                subtitle = "For counties in the Central Puget Sound Region",
                source = paste("Source: ACS 5-Year Estimates, table B03002",
                               "for King, Kitsap, Pierce and Snohomish counties.",
                               sep = "\n"))