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

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

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

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

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

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.

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.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.