Plotly R vs Python Dash: Best of the bunch

by Apr 19, 2021IoT Continuous Integration

Introduction

Plotly R vs Python Dash both are managed and developed by Plotly for data visualization. But there are some differences in the way you use them.

The R graphing library from Plotly allows users to create interactive, publication-quality graphs. Line plots, scatter plots, field charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, and 3D (WebGL based) charts are all examples that we can create using Plotly R.
Plotly.R is open source and free, moreover you can access the source code, report bugs, and further contribute to it on GitHub.

Dash, on the other hand, is a useful Python framework for creating web analytics apps. It is a pure Python data visualisation library built on top of Flask, Plotly.js, and React.js. It’s perfect for creating data visualisation apps with highly customised user interfaces. Certainly it is perfect for anyone who uses Python to work with info.

Setup for Plotly R and Python Dash comparision

Actually Dash was made by Plotly’s creators as a way to easily implement a web interface and create dashboards with Plotly without having to learn javascript, html and other web technologies.

In this tutorial I’ll be using a example to show you the comparison of Plotly R and Python Dash.

Installation

Plotly R for Data Visualization

Firstly install the plotly R package from CRAN using the install.package() method.

install.packages("plotly")

Secondly since this version might not be the most recent, we suggest downloading from Github using:

devtools::install_github("ropensci/plotly")

Users of RStudio should make sure they’re running the most recent version to ensure compatibility with the html widgets R kit.

By default, the plotly R package runs locally in your web browser or in the RStudio viewer.

Python Dash for Data Visualization

Likewise in your terminal, install dash.

pip install dash

This includes the plotly graphing library, as well as three component libraries that make up Dash’s core for instance: dash html components, dash core components, and dash table. Since these libraries are still in active growth, it’s a good idea to install and upgrade them on a regular basis.

Moreover you may also install jupyter or pandas as per your preference of development environment.

pip install jupyter-dash
pip install pandas

Comparison: Plotly R vs Python Dash

First example is a basic code for a grouped bar graph representation.

Bar Charts in Plotly R

Firstly, Creating simulated data

cities <- sample(c("NYC", "Boston", "LA", "Seattle"),
                 100,
                 replace = TRUE)
table(cities)
## cities
##  Boston      LA     NYC Seattle 
##      24      26      31      19
as.numeric(table(cities))
## [1] 24 26 31 19
names(table(cities))
## [1] "Boston"  "LA"      "NYC"     "Seattle"

Secondly, Simple bar chart

p1 <- plot_ly(x = names(table(cities)),
              y = as.numeric(table(cities)),
              name = "Cities",
              type = "bar")
p1
Image showing a simple bar graph can be made with Plotly R or Python Dash
Image showing a simple bar graph

Adding a title and axes names

p2 <- plot_ly(x = names(table(cities)),
              y = as.numeric(table(cities)),
              name = "Cities",
              type = "bar") %>% 
  layout(title = "Number of offices in each city",
         xaxis = list(title = "Cities",
                      zeroline = FALSE),
         yaxis = list(title = "Number",
                      zeroline = FALSE))
p2
Image of Bar graph with numeric data of Plotly R
Image of Bar graph with numeric data

Third, Creating simulated data for a data.frame

df <- data.frame(Cities = cities,
                 Group = sample(c("A", "B"),
                 100,
                 replace = TRUE))
head(df)
##    Cities Group
## 1     NYC     A
## 2      LA     B
## 3      LA     A
## 4      LA     A
## 5 Seattle     A
## 6      LA     A

Greate two new data.frame object for groups A and B.

groupA <- df %>% filter(Group == "A")
groupB <- df %>% filter(Group == "B")
table(groupA$Cities)
## 
##  Boston      LA     NYC Seattle 
##      11      14      14       9
names(table(groupA$Cities))
## [1] "Boston"  "LA"      "NYC"     "Seattle"
as.numeric(table(groupA$Cities))
## [1] 11 14 14  9
gBarChart <- data.frame(Cities = names(table(groupA$Cities)),
                      GroupA = as.numeric(table(groupA$Cities)),
                      GroupB = as.numeric(table(groupB$Cities)))
head(gBarChart)
##    Cities GroupA GroupB
## 1  Boston     11     13
## 2      LA     14     12
## 3     NYC     14     17
## 4 Seattle      9     10

Fourth, Grouped bar chart

p3 <- plot_ly(gBarChart,
              x = ~Cities,
              y = ~GroupA,
              type = "bar",
              name = "Group A") %>% 
  add_trace(y = ~GroupB,
            name = "Group B") %>% 
  layout(yaxis = list(title = "Cities"),
         barmode = "group")
p3
Image of Grouped bar graph in Plotly R
Image of Grouped bar graph in Plotly R

Fifth, Stacked bar chart

p4 <- plot_ly(gBarChart,
              x = ~Cities,
              y = ~GroupA,
              type = "bar",
              name = "group A") %>% 
  add_trace(y = ~GroupB,
            name = "Group B") %>% 
  layout(yaxis = list(title = "Cities"),
         barmode = "stack")
p4
Image of Stacked bar graph in Plotly R Data Visualization
Image of Stacked bar graph

Sixth,Changing the color

p5 <- plot_ly(x = names(table(cities)),
              y = as.numeric(table(cities)),
              name = "Cities",
              type = "bar",
              marker = list(color = "rgba(255, 70, 0, 0.7)",
                            line = list(color = "rgba(0, 0, 0, 0.5)",
                                        width = 1.5))) %>% 
  layout(title = "Number of offices per city",
         xaxis = list(title = "Cities",
                      zeroline = FALSE),
         yaxis = list(title = "Number",
                      zeroline = FALSE))
p5
Changing the color of a Bar graph in Plotly R Data Visualization
Changing the color of a Bar graph in Plotly R Data Visualization

Seventh, Changing the text angle on the xx axis

The tickangle = argument in the xaxis ragument of the layout command can change the angle of text.

p6 <- plot_ly(x = names(table(cities)),
              y = as.numeric(table(cities)),
              name = "Cities",
              type = "bar",
              marker = list(color = "rgba(255, 70, 0, 0.7)",
                            line = list(color = "rgba(0, 0, 0, 0.5)",
                                        width = 1.5))) %>% 
  layout(title = "Number of offices per city",
         xaxis = list(title = "Cities",
                      zeroline = FALSE,
                      tickangle = -20),
         yaxis = list(title = "Number",
                      zeroline = FALSE))
p6

Eighth, Specifying the color of each bar

Dash apps are divided into two parts. The “layout” of the app is the first section, and it explains how the app appears.

Further it provides Python classes for all of the visual components of the application. Moreover we maintain a set of components in the dash_core_components and the dash_html_components library but you can also build your own with JavaScript and React.js.

Note: Python code samples in this documentation are intended to be saved as files and run with python app.py. Jupyter is also compatible with the JupyterDash library.

p7 <- plot_ly(x = names(table(cities)),
              y = as.numeric(table(cities)),
              name = "Cities",
              type = "bar",
              marker = list(color = c("rgba(150, 150, 150, 0.7)",
                                      "rgba(150, 150, 150, 0.7",
                                      "rgba(255, 20, 0, 0.7)",
                                      "rgba(150, 150, 150, 0.7"))) %>% 
  layout(title = "Number of offices per city",
         xaxis = list(title = "Cities",
                      zeroline = FALSE),
         yaxis = list(title = "Number",
                      zeroline = FALSE))
p7

Media File(opens in a new tab)

Bar Charts in Python Dash for Data Visualization

# -*- coding: utf-8 -*-

# Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({
    "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
    "Amount": [4, 1, 2, 2, 4, 5],
    "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})

fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group")

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure=fig
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)
Data visualization using Python Dash by Plotly
Bar graph output of Python Dash

Feature Comparison Plotly R vs Python Dash

The Google App Engine standard environment is your buddy when it comes to deploying your Dash app. It’s designed to run for free or at a very low cost, with you only paying for what you need, when you need it. If there is no traffic, for example, the application will scale to 0 instances.

But the standard environment only supports a handful of languages-certainly -Python is one of them, R is not. To deploy a Plotly R, you’ll need to use the Flexible environment, which means you need to pay for all your app’s uptime rather than just when it has users. I’ve built apps for clients in Dash instead of R because they didn’t have a budget for deployment. The project manager can pay their GCP bill out of pocket because it usually ends up being less than $1/month. A flexible environment could have been closer to $20/month.

Plotly R, of course, has https://plotly.com/r/ . Their free tier is awesome for tinkerers, but less so for a client that doesn’t want RStudio branding on their app. Their non-r studio branded option was $9/month–again outside the clients budget. That being said–deployment to plotly R is the easiest remote deployment I’ve ever done.

Conclusion

Certainly identical production ready applications can be developed in both technologies . However what you chose depends on your specific needs and preferences.

However R Shiny is a bit faster for developers most importantly it requires less code than Dash for identical solutions . That’s only if you decide to not use Bootstrap in your dashboards. Further, creating custom components in Dash requires extensive React.js knowledge , which is a large hurdle. Likewise dash also falls short with intermediate reactive variables . Subsequently using them requires you to create a hidden div to store and use calculations.

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Comparison Between MongoDB and MySQL

Comparison Between MongoDB and MySQL

In this article, we are going to discuss comparison between MongoDB and MySQL. World is experiencing fourth industrial revolution. These revolution comprised of Artificial Intelligence (AI), Block chain, Cloud Computing and Internet of Thing (IoT) technologies. Data...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....