0
0
R-programmingHow-ToBeginner ยท 3 min read

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 bins or the bin width with binwidth.
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 x aesthetic inside aes(), which causes errors or empty plots.
  • Using geom_histogram() on categorical data instead of continuous data.
  • Not adjusting bins or binwidth to 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

ArgumentDescriptionExample
dataData frame containing variablesmtcars
aes(x = variable)Maps variable to x-axisaes(x = mpg)
binsNumber of bins in histogrambins = 20
binwidthWidth of each binbinwidth = 2
fillFill color of barsfill = "blue"
colorBorder color of barscolor = "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.