How to change histogram bins in R?

This recipe helps you change histogram bins in R

Recipe Objective

How to change histogram bins in R? A histogram plots a graph of continuous value variable with their corresponding frequencies. The data is plotted as bars of different heights. The difference between bar plot and histogram is that the bar charts are plotted for discrete data, whereas a histogram is plotted for continuous data. A histogram provides a visual representation of the distribution of data, which helps us to know whether the data is symmetric or skewed in nature. It also displays if there are any outliners in my dataset. breaks parameter changes the histogram bins in an R code. This recipe demonstrates an example of histogram.

Step 1 - Define a vector

Syntax for histogram - hist(x, main, xlab, xlim, ylim, breaks,col) where, x - defined vector main - title of the histogram xlab - title to the X axis xlim - range of values on X -axis ylim - range of values on Y -axis breaks - mention the width/bin size of each bar col - colour of the bars

x <- c(1,5,6,3,9,7,2,8,9,7,5,1)

Step 2 - Plot a histogram

Here , the histogram returns the frequency bars of the given vector for a continuous range of values.

hist(x,main="Histogram plot",xlab="data points for x",xlim=c(0,10),ylim=c(0,5),breaks=5,col='blue') # number of bins = 5 hist(x,main="Histogram plot",xlab="data points for x",xlim=c(0,10),ylim=c(0,5),breaks=8,col='red') # number of bins = 8

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker