← Back to R Course | Chapter 10: Data Visualization | Lesson 2 of 7

hist()

A histogram groups numbers into ranges and draws a bar for how many fall in each, like stacking blocks by height groups. It shows the shape of your data.

In this page:

  1. hist()
Syntax
r
hist(x, breaks = number_of_bins, main = "title", xlab = "label")

hist()

hist(x) draws a histogram showing the frequency distribution of a numeric vector, automatically choosing sensible bin widths (or accept an explicit breaks argument).

It's one of the fastest ways to visually inspect a dataset's distribution shape. Histograms are a first step in exploratory data analysis for any numeric variable.

Note: Pass breaks = n to hist() to control roughly how many bins the histogram uses.

Example: hist()

markup
set.seed(1)
values <- rnorm(100, mean = 50, sd = 10)

cat("Summary of values that hist() would visualize:\n")
print(summary(values))
cat("hist(values) would draw a histogram of this distribution\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing a data frame or a character vector, which gives x must be numeric.
  2. Reading the number of bars as data, when it depends on the breaks setting and can hide the shape.
  3. Confusing hist() with barplot(), when a histogram groups a numeric range into bins.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.