Show Sidebar Hide Sidebar

# Contour Plots in R

How to make a contour plot in R. Two examples of contour plots of matrices and 2D distributions.

### New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
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.9.1'


### Basic Contour

library(plotly)

p <- plot_ly(z = ~volcano, type = "contour")

p


### Set X and Y Coordinates

library(plotly)

p <- plot_ly(
x = c(-9, -6, -5, -3, -1),
y = c(0, 1, 4, 5, 7),
z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125, 11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625, 1.25, 3.125,
6.25, 10.625, 0, 0.625, 2.5, 5.625, 10), nrow = 5, ncol = 5),
type = "contour"
)

p


### Set Size and Range of a Contours

library(plotly)

p <- plot_ly(
type = 'contour',
z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125,
11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625,
1.25, 3.125, 6.25, 10.625, 0, 0.625, 2.5, 5.625,
10), nrow=5, ncol=5),
colorscale = 'Jet',
autocontour = F,
contours = list(
start = 0,
end = 8,
size = 2
)
)

p


### Smoothing Contour Lines

library(plotly)

p1 <- plot_ly(
type = "contour",
z = matrix(c(2, 4, 7, 12, 13, 14, 15, 16, 3, 1, 6, 11, 12, 13,
16, 17, 4, 2, 7, 7, 11, 14, 17, 18, 5, 3, 8, 8, 13,
15, 18, 19, 7, 4, 10, 9, 16, 18, 20, 19, 9, 10, 5, 27,
23, 21, 21, 21, 11, 14, 17, 26, 25, 24, 23, 22),
nrow=7, ncol=8),
autocontour = TRUE,
contours = list(
end = 26,
size = 2,
start = 2
),
line = list(smoothing = 0)
)

p2 <- plot_ly(
type = "contour",
z = matrix(c(2, 4, 7, 12, 13, 14, 15, 16, 3, 1, 6, 11, 12, 13,
16, 17, 4, 2, 7, 7, 11, 14, 17, 18, 5, 3, 8, 8, 13,
15, 18, 19, 7, 4, 10, 9, 16, 18, 20, 19, 9, 10, 5, 27,
23, 21, 21, 21, 11, 14, 17, 26, 25, 24, 23, 22),
nrow=7, ncol=8),
autocontour = TRUE,
contours = list(
end = 26,
size = 2,
start = 2
),
line = list(smoothing = 0.85)
)

p <- subplot(p1,p2)

p


### Smoothing Contour Coloring

library(plotly)

p <- plot_ly(
type = 'contour',
z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125,
11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625,
1.25, 3.125, 6.25, 10.625, 0, 0.625, 2.5, 5.625,
10), nrow=5, ncol=5),
contours = list(
coloring = 'heatmap'
)
)

p


### Add Contour Labels

library(plotly)

p <- plot_ly(z = volcano, type = "contour", contours = list(showlabels = TRUE)) %>%
colorbar(title = "Elevation \n in meters")

p


### Create Matrix and Plot Contour

This example is based on (this)[https://www.r-statistics.com/2016/07/using-2d-contour-plots-within-ggplot2-to-visualize-relationships-between-three-variables/] r-statistics post.

library(plotly)
library(stringr)
library(reshape2)

data.loess <- loess(qsec ~ wt * hp, data = mtcars)

# Create a sequence of incrementally increasing (by 0.3 units) values for both wt and hp
xgrid <-  seq(min(mtcars$wt), max(mtcars$wt), 0.3)
ygrid <-  seq(min(mtcars$hp), max(mtcars$hp), 0.3)
# Generate a dataframe with every possible combination of wt and hp
data.fit <-  expand.grid(wt = xgrid, hp = ygrid)
# Feed the dataframe into the loess model and receive a matrix output with estimates of
# acceleration for each combination of wt and hp
mtrx3d <-  predict(data.loess, newdata = data.fit)
# Abbreviated display of final matrix
mtrx3d[1:4, 1:4]

##           hp
## wt         hp= 52.0 hp= 52.3 hp= 52.6 hp= 52.9
##   wt=1.513 19.04237 19.03263 19.02285 19.01302
##   wt=1.813 19.25566 19.24637 19.23703 19.22764
##   wt=2.113 19.55298 19.54418 19.53534 19.52645
##   wt=2.413 20.06436 20.05761 20.05077 20.04383

# Transform data to long form
mtrx.melt <- melt(mtrx3d, id.vars = c('wt', 'hp'), measure.vars = 'qsec')
names(mtrx.melt) <- c('wt', 'hp', 'qsec')
# Return data to numeric form
mtrx.melt$wt <- as.numeric(str_sub(mtrx.melt$wt, str_locate(mtrx.melt$wt, '=')[1,1] + 1)) mtrx.melt$hp <- as.numeric(str_sub(mtrx.melt$hp, str_locate(mtrx.melt$hp, '=')[1,1] + 1))

p <- plot_ly(mtrx.melt, x = ~wt, y = ~hp, z = ~qsec, type = "contour",
width = 600, height = 500)

p


### 2D Density Contour Plot

x <- rnorm(200)
y <- rnorm(200)
s <- subplot(
plot_ly(x = x, type = "histogram"),
plotly_empty(),
plot_ly(x = x, y = y, type = "histogram2dcontour"),
plot_ly(y = y, type = "histogram"),
nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), margin = 0,
shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE
)
p <- layout(s, showlegend = FALSE)

p


### Contour Colorscales

See here for more examples concerning colorscales!

### Reference

See https://plot.ly/r/reference/#contour for more information and chart attribute options!