Percentage as Axis Tick Labels in plotly Graph in R (Example)

 

Hello folks! It is nice to be here again, with another interesting plotly tutorial in the R programming language. In this tutorial, you’ll learn how to set percentage as the Y axis tick labels in a plotly line plot.

As with other aspects of using the plotly library, it is very simple and easy to do. All it takes is just one extra line of code with an argument, as you will soon see.

Here is a sneak peek at what we shall be doing in this tutorial:

Let’s get right into it!

 

Install R plotly Library

To use the R plotly library, we need to first install it. We will also need to install the dplyr library, which will enable us to pipe lines of code together as a chunk. Therefore, in either the R Studio IDE or your preferred code editor that can run the R programming environment, please run the line of code below:

install.packages(c("plotly","dplyr"))

That will install both libraries and all their dependencies.
 

Load R plotly Library

Now that we have installed the plotly library, we will need to load it into our R environment so that we can use its plot-building functions. Please run the line of code below:

library(plotly)

We will also need to load the dplyr library, like we did plotly library(dplyr).
 

Create a Line Plot

Let us now create a simple line plot with a linear curve. Here, we are going to use a dummy data frame that contains data about the growth in birth rate over 10 years, starting from the year 2012 and ending in the year 2021. Please run the code below:

df <- data.frame(
  Year = c(2012,2013,2014,2015,2016,2017,2018,2019,2020,2021),
  Birth_rate = c(20,25,30,35,40,45,50,55,60,65)
)

You can print the data frame by running print(df)

#   Year Birth_rate
#1  2012         20
#2  2013         25
#3  2014         30
#4  2015         35
#5  2016         40
#6  2017         45
#7  2018         50
#8  2019         55
#9  2020         60
#10 2021         65

Let us now visualize this data as a line plot in plotly. Run the lines of code below in your IDE or editor:

fig <- df |> 
  plot_ly(x = ~Year,y = ~Birth_rate,type = "scatter",mode = "markers+lines")
 
print(fig)


From the above graph, we can tell that birth rate is increasing as the years are also increasing. However, we can make our graph easier for a user to understand by labeling the Y axis tick label as percentages so that it is immediately obvious that those values are in percentages.
 

Set Percentage As Y Axis Tick Label

Let us now set percentages as the Y axis tick label by running the code chunk below:

fig <- df |> 
  plot_ly(x = ~Year,y = ~Birth_rate,type = "scatter",mode = "markers+lines") |> 
  layout(yaxis = list(ticksuffix = "%"))
 
print(fig)


This is much better! Now, a user can readily see that the Y axis values are in percentages, and the graph now communicates information better than before.

Like said at the beginning of this tutorial, the process of setting percentages to the Y axis tick label in plotly is very simple. We only needed to add the layout() function to our code chunk and define the argument yaxis = list(ticksuffix = "%") inside the function to achieve our goal.

You can try this with other data frames. For example, you can build a bar plot that visualizes car types and their respective speed, and then set “Mph” as the Y axis tick labels.

So, working in plotly in the R programming language is actually very easy. With just a few lines of code, you can add customizations to your graph that will make it not only more visually appealing, but also more informative to the end user.

I do hope you have enjoyed this tutorial and have learned something new. Please watch out for more interesting R plotly tutorials on this blog, and I will see you soon in the next one. Take care!

 

Video, Further Resources & Summary

Do you need more explanations on how to set percentages as axis tick labels in a plotly graph 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 set percentage points as axis tick labels in a plotly graph in R.

 

The YouTube video will be added soon.

 

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

This post has shown how to set percentage points as the axis tick labels of a plotly graph in R. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

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

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top