Bar Chart

Author

Francisco Zambrano

Published

July 1, 2023

Installation

Development version from GitHub:

GitHub: https://github.com/zpio/ggexplorer

Show/Hide code
remotes::install_github("zpio/ggexplorer")
Show/Hide code
library(ggexplorer)
library(dplyr)

Examples

Diamonds dataset

Show/Hide code
diamonds <- ggplot2::diamonds
diamonds

Basic Chart

Show/Hide code
bar_chart(ggplot2::diamonds, x = clarity)

Proportions

Show/Hide code
bar_chart(diamonds, x = clarity, prop = TRUE)

Flip

Show/Hide code
bar_chart(diamonds, x = clarity, flip = TRUE)

Top

Show/Hide code
bar_chart(diamonds, x = clarity, top = 5)

Fill

Show/Hide code
bar_chart(diamonds, x = clarity, fill = "blue")

Fill by

Show/Hide code
bar_chart(diamonds, x = clarity, fill_var = cut)

Show/Hide code
bar_chart(diamonds, x = clarity, fill_var = clarity)

Legend

Show/Hide code
bar_chart(diamonds, x = clarity, fill_var = clarity,
          legend_position = "none")

Position

Show/Hide code
bar_chart(diamonds, x = clarity, fill_var = cut,
          position = "stack", flip = TRUE)

Show/Hide code
bar_chart(diamonds, x = clarity, fill_var = cut,
          position = "dodge", flip = TRUE)

Show/Hide code
bar_chart(diamonds, x = clarity, fill_var = cut,
          position = "fill", flip = TRUE)

Facet

Show/Hide code
bar_chart(diamonds, x = clarity, facet_var = cut)

Show/Hide code
bar_chart(diamonds, x = clarity, facet_var = cut, 
          fill_var = cut, legend_position = "none")

Show/Hide code
bar_chart(diamonds, x = clarity, facet_var = color, 
          fill_var = cut, position = "stack", 
          top = 5)

Ncol

Show/Hide code
bar_chart(diamonds, x = clarity, facet_var = cut, 
          ncol = 2)

Scales

Show/Hide code
bar_chart(diamonds, x = clarity, facet_var = cut, ncol = 2,
          scales = "fixed")

Highlight

Show/Hide code
bar_chart(diamonds, x = clarity, 
          highlight = c("SI1", "VS1"))

Show/Hide code
bar_chart(diamonds, x = clarity, facet_var = cut,
          highlight = c("SI1", "VS1"))

Stat: Identity

Show/Hide code
sales <- data.frame(
  categories = c("Ohio","Illinois", "Michigan", "Florida", "Texas", "New York", "California"),
  sales = c(10000, 20000, 15000, 17000, 25000, 30000, 40000),
  yoy = c(-0.45, -0.02, 0.15, 0.25, 0.35, -0.45, -0.15)
)
sales
Show/Hide code
bar_chart(sales, stat = "identity", 
          x = categories, y = sales)

Label

Show/Hide code
bar_chart(sales, stat = "identity", label_var = sales,
          x = categories, y = sales, flip = TRUE)

Palette

See Details for available palettes (ordered-sequential):

https://jrnold.github.io/ggthemes/reference/scale_colour_gradient_tableau.html

Show/Hide code
bar_chart(sales, stat = "identity", fill_var = sales,
          x = categories, y = sales, 
          label_var = sales, flip = TRUE,
          palette_gradient = 'Green-Gold',
          legend_position = "none")

Show/Hide code
p1 <- bar_chart(
  sales, stat = "identity", 
  x = categories, y = yoy, fill_var = yoy,
  label_var = yoy, flip = TRUE, expand_mult = c(0.20, 0.20)
) 

p1 +
  ggplot2::scale_fill_gradient2(
    low = "red3",
    mid = "gray90",
    high = "green3",
    midpoint = 0
  )

Show/Hide code
sales <- sales %>% 
  mutate(
    positive = ifelse(yoy >= 0, "positive", "negative")
  )

p1 <- bar_chart(
  sales, stat = "identity", 
  x = categories, y = yoy, fill_var = positive,
  label_var = yoy, flip = TRUE, expand_mult = c(0.20, 0.20)
) 

p1 +
  ggplot2::scale_fill_manual(
    values = c("positive" = "green3", "negative" = "red3"),
    guide = "none"
  )

Expand

Show/Hide code
bar_chart(sales, stat = "identity", 
          x = categories, y = yoy, fill_var = yoy,
          label_var = yoy, flip = TRUE,
          expand_mult = c(0.20, 0.20))

Label Color

Show/Hide code
mtcars <- mtcars %>% 
  mutate(across(c(am, carb, cyl, gear, vs), as.factor))

data <- mtcars %>%
  count(am, cyl) %>%
  group_by(am) %>%
  mutate(
    pct = round(n/sum(n),3),
    label = paste(pct*100, '%')
  )
data
Show/Hide code
bar_chart(data, x = am, y = pct, stat = "identity", width = 0.5,
          fill_var = cyl, position = "fill", flip = TRUE,
          label_var = label, label_color = "white", label_size = 3.5)