# plotly Contour Plot in R (3 Examples)

Hi! This tutorial will show you how to build a plotly contour plot in the R programming language.

Here is an overview:

Let’s get into the R code!

To install and load the R plotly library, run the lines of code below in your preferred R code editor:

```# install plotly
install.packages("plotly")

library(plotly)```

With plotly installed and loaded into our R programming environment, we can now access its plot-building functions.

However, we will need data to visualize in a contour plot.

## Create Example Dataset

Since contour plots can be used to visualize both 2-dimensional and 3-dimensional data, we will make use of the volcano dataset, which comes pre-loaded inside of R Studio.

It has 61 columns and 87 rows. To preview the first 10 columns and rows of the dataset, run the code below:

```# sample data
volcano[1:10,1:10]

#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]  100  100  101  101  101  101  101  100  100   100
# [2,]  101  101  102  102  102  102  102  101  101   101
# [3,]  102  102  103  103  103  103  103  102  102   102
# [4,]  103  103  104  104  104  104  104  103  103   103
# [5,]  104  104  105  105  105  105  105  104  104   103
# [6,]  105  105  105  106  106  106  106  105  105   104
# [7,]  105  106  106  107  107  107  107  106  106   105
# [8,]  106  107  107  108  108  108  108  107  107   106
# [9,]  107  108  108  109  109  109  109  108  108   107
#[10,]  108  109  109  110  110  110  110  109  109   108```

Now that we have previewed the dataset, we can go on to build a contour plot.

## Example 1: Build Contour Plot

In this first example, we will build a basic contour plot:

```# build contour plot
fig <- plot_ly(z = ~volcano, type = "contour")

fig```

In the above example, we used the plot_ly() function to generate the contour plot, with the data for the plot specified as the volcano dataset, denoted by `~volcano`.

The argument `type =` is set to “contour” to specify that a contour plot should be created.

Finally, the `fig` object is returned, which is used to visualize the contour plot of the volcano dataset.

## Example 2: Smooth Contour Coloring

In this second example, we will change the color scale of the contour plot and smoothen out its contours:

```# smooth coloring
fig <- plot_ly(z = ~volcano,
type = "contour",
colorscale = "Jet",
contours = list(
coloring = "heatmap"
))

fig```

Here, we defined the `colorscale =` argument as “Jet” in the `plot_ly()` function, and then passed a list containing the contours’ coloring style to the `contours =` argument.

Now, you will notice that the contours’ coloring looks smoother and blended, just like in a heatmap.

## Example 3: Add Contour Labels

In this last example, we will add labels to the contour plot:

```# add counter labels
fig <- plot_ly(z = ~volcano,
type = "contour",
colorscale = "Jet",
contours = list(
coloring = "heatmap",
showlabels = TRUE
))

fig```

To add or display labels over the contours, we only needed to define the `showlabels =` argument as `TRUE` in the list passed to `contours =` argument.

That way, the data is displayed in the plot.

## Video, Further Resources & Summary

Do you need more explanations on how to build a plotly contour plot in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to build a plotly contour plot in R.

I hope this tutorial has helped you get started with making plotly contour plots in R. If you would like to learn more about using plotly in the R programming language, then be sure to check out other interesting plotly in R tutorials on Statistics Globe:

This post has shown how to build a plotly contour plot in R. I hope you enjoyed reading it! In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter