How to Use geom_histogram in ggplot2 for Histograms
Use
geom_histogram() in ggplot2 to create histograms by mapping a continuous variable to the x-axis. You specify the data and aesthetics inside ggplot(), then add geom_histogram() to plot the frequency distribution of the data.Syntax
The basic syntax for geom_histogram() is:
ggplot(data, aes(x = variable)): sets the data and the variable to plot on the x-axis.geom_histogram(): adds the histogram layer.- You can customize the number of bins with
binsor the bin width withbinwidth.
r
ggplot(data, aes(x = variable)) + geom_histogram(bins = 30, fill = "blue", color = "black")
Example
This example shows how to create a histogram of the mpg variable from the built-in mtcars dataset. It uses 15 bins and colors the bars blue with black borders.
r
library(ggplot2) ggplot(mtcars, aes(x = mpg)) + geom_histogram(bins = 15, fill = "blue", color = "black") + labs(title = "Histogram of Miles Per Gallon", x = "Miles Per Gallon", y = "Count")
Output
A histogram plot showing the distribution of mpg values with 15 blue bars outlined in black.
Common Pitfalls
Common mistakes when using geom_histogram() include:
- Not specifying the
xaesthetic insideaes(), which causes errors or empty plots. - Using
geom_histogram()on categorical data instead of continuous data. - Not adjusting
binsorbinwidthto suit the data, resulting in too few or too many bars.
Example of a wrong and right way:
r
# Wrong: missing x aesthetic
library(ggplot2)
ggplot(mtcars) +
geom_histogram()
# Right: specify x inside aes
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(bins = 10)Output
Error or empty plot for wrong code; histogram with 10 bins for right code.
Quick Reference
| Argument | Description | Example |
|---|---|---|
| data | Data frame containing variables | mtcars |
| aes(x = variable) | Maps variable to x-axis | aes(x = mpg) |
| bins | Number of bins in histogram | bins = 20 |
| binwidth | Width of each bin | binwidth = 2 |
| fill | Fill color of bars | fill = "blue" |
| color | Border color of bars | color = "black" |
Key Takeaways
Always map a continuous variable to x inside aes() when using geom_histogram().
Adjust bins or binwidth to control the histogram's bar count and appearance.
Use fill and color to customize bar colors and borders for better visuals.
Avoid using geom_histogram() with categorical variables; use geom_bar() instead.
Check for missing aesthetics to prevent empty or error plots.