- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to show multiple ggplot2 plots with Plotly using R?
R is a programming language for statistical computing and graphics. ggplotly() is a function used to convert static plots to web-based plots. ggplotly() returns a Plotly object. In this tutorial, we will see how to show multiple ggplot2 plots with Plotly using R.
Here, we will use the aes() function that is used for aesthetic mapping between visual cue and a variable. It contains the following arguments: position (X and Y axes), color, fill, shape, line type, and size.
To display multiple ggplot2 plots, we will use the facet_grid() function.
Follow the steps given below to show multiple ggplot2 plots with R language.
Step 1
Make sure you have the following libraries installed
install.packages('ggplot2') install.packages('plotly') install.packages('readr') install.packages("ggpubr")
Step 2
After installing, import these libraries −
library(readr) library(ggplot2) library(plotly) library("ggpubr")
Step 3
Create a dataset from an external CSV file. Here, we have imported the "students_result.csv" file. You need to keep the CSV file in the project directory or provide its full path in the following command −
students_result <- read_csv("students_result.csv")
Step 4
Create a ggplot using the following arguments −
p1 = ggplot(students_data) + geom_histogram(mapping = aes(x=language), color='blue', fill='lightblue', bins=15)
Step 5
Use the facet_grid() method to create a figure with multiple ggplot2 plots
figure = p1 + facet_grid(rows = vars(state))
Step 6
Create a ggplot from the figure.
ggplotly(figure)
Example
Here is the complete code to show multiple ggplot2 plots with Plotly using R −
install.packages('ggplot2') install.packages('plotly') install.packages('readr') install.packages("ggpubr") library(readr) library(ggplot2) library(plotly) library("ggpubr") students_result <- read_csv("students_result.csv") p1 = ggplot(students_data) + geom_histogram(mapping = aes(x=language), color='blue', fill='lightblue', bins=15) figure = p1 + facet_grid(rows = vars(state)) ggplotly(figure)
Output
It will produce the following output on the browser −
- Related Articles
- How to create multiple bar plots for varying categories with same width bars using ggplot2 in R?
- How to create boxplot with multiple factor levels using ggplot2 in R?
- How to save multiple plots into a single HTML file in Python Plotly?
- How to plot multiple time series using ggplot2 in R?
- How to convert ggplot2 graph into a plotly graph in R?
- How to show legend and label axes in 3D scatter plots in Python Plotly?
- How to create two plots using ggplot2 arranged in a vertical manner in R?
- How to change legend for multiple histograms using ggplot2 in R?
- How to create multiple regression lines using ggplot2 in R?\n
- How to draw a multiple line chart using Plotly Express in Python Plotly?
- How to create the plots arranged in a list that were generated using ggplot2 in R?
- How to create multiple series scatter plots with connected points using seaborn?
- How to create multiple plots of different sizes in base R?
- How to create pie chart using plotly in R?
- How to create a bar chart using plotly in R?
