ggplot2::geom_bar()
   get_help() docs


Description

The geom_bar() function is part of the {ggplot2} package, which is part of the {tidyverse}.

The geom_bar() function is used within {ggplot2} plots to create barplots of counts of a given categorical variable. When you call geom_bar(), {ggplot2} will automatically count the number of observations in each category of your primary variable and then plot that calculated value.

To instead plot bars whose height equals an existing literal column in the dataset, it is recommended to use ggplot2::geom_col().

Required aesthetics:

  • x: A categorical variable to visualize bars showing counts of
    • Note that it is also allowed to make a horizontal barplot by providing a y aesthetic instead of an x (but never both)

Commonly used optional aesthetics:

  • color (colour): The outline color for the bars
  • fill: The fill for the bars
  • size: The width of the bar outlines

To use this function, you need to either first load the {ggplot2} library, or always use the function with ggplot2::geom_bar() notation.

# Load the library
library(ggplot2)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
ggplot2::geom_bar()

Examples

The examples below use the msleep dataset. Learn more about this dataset with get_help("msleep").

# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
##   name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt  bodywt
##   <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>   <dbl>
## 1 Owl … Aotus omni  Prim… <NA>                17         1.8      NA       7    0.0155    0.48 
## 2 Moun… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA         1.35 
## 3 Grea… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029   0.019
## 4 Cow   Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423   600    
## 5 Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6 NA         3.85 
## 6 Nort… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA        20.5


# Barplot of counts of categories in the vore column
# Note to remove the NAs, you must manipulate the data first
ggplot(msleep) + 
  aes(x = vore) + 
  geom_bar()


# Barplot of counts of categories in the vore column, with added custom color, size, and fill
# Note to remove the NAs, you must manipulate the data first
ggplot(msleep) + 
  aes(x = vore) + 
  geom_bar(color = "goldenrod",
           size = 2, 
           fill = "blue")


# Barplot of counts of categories in the vore column, fill mapped to vore
# Note to remove the NAs, you must manipulate the data first
ggplot(msleep) + 
  aes(x = vore, fill = vore) + 
  geom_bar()


# Barplot of counts of categories in the vore column, using forcats::fct_infreq() to automatically order vores by frequency on the x-axis
# It best practice to also update the x-axis title 
# Note to remove the NAs, you must manipulate the data first
ggplot(msleep) + 
  aes(x = fct_infreq(vore)) + 
  geom_bar() + 
  labs(x = "vore")


## CAUTION!! `geom_bar()` can only take either `x` or `y` in its default (non-advanced) usage 
## This code therefore does not work and results in blank canvas with an error
ggplot(msleep) + 
  aes(x = vore, y = awake) + 
  geom_bar() 
## Error in `f()`:
## ! stat_count() can only have an x or y aesthetic.


## CAUTION!! `geom_bar()` is used to plot categorical data, NOT numeric data!
## Plotting a numeric continuous variable results in confusion. 
## When plotting continuous data as bars, use `ggplot2::geom_histogram()` instead
ggplot(msleep) + 
  aes(x = awake) + 
  geom_bar() 


# Combined barplot of vore and conservation, in default "stacked" layout
ggplot(msleep) + 
  aes(x = vore, fill = conservation) + # Change the fill for a grouped barplot
  geom_bar()

# Combined barplot of vore and conservation, in default "stacked" layout
ggplot(msleep) + 
  aes(x = conservation, fill = vore) + # Flipped from previous example
  geom_bar()


# Use the "dodged" position for grouped barplot instead of default "stacked"
ggplot(msleep) + 
  aes(x = conservation, fill = vore) +
  geom_bar(position = position_dodge()) # This argument makes bars side-by-side, taking up all the space by default


# Use the "dodged" position for grouped barplot instead of default "stacked"
# Include the argument preserve = "single" to make all bars the same width
ggplot(msleep) + 
  aes(x = conservation, fill = vore) +
  geom_bar(position = position_dodge(preserve = "single")) 


# Example of a horizontal barplot: Use `y` instead of `x`
ggplot(msleep) + 
  aes(y = vore, fill = vore) + 
  geom_bar()