How to add dropdowns to chart using plotly in R

This recipe helps you add dropdowns to chart using plotly in R

Recipe Objective

How to add dropdowns to chart using plotly ?

Plotly package is a highly interactive package in R, which allows the users to control/change the type of plots using options like dropdowns, buttons, sliders, etc. The dropdown option allows the users to change the type of chart, the color of the graph as required. This recipe demonstrates an example of how to add dropdowns to chart using Plotly in R.

Step 1 - Install the necessary library

install.packages("plotly") library(plotly)

Step 2 - Define the data

m1 <- matrix(c(1:4), nrow = 2, ncol = 2, byrow = TRUE) print(m1)

 "matrix is :" 
     [,1] [,2]
[1,]    1    2
[2,]    3    4

data <- data.frame(x = c(10,20,30,40,50,60,70,80,90,100), y = c(2,9,7,8,10,5,3,6,7,3))

Step 3 - Use dropdown for charts

colnames(data) <- c("x", "y") figure <- plot_ly(data, x = ~x, y = ~y, alpha = 0.3) figure <- figure %>% add_markers(marker = list(line = list(color = "blue", width = 5 ))) figure <- figure %>% layout( title = "Drop-down for charts", xaxis = list(domain = c(0.1, 1)), yaxis = list(title = "y"), updatemenus = list( list( y = 0.8, buttons = list( list(method = "restyle", args = list("type", "scatter"), label = "scatter"), list(method = "restyle", args = list("type", "histogram"), label = "Histogram"))) )) figure {"mode":"full","isActive":false}

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

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 Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .