Â
ggplot2::geom_histogram()
   get_help() docs
The geom_histogram() function is part of the {ggplot2} package, which is part of the {tidyverse}.
The geom_histogram() function is used within {ggplot2} plots to create a histogram, which shows the distribution of a numeric continuous variable. The x-axis shows values of the variable, and the y-axis shows binned counts of how often values in a given range occur. By default, geom_histogram() uses 30 bins.
x: The numeric variable whose distribution the histogram visualizes.
y aesthetic instead of an x (but never both)color (colour): The outline color for the histogram barsfill: The fill for the histogram barssize: The width of the histogram bar outlinesbins: How many bins should be used (will be overridden by binwidth if both are provided)binwidth: The width of each bin (will override bins if both are provided)To use this function, you need to either first load the {ggplot2} library, or always use the function with ggplot2::geom_histogram() notation.
# Load the library
library(ggplot2)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
ggplot2::geom_histogram()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
# By default, histograms have no color outline, are filled with dark gray, and use 30 bins
# If you do not specify binning, you will get a _normal_ message telling you 30 were chosen
ggplot(msleep) +
aes(x = awake) +
geom_histogram()## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Change the binning with `bins`
ggplot(msleep) +
aes(x = awake) +
geom_histogram(bins = 10)# Change the binning with `binwidth`
ggplot(msleep) +
aes(x = awake) +
geom_histogram(binwidth = 0.5)# Include custom fill and color
ggplot(msleep) +
aes(x = awake) +
geom_histogram(color = "black", fill = "purple")## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Include custom fill and color, with a larger outline size
# When you have many arguments, placing on separate lines makes code easier to read and work with
ggplot(msleep) +
aes(x = awake) +
geom_histogram(color = "black",
fill = "purple",
size = 2)## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# A horizontal histogram using y instead of x aesthetic
ggplot(msleep) +
aes(y = awake) +
geom_histogram()## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.