Carpet Scatter Plot in R
How to create carpet plots in R with Plotly.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.
Basic Carpet Plot
library(plotly)
fig <- plot_ly(
type = 'carpet',
a = c(4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6),
b = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3),
y = c(2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10),
aaxis = list(
tickprefix = 'a = ',
ticksuffix = 'm',
smoothing = 1,
minorgridcount = 9
),
baxis = list(
tickprefix = 'b = ',
ticksuffix = 'Pa',
smoothing = 1,
minorgridcount = 9
)
)
fig
Add Carpet Scatter Trace
library(plotly)
fig <- plot_ly(
type = 'carpet',
a = c(4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6),
b = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3),
y = c(2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10),
aaxis = list(
tickprefix = 'a = ',
ticksuffix = 'm',
smoothing = 1,
minorgridcount = 9
),
baxis = list(
tickprefix = 'b = ',
ticksuffix = 'Pa',
smoothing = 1,
minorgridcount = 9
)
)
fig <- fig %>% add_trace(
type = 'scattercarpet',
a = c(4, 4.5, 5, 6),
b = c(2.5, 2.5, 2.5, 2.5),
line = list(
shape = 'spline',
smoothing = 1,
color = 'blue'
),
marker = list(color = "blue")
)
fig
Adding Multiple Traces
library(plotly)
fig <- plot_ly()
fig <- fig %>% add_trace(
type = 'carpet',
a = c(4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6),
b = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3),
y = c(2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10),
aaxis = list(
tickprefix = 'a = ',
ticksuffix = 'm',
smoothing = 1,
minorgridcount = 9
),
baxis = list(
tickprefix = 'b = ',
ticksuffix = 'Pa',
smoothing = 1,
minorgridcount = 9
)
)
fig <- fig %>% add_trace(
type = 'scattercarpet',
a = c(4, 4.5, 5, 6),
b = c(2.5, 2.5, 2.5, 2.5),
mode = 'markers+lines',
line = list(
shape = 'spline',
smoothing = 1,
color = "blue"
),
marker = list(color = "blue")
)
fig <- fig %>% add_trace(
type = 'scattercarpet',
a = c(4, 4.5, 5, 6),
b = c(1.5, 1.5, 1.5, 1.5),
mode = 'lines',
line = list(
shape = 'spline',
smoothing = 1,
color = "green"
)
)
fig <- fig %>% add_trace(
type = 'scattercarpet',
a = c(5, 5, 5, 5),
b = c(1, 1.5, 2, 3),
mode = 'markers',
marker = list(
color = "red",
size = c(0,0,20,0)
)
)
fig <- fig %>% add_trace(
type = 'scattercarpet',
a = c(4.5, 4.5, 4.5, 4.5),
b = c(1, 1.5, 2, 3),
mode = 'markers',
marker = list(
color = "black",
size = c(0,0,30,0)
)
)
fig
Reference
See https://plotly.com/r/reference/#scattercarpet for more information and options!
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)