How to Create Histogram with ggplot2 in R
To create a histogram in
ggplot2, use geom_histogram() inside ggplot() with your data and specify the variable with aes(x=). This plots the distribution of a numeric variable as bars representing frequency counts.Syntax
The basic syntax to create a histogram with ggplot2 is:
ggplot(data, aes(x = variable)): sets the data and the variable to plot on the x-axis.geom_histogram(): adds the histogram layer that counts and displays the frequency of values.
You can customize the number of bins with bins or the bin width with binwidth.
r
ggplot(data, aes(x = variable)) +
geom_histogram(bins = 30)Example
This example shows how to create a histogram of the mpg variable from the built-in mtcars dataset. It uses 15 bins to group the data.
r
library(ggplot2) ggplot(mtcars, aes(x = mpg)) + geom_histogram(bins = 15, fill = "skyblue", 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 creating histograms with ggplot2 include:
- Not specifying
aes(x=)insideggplot(), which causes errors or empty plots. - Using
geom_bar()instead ofgeom_histogram()for numeric data, which counts categories instead of numeric bins. - Choosing too few or too many bins, which can hide or exaggerate data patterns.
Always check your data type and adjust bins or binwidth for clear visualization.
r
## Wrong: Using geom_bar for numeric data # ggplot(mtcars, aes(x = mpg)) + geom_bar() ## Right: Use geom_histogram # ggplot(mtcars, aes(x = mpg)) + geom_histogram(bins = 10)
Quick Reference
| Function/Argument | Description |
|---|---|
| ggplot(data, aes(x = variable)) | Set data and variable for x-axis |
| geom_histogram() | Create histogram layer showing frequency counts |
| bins | Number of bins to divide data into (default 30) |
| binwidth | Width of each bin, alternative to bins |
| fill | Color inside bars |
| color | Color of bar borders |
| labs() | Add titles and axis labels |
Key Takeaways
Use geom_histogram() inside ggplot() with aes(x=variable) to create histograms.
Adjust bins or binwidth to control the granularity of the histogram.
Do not use geom_bar() for numeric histograms; it counts categories instead.
Customize colors and labels for clearer, more informative plots.
Always check your data type and mapping to avoid empty or incorrect plots.