How to create a bar chart using lattice package in R?

This recipe helps you create a bar chart using lattice package in R

Recipe Objective

How to create a bar chart using a lattice package in R? Bar charts are a graphical representation of categorical or discrete type of data. The data is represented in the form of rectangular bars either horizontally or vertically on the graph. Bar graphs can be used to compare different categories of data over a time period. Lattice is a data visualization and graphics package in R — graph_type (formula, data) This recipe demonstrates an example on a bar chart.

Step 1 - Install necessary package and library

install.packages("lattice") library(lattice)

Step 2 - Create a dataframe

data <- data.frame(x = c(10,5,6,15,20,19), type = c("A","B","C","D","E","F")) print(data)

"Data is :" 

print(data)
   x type
1 10    A
2  5    B
3  6    C
4 15    D
5 20    E
6 19    F

Step 3 - Plot a bar chart

syntax - barchart(x,data,main,xlab,ylab) x - the categorical data value data - the input data main - the title of the chart xlab - the title of the x axis ylab - the title of the y axis

barchart(x~type, data=data, main="Bar chart", xlab="x_values", ylab="type")

 " Output of the code is :"

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python