How to create a bubble chart using plotly in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to create a bubble chart using plotly in R?

This recipe helps you create a bubble chart using plotly in R

0

## Recipe Objective

Bubble plot is a type of scatter plot which not only uses cartesian coordinates to display the relation between two variables but also considers a third numerical variable represented by the size of the dots. Hence, we need three numerical variables, 2 for the x and y axis and 1 for the size of the dot. ​

In this recipe we are going to use Plotly package to plot the required bubble plot. Plotly package provides an interface to the plotly javascript library allowing us to create interactive web-based graphics entrirely in R. Plots created by plotly works in multiple format such as: ​

1. R Markdown Documents
2. Shiny apps - deploying on the web
3. Windows viewer

Plotly has been actively developed and supported by it's community. ​

This recipe demonstrates how to plot a bubble plot in R using plotly package. ​

## STEP 1: Loading required library and dataset

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

``` # Data manipulation package library(dplyr) library(tidyverse) # reading a dataset customer_seg = read.csv('R_129_Mall_Customers.csv') # selecting the required variables using the select() function customer_seg_var = select(customer_seg, Age, Annual.Income..k..,Spending.Score..1.100.) # summary of the selected variables glimpse(customer_seg_var) ```
```Observations: 200
Variables: 3
\$ 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 bubble plot using Plotly

We use the plot_ly() function to plot a bubble plot between annual income and spending score variables using Age as a dot size

Syntax: plot_ly( data = , x = , y = , type = "scatter", mode = "markers", marker = list(size = , opacity = ))

Where:

1. x = variable to be plotted in x axis
2. y = variable to be plotted in y axis
3. data = dataframe to be used
4. type = type of the chart
5. mode = This is the most important arguement for a bubble plot which indicates the mode to be of marker type in our case.
6. marker = provides us with the size and opacity input to the graph

Note:

1. The %>% sign in the syntax earlier makes the code more readable and enables R to read further code without breaking it.
2. We also use layout() function to give a title to the graph
``` fig <- plot_ly(x = ~Annual.Income..k..[1:20], y = ~Spending.Score..1.100.[1:20], data = customer_seg, type = "scatter", mode = "markers", marker = list(size = ~Age, opacity = 0.5)) %>% layout(title = 'Bubble Plot using Plotly') embed_notebook(fig) ```

#### Relevant Projects

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

##### Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

##### Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

##### Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

##### Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

##### Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

##### Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

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

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

##### Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.