ggplot2::geom_col()
   get_help() docs


Description

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

The geom_col() function is used within {ggplot2} plots to create barplots where the bar heights’ are directly taken from a variable in the dataset.

To instead plot bars whose heights are equal to the counted number of categories, it is recommended to use ggplot2::geom_bar().

Required aesthetics:

  • x: A categorical variable to visualize bars across
  • y: A variable that determines the height of the bars
    • Note that these can be switched for a horizontal bar plot

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_col() notation.

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

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

Examples

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

# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Cheetah           Acinonyx      11.9 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Northern fur seal Callorhinus   15.3 NA     
## 8 Red fox           Vulpes        14.2  0.0504
## 9 Tiger             Panthera       8.2 NA


# Plot the literal values of `awake` for each mammal in the carnivores dataset
ggplot(carnivores) + 
  aes(x = name, y = awake) + 
  geom_col()


# Plot the literal values of `awake` for each mammal in the carnivores dataset, with additional stylings
ggplot(carnivores) + 
  aes(x = name, y = awake) + 
  geom_col(color = "goldenrod",
           size = 2,
           fill = "blue")


# Horizontally plot the literal values of `awake` for each mammal in the carnivores dataset
ggplot(carnivores) + 
  aes(x = awake, y = name) + # note that these were switched to make it horizontal
  geom_col()


# Use forcats::fct_reorder() to order the names by their awake values for a cleaner look
# It best practice to also update the x-axis title when using forcats::fct_reorder()
ggplot(carnivores) + 
  aes(x = fct_reorder(name, awake), 
      y = awake) + 
  geom_col() + 
  labs(x = "name")