Disable Hover Information in plotly Using R (Example)
Hi there! I am happy to welcome you to another exciting plotly tutorial using the R programming language. In this tutorial, you’ll learn how to disable hover information in plotly graphs using R.
However, before we get into it, let’s get a quick overview of what things we shall be doing in this tutorial:
Let’s now get right into it!
Download & Install plotly Library
To use the R plotly library, it must first be installed in either the R Studio IDE or any other preferred code editor that can run the R programming environment. In your IDE, run the line of code below to download and install plotly:
install.packages("plotly")
The code above will download and install plotly along with all of its dependencies in your R programming environment.
Load plotly Library
With plotly downloaded and installed, let us now load it into our coding environment by running:
library(plotly)
This will load plotly and all its plot-building functions, including the one we will soon use to build a simple bar plot.
We will also need to download and install the R dplyr library, which is a library used for data wrangling and piping in the R programming language. With the dplyr library, we can use the pipe operator to run several lines of code at once in a chunk. Therefore, run the lines of code below to install and load the dplyr library:
install.packages("dplyr") library(dplyr)
Build a Simple Bar Plot
We are going to build a sample data frame that we will visualize as a bar plot. It is a data frame of pets and their measured level of popularity. Please run the lines of code below to build the data frame and to view it:
df <- data.frame( Pets = c("Dogs","Cats","Hamsters","Iguanas","Snakes","Goats"), Popularity = c(90,75,60,55,85,70) ) print(df) # Pets Popularity #1 Dogs 90 #2 Cats 75 #3 Hamsters 60 #4 Iguanas 55 #5 Snakes 85 #6 Goats 70
Now, let us build a bar plot of the above data frame:
fig <- df |> plot_ly(x = ~Pets, y = ~Popularity, type = "bar", color = ~Pets) |> layout(title = "Pet Popularity") print(fig)
When you move your mouse over the plot, you will see the hover information of the plot, which plotly does by default. Nevertheless, this feature can be disabled, as we will see in the next section of this tutorial.
Disable Hover Info of Bar Plot
It is possible to disable the hover information of your plotly graph. To do so, we will just pipe a new line of code to our existing chunk, and define an argument inside that code function, as demonstrated below:
fig <- df |> plot_ly(x = ~Pets, y = ~Popularity, type = "bar", color = ~Pets) |> layout(title = "Pet Popularity") |> style(hoverinfo = "none") print(fig)
Now, when you hover over the plot, you do not see any hover information because it has been disabled. All we needed to do to disable this feature was to pipe the style()
function to our chunk and define hoverinfo = "none"
within that function. With that, the hover information of the plot becomes inactive. And this can be applied to all other visualization types created with plotly.
So, that is how to disable hover information in plotly using the R programming language. I hope you’ve learned something new in this tutorial. If yes, then be sure to check out other R plotly tutorials on Statistics Globe. Thank you for reading, and I will see you soon in the next one!
Video, Further Resources & Summary
Do you need more explanations on how to disable hover information in plotly using the R programming language? 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 disable hover information in plotly using R.
The YouTube video will be added soon.
Furthermore, you could have a look at some other tutorials on Statistics Globe:
- Order Bars in plotly Barchart in R (Example)
- How to Draw a plotly Boxplot in R (Example)
- Change plotly Axis Range in R (Example)
- Learn R
- How to Draw a plotly Boxplot in R (Example)
- Modify plotly Axis Labels in R (Example)
- Transparent plotly Graph Background Color in R (Example)
This post has shown how to disable hover information in plotly using R. 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.
Statistics Globe Newsletter