How to create a boxplots using lattice package in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to create a boxplots using lattice package in R?

This recipe helps you create a boxplots using lattice package in R

0

## Recipe Objective

Box-plots are also known as box-whisker plots. They are a special type of plot which showcases complex numerical data in a compact manner. It is more informative than a strip chart as it also displays the interquartile range, median and min-max range. It is mainly used to find outliers in the dataset. ​

In this recipe we are going to use Lattice package to plot the required box plot. Lattice package provides powerful data visualisation functions which is mainly used for statistical graphics of multivariate data. It is pre-installed in R and is inspired by trellis graphics. ​

This recipe demonstrates how to plot a box plot in R using lattice package. ​

## STEP 1: Loading required library and dataset

Dataset description: It is the basic data about the customers going to the supermarket mall. The variables that we are interested in: Annual.Income (which is in 1000s), Spending Score and age

``` # Data manipulation package library(tidyverse) # Lattice package for data visualisation install.packages("lattice") library(lattice) # reading a dataset customer_seg = read.csv('R_146_Mall_Customers.csv') glimpse(customer_seg) ```
```Observations: 200
Variables: 5
\$ CustomerID              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,…
\$ Gender                  Male, Male, Female, Female, Female, Female, Fe…
\$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, 35…
\$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19…
\$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99, 1…
```

## STEP 2:Plotting a scatter plot using Lattice

We use the bwplot() function to plot a box plot between annual income and Gender variables.

Syntax: bwplot(x, data, main = )

where:

1. x = variables to be plotted
2. data = dataframe to be used
3. main = gives the title to the plot
``` customer_seg\$Gender = as.factor(customer_seg\$Gender) bwplot(Annual.Income..k.. ~ Gender, data = customer_seg, main = "Annual Income Box Plot") ```

#### Relevant Projects

##### Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

##### Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

##### 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.

##### Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

##### Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

##### German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

##### Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

##### Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

##### Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.