How to plot histogram in R?

This recipe helps you plot histogram in R

Recipe Objective

Histogram is a type of visual representation where frequency distribution of any numerical data is plotted. The plot includes frequency on the y-axis and bars on the x-axis where the height of the bars corresponds to it's frequency. The bars indicate the range of values which are also known as bins.

This recipe demonstrates how to plot a histogram in R-language with an example dataset

Build Expedia Hotel Recommendation System using Machine Learning

STEP 1: Loading required library and dataset

# Data manipulation package library(tidyverse) ​ # reading a dataset customer_seg = read.csv('R_99_Mall_Customers.csv') ​ glimpse(customer_seg)

Rows: 200
Columns: 5
$ CustomerID              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1...
$ Gender                  Male, Male, Female, Female, Female, Female, ...
$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, ...
$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, ...
$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99,...

Step 2: Plotting a histogram of Age variable

Using hist() function to plot distribution of Age variable

Syntax:

hist(X, type, col, main, xlab, ylab)

where,

  1. X = Variable - dataframe
  2. col = colour of bar
  3. main = title of the graph
  4. xlab = xlabel of the plot
  5. breaks = binsize

hist(customer_seg$Age, col = 'red', main = 'Histogram of Age', xlab = 'Age', breaks = 5)

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Multimodal RAG System using AWS Bedrock and FAISS
In this LLM RAG Project, you will learn to build a Multimodal RAG system for a restaurant aggregator app, integrating text and visuals to deliver personalized food recommendations using advanced technologies like Amazon S3, Amazon Bedrock, and FAISS.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Build a Customer Support Agent using OpenAI and AzureML
In this LLM Project, you will build an intelligent customer support agent using OpenAI and Azure ML to automate ticket categorization, prioritization, and response generation.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

OSZAR »