Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Histograms for Distribution Analysis | Section
Information Visualization with ggplot2 in R
Section 1. Chapter 4
single

single

bookHistograms for Distribution Analysis

Swipe to show menu

Histograms are a fundamental tool in data analysis for visualizing the distribution of numeric variables. By dividing the range of data into intervals, or "bins," and counting how many data points fall into each bin, a histogram provides a visual summary of how values are spread, clustered, or dispersed. This makes it easier to spot patterns, outliers, and the overall shape of your data, which is crucial before applying statistical models or drawing conclusions.

123456789101112131415161718
library(ggplot2) # Sample data: heights of individuals heights <- c(160, 165, 170, 172, 168, 175, 180, 178, 182, 169, 174, 177, 185, 172, 167) # Basic histogram ggplot(data.frame(heights), aes(x = heights)) + geom_histogram(binwidth = 5, fill = "skyblue", color = "black") + labs(title = "Histogram of Heights", x = "Height (cm)", y = "Count") # Adjusting bin width for more or fewer bins ggplot(data.frame(heights), aes(x = heights)) + geom_histogram(binwidth = 2, fill = "orange", color = "black") + labs(title = "Histogram of Heights (Smaller Bin Width)", x = "Height (cm)", y = "Count")
copy

When you examine a histogram, the shape of the bars tells you about the underlying distribution of your data. If the histogram is roughly symmetrical and bell-shaped, your data may be normally distributed. If the bars stretch further to the right, with a long tail, the distribution is right-skewed (positively skewed); if the tail is to the left, it is left-skewed (negatively skewed). You may also observe modality:

  • A single prominent peak (unimodal);
  • Two peaks (bimodal);
  • More than two peaks, indicating clusters or subgroups in your data.

Interpreting these shapes helps you decide on further analysis steps and informs you about potential data issues or interesting patterns.

Task

Swipe to start coding

Plot a histogram to visualize the distribution of the temperatures variable. Use ggplot2 to create the histogram.

  • Use the temperatures variable as the data input.
  • Display the distribution using a histogram.
  • Set the bin width to 2.
  • Use any fill color and outline color for the bars.
  • Add appropriate axis labels and a title.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt