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