Table of contents
1.
Introduction
2.
R – Histograms
3.
Syntax of Histogram in R
4.
Creating a simple Histogram in R
5.
Range of X and Y Values
6.
Using Histogram Return Values for Labels Using text()
7.
Histogram Using Non-Uniform Width
8.
Example: Non-Uniform Width Histogram
9.
Frequently Asked Questionss
9.1.
Why use non-uniform bin widths in histograms?
9.2.
How do I choose the number of bins for my histogram in R?
9.3.
How to draw a histogram with 2 variables in R?
9.4.
Can I add multiple data series to a single histogram in R?
10.
Conclusion
Last Updated: Oct 7, 2024
Easy

Histogram in R Programming

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Histograms are powerful tools for visualizing data distributions, commonly used in statistical analysis to understand the frequency of data points within specified ranges. They help in identifying patterns, outliers, and the overall spread of data, making them indispensable in data analysis. 

Histogram in R Programming

This article will guide you through the basics of creating histograms in R programming, a language renowned for its statistical capabilities. We'll cover how to craft simple histograms, manipulate the range of values, add labels, and adjust bin widths for more insightful data representation. 

R – Histograms

In R, histograms are created using the hist() function, which takes a vector of values as input and plots the frequencies of these values across different intervals.

The hist() function is versatile, allowing for customization of various parameters such as the number of bins, color, labels, and more. This flexibility makes it a powerful tool for data exploration and analysis, enabling you to quickly identify patterns, trends, and potential outliers in your data.

Syntax of Histogram in R

hist(v, main, xlab, ylab, xlim, ylim, breaks, col, border)
S.NoParameterDescription
1vIt is a vector that contains numeric values.
2mainIt indicates the title of the chart.
3colIt is used to set the color of the bars.
4borderIt is used to set the border color of each bar.
5xlabIt is used to describe the x-axis.
6ylabIt is used to describe the y-axis.
7xlimIt is used to specify the range of values on the x-axis.
8ylimIt is used to specify the range of values on the y-axis.
9breaksIt is used to mention the width of each bar.

Creating a simple Histogram in R

Creating a histogram in R is straightforward. Let's start with a simple example using a dataset on heights of individuals. We'll generate a basic histogram to visualize the distribution of heights and then explore how to customize our histogram to gain more insights from the data.

# Sample data: Heights of individuals in cm
heights <- c(162, 170, 168, 174, 178, 165, 171, 169, 175, 180, 182, 167, 172, 177, 183)

# Creating a basic histogram
hist(heights, main="Histogram of Heights", xlab="Height (cm)", ylab="Frequency", col="lightblue", border="black")

Output:

output


In this example, heights is a vector containing the heights of individuals. The hist() function creates a histogram where the x-axis represents height intervals (bins), and the y-axis represents the frequency of individuals falling within those intervals. The main, xlab, ylab, col, and border arguments are used to add titles, label axes, and customize the histogram's appearance.

Range of X and Y Values

Adjusting the range of x and y values in a histogram can significantly enhance the visualization by focusing on specific data segments and improving readability. In R, you can control these ranges using the xlim and ylim parameters within the hist() function. This allows for a more detailed analysis of data distributions within a particular range, making it easier to identify patterns or anomalies that might be of interest.

For instance, if we're only interested in the heights between 165 cm and 180 cm in our previous example, we can set the xlim parameter to restrict the x-axis to this range. Similarly, if we want to focus on frequency counts that fall within a certain range, we can adjust the ylim parameter accordingly.

Here's how you can adjust the range of x and y values in your histogram:

# Adjusting the range of x and y values

hist(heights, main="Histogram of Heights (Focused Range)", xlab="Height (cm)", ylab="Frequency", col="lightgreen", border="black", xlim=c(165, 180), ylim=c(0, 5))


In this modified example, the xlim=c(165, 180) argument narrows down the histogram's x-axis to display only the heights between 165 cm and 180 cm. The ylim=c(0, 5) argument adjusts the y-axis to focus on frequency counts between 0 and 5. This focused view can help in closely examining the distribution of data within a specified range, making it easier to draw insights.

Using Histogram Return Values for Labels Using text()

In R, the hist() function not only plots a histogram but also returns a list of values that describe the histogram, including the breakpoints of the bins, the counts in each bin, and the density values. This feature can be incredibly useful for adding informative labels directly to the histogram, enhancing its interpretability.

Let's use the return values from the hist() function to add labels to our histogram that display the count of individuals within each bin. We'll use the text() function to place these labels above the corresponding bars in the histogram, providing a clear and immediate understanding of the data distribution.

Here's an example of how to accomplish this:

heights <- c(160, 162, 165, 167, 170, 172, 175, 177, 180, 182, 185, 160, 168, 174, 179, 183, 161, 164, 171, 178, 181, 184, 160, 169, 173)
# Creating a histogram and capturing its return values
hist_data <- hist(heights, main="Histogram of Heights with Labels", xlab="Height (cm)", ylab="Frequency", col="lightcoral", border="black", ylim=c(0, 5))

# Adding count labels above each bar
text(x=hist_data$mids, y=hist_data$counts, labels=hist_data$counts, pos=3, cex=0.8, col="blue")

Output:

output

In this example, hist_data captures the return values of the hist() function, which includes mids (the midpoints of each bin) and counts (the number of observations in each bin). The text() function then uses these values to place labels above each bar, with pos=3 indicating that the text should be positioned above the bars. The cex parameter adjusts the text size for better visibility, and col changes the text color to blue for contrast.

This method of adding labels can provide immediate insights into the exact number of observations within each bin, making the histogram more informative and useful for data analysis.

Histogram Using Non-Uniform Width

Creating histograms with non-uniform bin widths in R can be particularly useful when dealing with data that is not evenly distributed or when you want to emphasize certain ranges of your data more than others. This approach allows for a more nuanced representation of the data, highlighting specific characteristics that might be lost in a histogram with uniform bin widths.

To create a histogram with non-uniform bin widths, you can use the breaks argument in the hist() function. This argument allows you to specify the exact breakpoints between bins, giving you full control over the size of each bin.

Here's an example to illustrate how you can create a histogram with varying bin widths:

# Sample heights data (in cm)
heights <- c(160, 162, 165, 167, 170, 172, 175, 177, 180, 182, 185, 160, 168, 174, 179, 183, 161, 164, 171, 178, 181, 184, 160, 169, 173)
# Defining custom bin widths
break_points <- c(160, 165, 170, 175, 180, 185)
# Creating a histogram with non-uniform bin widths
hist(heights, breaks=break_points, main="Histogram with Non-Uniform Bin Widths", xlab="Height (cm)", ylab="Frequency", col="lightgoldenrod", border="black")

Output:

output


In this example, the break_points vector specifies the boundaries of the bins. The first bin will include heights from 160 to 165 cm, the second bin from 165 to 170 cm, and so on. By setting breaks=break_points, the hist() function will create bins according to these specified breakpoints, resulting in a histogram where each bin can have a different width.

This method is particularly useful for highlighting specific ranges within your data, such as ranges with higher or lower data density, and can provide more insight than a standard histogram with equal-sized bins.

Example: Non-Uniform Width Histogram

To demonstrate the practical application of histograms with non-uniform bin widths, let's consider a dataset that represents the scores of students in a particular exam. The scores range from 0 to 100, but we're especially interested in the distribution of scores around the passing mark, say 50, and the distinction mark, say 75. We'll use non-uniform bin widths to give more granularity around these critical values.

First, we'll define our dataset and the custom breakpoints that emphasize the ranges of interest:

# Scores of students in an exam
scores <- c(23, 45, 56, 78, 98, 33, 54, 76, 87, 45, 67, 89, 90, 55, 60, 70, 80, 85, 95, 40, 65, 75, 58, 82, 91)
# Custom breakpoints for the histogram
break_points <- c(0, 45, 50, 60, 70, 75, 85, 100)
# Creating the histogram with non-uniform bin widths
hist(scores, breaks=break_points, main="Exam Score Distribution", xlab="Scores", ylab="Number of Students", col="skyblue", border="darkgray")

 

Output:

output

In this histogram, the bin widths are deliberately chosen to be narrower around the scores of 50 and 75 to provide more detail about how many students are just passing or achieving distinction. Wider bins are used for other score ranges where we might not need as much granularity.

This approach could be helpful for teachers, for instance, to quickly identify how many students are on the edge of passing or achieving excellence, potentially informing decisions about extra support or recognition. The use of non-uniform bin widths here makes the histogram a more powerful tool for data analysis in this context.

By customizing bin widths, you can tailor your histogram to emphasize the parts of the data that are most relevant to your analysis or presentation, making your findings clearer and more impactful.

Frequently Asked Questionss

Why use non-uniform bin widths in histograms?

Non-uniform bin widths allow for more detailed analysis in areas of interest within the data, highlighting nuances and patterns that might be overlooked with uniform bins. They are particularly useful for datasets with uneven distributions or specific ranges of heightened interest.

How do I choose the number of bins for my histogram in R?

The choice of bin number can depend on the dataset size and the level of detail you wish to observe. While R's default setting often provides a reasonable starting point, experimenting with different bin counts using the breaks argument in the hist() function can help identify the most informative representation of your data.

How to draw a histogram with 2 variables in R?

To draw a histogram with 2 variables in R, use the ggplot2 package. Create a plot with ggplot() and geom_histogram(), and use aes(fill=variable) to differentiate the variables. This overlays histograms for visual comparison.

Can I add multiple data series to a single histogram in R?

Yes, you can overlay histograms for multiple data series in R to compare distributions, but it requires careful management of colors, transparency, and potentially using the add parameter in the hist() function for clarity. However, for complex comparisons, alternative visualizations like density plots or box plots might be more effective.

Conclusion

Histograms in R are a fundamental tool for exploring and understanding the distribution of your data. Starting from creating simple histograms to customizing them with non-uniform bin widths and adding informative labels, you can extract significant insights and communicate your findings effectively. Whether adjusting the range of x and y values to highlight specific data segments or employing non-uniform bin widths to focus on areas of interest, histograms offer a versatile means of data analysis. Remember, the key to effective histogram usage lies in choosing the right bin sizes and ranges to suit your data's story, enabling a deeper understanding of the underlying patterns and trends.

You can refer to our guided paths on Code360. You can check our course to learn more. Also, check out our Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass