Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove option bar from ggplotly using R?
Looking at this article, I notice it's about R programming, not Python. The topic and content are completely focused on R's ggplotly function. This appears to be misclassified. However, I'll improve the HTML structure and formatting while keeping the R content intact.
R is a programming language for statistical computing and graphics. ggplotly() is a function that converts a static ggplot to an interactive web-based version. ggplotly() returns a Plotly object that displays control options (ModeBar) by default. In this tutorial, we will see how to remove the option bar from ggplotly using R.
Key Concept
To remove the option bar from ggplotly, we use the config(displayModeBar = FALSE) parameter. The ModeBar contains interactive controls like zoom, pan, and download options that appear at the top-right corner of the chart.
Required Packages
Make sure you have the following packages installed in R
install.packages('ggplot2')
install.packages('plotly')
install.packages('readr')
Loading Libraries
Load the installed libraries
library(readr) library(ggplot2) library(plotly)
Step-by-Step Implementation
Step 1: Create Sample Data
For this example, we'll create sample data instead of requiring an external CSV file
# Create sample student data
students_data <- data.frame(
language = c(85, 92, 78, 88, 95, 82, 90, 87, 93, 79,
84, 91, 86, 89, 94, 83, 88, 92, 85, 90)
)
Step 2: Create the ggplot
Create a histogram using ggplot2
tplot <- ggplot(students_data) +
geom_histogram(mapping = aes(x = language),
color = 'blue',
fill = 'lightblue',
bins = 15)
Step 3: Remove the Option Bar
Convert to plotly and hide the ModeBar
ggplotly(tplot) %>% config(displayModeBar = FALSE)
Complete Example
Here's the complete code to remove the option bar from ggplotly
library(readr)
library(ggplot2)
library(plotly)
# Create sample data
students_data <- data.frame(
language = c(85, 92, 78, 88, 95, 82, 90, 87, 93, 79,
84, 91, 86, 89, 94, 83, 88, 92, 85, 90)
)
# Create ggplot
tplot <- ggplot(students_data) +
geom_histogram(mapping = aes(x = language),
color = 'blue',
fill = 'lightblue',
bins = 15)
# Convert to plotly without ModeBar
ggplotly(tplot) %>% config(displayModeBar = FALSE)
Comparison
To see the difference, here's how to show the chart with the ModeBar
ggplotly(tplot) %>% config(displayModeBar = TRUE)
| Setting | ModeBar Visible | Use Case |
|---|---|---|
displayModeBar = FALSE |
No | Clean presentation, embedded dashboards |
displayModeBar = TRUE |
Yes | Interactive exploration, full functionality |
Conclusion
Use config(displayModeBar = FALSE) with ggplotly to create clean, professional-looking interactive charts without control buttons. This is ideal for embedded dashboards or presentations where you want minimal visual distractions.
