# 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 is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

### Basic Contour

```
library(plotly)
fig <- plot_ly(z = ~volcano, type = "contour")
fig
```

### Set X and Y Coordinates

```
library(plotly)
fig <- 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"
)
fig
```

### Set Size and Range of a Contours

```
library(plotly)
fig <- 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
)
)
fig
```

### Smoothing Contour Lines

```
library(plotly)
fig1 <- 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)
)
fig2 <- 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)
)
fig <- subplot(fig1,fig2)
fig
```

### Smoothing Contour Coloring

```
library(plotly)
fig <- 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'
)
)
fig
```

### Add Contour Labels

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

### 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))
fig <- plot_ly(mtrx.melt, x = ~wt, y = ~hp, z = ~qsec, type = "contour",
width = 600, height = 500)
fig
```

### 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
)
fig <- layout(s, showlegend = FALSE)
fig
```

### Contour Colorscales

See here for more examples concerning colorscales!

### Reference

See https://plotly.com/r/reference/#contour for more information and chart attribute 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)
```