# Change plotly Axis Range in R (Example)

Hi there! Welcome again to another interesting tutorial. This time, we shall be learning how to change the axis range of a plotly graph in the R programming language. As we shall soon see, it is very easy to do.

However, let’s see a summary of the steps we shall take to reach our goal:

Now that we’ve seen what we are going to do, let’s get right into it.

## Install R plotly Library

To use the plotly library, we must first of all install it. To do so, please run the following line of code:

`install.packages("plotly")`

Please note that you can use any code editor of your choice that can run R to follow along with this tutorial, although the most popular among R programmers is the R Studio IDE

We will also need to install the dplyr library, which allows us to use the pipe `|>` operator to link lines of code together. Therefore, please run `install.packages("dplyr")` as well.

Now that we have installed the R plotly and dplyr libraries and their dependencies, let us now load them into our R environment by running the code below:

```library(plotly)
library(dplyr)```

This loads all the functions that can be used to create different interactive visualizations with plotly, including the one we shall be using to create a line plot in this tutorial.

## Create a Simple Line plot

With the plotly library loaded, let us now create a line plot. I have created a dataset for this tutorial. Therefore, you can just copy and run the code below in order to have the exact same dataset in your environment as well:

```df <- data.frame(
X = 1:10,
Y = c(-0.1,-0.4,-0.2,-0.5,-0.7,0.3,0.8,0.5,0.9,0.6)
)```

Print the data frame:

`print(df)`
```#    X   Y
#1   1 -0.1
#2   2 -0.4
#3   3 -0.2
#4   4 -0.5
#5   5 -0.7
#6   6  0.3
#7   7  0.8
#8   8  0.5
#9   9  0.9
#10 10  0.6```

## Create Line Plot

Let’s now create a simple line plot from the dataset. In your editor, please run the code below:

```fig <- df |>
plot_ly(x = ~X,y = ~Y,type = "scatter",mode = "lines")

print(fig)```

## Adjust the Y Axis Range of the Line plot

Great! So we have created a basic line plot in plotly. Let us now change the range of the Y axis. As you can see, the Y axis currently ranges from -0.7 to 0.9, just as we have in our data frame. We shall now adjust it so that it will range from -0.8 to 1. To do so, run the code below:

```fig <- df |>
plot_ly(x = ~X,y = ~Y,type = "scatter",mode = "lines") |>
layout(yaxis = list(range = c(-0.8,1)))

print(fig)```

Just as you have seen, we only needed to pipe the `layout(yaxis = list(range = c(-0.8,1)))` line of code to our existing plotly line plot code in order to modify or change the Y axis range.

So, there you have it. You can now change the axis range of your line plot in plotly using the R programming language.

Note that you could use the xaxis argument instead of the yaxis argument within the layout function to modify the x-axis of a plotly graphic.

## Video, Further Resources & Summary

Would you like more explanations on how to create interactive visualizations using plotly in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, Kirby White explains how to create interactive visualizations with plotly in the R programming language.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to change the axis range of a line plot using plotly in the R programming language. In case you have further questions, you may leave a comment below.

I hope you found this tutorial helpful, and I will see you in the next one. Take Care!

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