fig <- plot_ly(
type = 'table',
columnwidth = c(100, 100),
columnorder = c(0, 1),
header = list(
values = c("Name","Grade"),
align = c("center", "center"),
line = list(width = 1, color = 'black'),
fill = list(color = c("lightblue", "lightblue")),
font = list(family = "Arial", size = 14, color = "white")
),
cells = list(
values = rbind(
c("Steve", "Carol", "Bob", "Tina", "Clay"),
c("97", "34", "73", "52", "88")
),
align = c("center", "center"),
line = list(color = "black", width = 1),
font = list(family = "Arial", size = 12, color = c("black"))
))
figUnderstanding Plotly
MATH/COSC 3570 Spring 2023
R
Tables in R
Pie Charts in R
labels = c('Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen')
values = c(4500, 2500, 1053, 500)
fig <- plot_ly(type='pie', labels=labels, values=values,
textinfo='label+percent',
insidetextorientation='radial')
fig##ScatterPlots in R
fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
figNo trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
Bubblegraphs in R
##Bubble
plot_ly() |>
add_markers(data = mtcars,
x = ~mpg,
y = ~hp,
hoverinfo = 'text',
text = ~paste('Car Model:', rownames(mtcars)),
marker = list(size = ~2*cyl, opacity = 0.5, color = 'purple')) |>
layout(title = 'Horsepower Vs. MPG',
xaxis = list(title = 'Miles Per Gallon'),
yaxis = list(title = 'Horsepower'))Sunburst Graphs in R
##Sunburst
library(plotly)
labels <- c("Andrew", "Alex", "Sophia", "Seth", "Max", "Lisa", "Mark", "Louise",
"Kevin", "Martha", "Jane", "Abby")
parents <- c("", "Andrew", "Andrew", 'Andrew', "Andrew", "Andrew", "Alex", "Alex",
"Sophia", "Seth", "Max", "Seth")
values <- c(20,34,25,20,8, 13, 17, 7, 25, 10, 20, 8)
fig <- plot_ly(
labels = labels,
parents = parents,
values = values,
type = "sunburst"
)
fig3D Surface Plot in R
library(plotly)
# volcano is a numeric matrix that ships with R
fig <- plot_ly(z = ~volcano)
fig <- fig %>% add_surface()
figMap Graph in R
##Map
g <- list(showland = TRUE,
showcountries = TRUE,
countrycolor = "orange",
landcolor = "#e5ecf6",
projection = list(type = "natural earth"))
fig <- plot_ly(type = 'scattergeo', mode = 'markers')
fig <- fig |> layout(geo = g)
figPython
Tables in Python
import pandas as pd
import plotly.graph_objects as go
data = pd.read_csv('./data/mtcars.csv')
data = data.head(7)
data = data.filter(items = ['mpg', 'cyl', 'disp', 'hp'])
fig1 = go.Figure(data=[go.Table(
header = dict(values = list(data.columns),
fill_color = 'paleturquoise',
align = 'left'),
cells = dict(values=[data.mpg, data.cyl, data.disp, data.hp],
fill_color = 'lavender',
align = 'left'))
])
fig1Pie Charts in Python
import plotly.graph_objects as go
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500, 2500, 1053, 500]
fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
fig.show()##ScatterPlots in Python
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()Bubblegraphs in Python
import plotly.graph_objects as go
import pandas as pd
cars = pd.read_csv('./data/mtcars.csv')
size = 0.5*cars['cyl']
bubble2 = go.Figure(data=[go.Scatter(
x=cars['mpg'],
y=cars['hp'],
mode='markers',
marker=dict(
size=size,
sizemode='area',
sizeref=2.*max(size)/(40.**2),
sizemin=4,
opacity = 0.5
)
)])
bubble2Sunburst Graphs in Python
import plotly.express as px
data = dict(
character=["Andrew", "Alex", "Sophia", "Seth", "Max", "Lisa", "Mark", "Louise",
"Kevin", "Martha", "Jane", "Abby"],
parent=["", "Andrew", "Andrew", 'Andrew', "Andrew", "Andrew", "Alex", "Alex",
"Sophia", "Seth", "Max", "Seth"],
value=[20,34,25,20,8, 13, 17, 7, 25, 10, 20, 8])
fig = px.sunburst(
data,
names='character',
parents='parent',
values='value',
)
fig3d Surface Plot in Python
import plotly.graph_objects as go
import pandas as pd
# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')
fig = go.Figure(data=[go.Surface(z=z_data.values)])
fig.update_layout(title='Mt Bruno Elevation', autosize=False,
width=500, height=500,
margin=dict(l=65, r=50, b=65, t=90))Map Graph in Python
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
world = go.Figure(go.Scattergeo())
world.update_geos(
projection_type = "orthographic",
visible = True, resolution = 110, scope = "world",
showcountries = True, countrycolor = "Black",
coastlinecolor = "Black", showcoastlines = True
)world.update_layout(height = 300, margin = {"r":0, "t":0, "l":0,"b":0})