Show Sidebar Hide Sidebar

# Carpet Plot in R

How to create carpet plots in R with Plotly.

### New to Plotly?

Plotly's R library is free and open source!
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

``````library(plotly)
packageVersion('plotly')
``````
``````## [1] '4.7.1'
``````

### Set the Coordinates

To set the `x` and `y` coordinates use `x` and `y` attributes. If `x` coorindate values are ommitted a cheater plot will be created.

``````library(plotly)

p <- plot_ly(
type = 'carpet',
y = c(2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10))
``````

To save parameter values use `a` and `b` attributes.

``````library(plotly)

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

# Set up API credentials: https://plot.ly/r/getting-started
``````

Use `aaxis` or `baxis` lists to make changes to the axes. For a more detailed list of attributes refer to R reference.

``````library(plotly)

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

# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="carpet-axes", sharing = 'public')
``````

### Style Carpet Axes

``````library(plotly)

p <- 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,
minorgridwidth = 0.6,
minorgridcolor = 'white',
gridcolor = 'white',
color = 'white'
),
baxis = list(
tickprefix = 'b = ',
ticksuffix = 'Pa',
smoothing = 1,
minorgridcount = 9,
minorgridwidth = 0.6,
gridcolor = 'white',
minorgridcolor = 'white',
color = 'white'
)
) %>%
layout(
plot_bgcolor = 'black', paper_bgcolor = 'black',
xaxis = list(showgrid = F, showticklabels = F),
yaxis = list(showgrid = F, showticklabels = F)
)

# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="carpet-styled", sharing = 'public')
``````