Plotting Using the Plotly Package in R
- Get link
- X
- Other Apps
Plotting Using the Plotly Package in R
Introduction
Plotly is an interactive visualization library for R that allows the creation of high-quality, web-based graphics. Unlike base R graphics, Plotly graphs are interactive and support:
- Zooming
- Panning
- Hover information
- Legends
- Saving graphs as images
- Dynamic exploration
Plotly can create:
- Scatter plots
- Line graphs
- Bar charts
- Histograms
- Pie charts
- Box plots
- Bubble charts
- Heat maps
- 3D scatter plots
- Surface plots
Installing Plotly
install.packages("plotly")
Load the package:
library(plotly)
The plot_ly() Function
The primary function in Plotly is:
plot_ly()
Syntax
plot_ly(data,
x,
y,
type,
mode)
where
- data : data frame
- x : x-axis variable
- y : y-axis variable
- type : graph type
- mode : line, marker, etc.
Example 1: Scatter Plot
library(plotly)
x <- c(1,2,3,4,5)
y <- c(10,15,13,17,20)
plot_ly(x=x,
y=y,
type='scatter',
mode='markers')
Produces an interactive scatter plot.
Example 2: Line Graph
Example 3: Line and Markers
Example 4: Customize Marker Color and Size
plot_ly(x=x,
y=y,
type='scatter',
mode='markers',
marker=list(
color='red',
size=12
))
Example 5: Add Title and Axis Labels
Example 6: Bar Chart
Example 7: Horizontal Bar Chart
Example 8: Pie Chart
Example 9: Doughnut Chart
Example 10: Histogram
Example 11: Box Plot
Example 12: Multiple Box Plots
Example 13: Bubble Chart
plot_ly(
x=c(1,2,3,4),
y=c(10,20,15,25),
mode='markers',
marker=list(
size=c(20,40,30,50)
))
Bubble size represents an additional variable.
Example 14: Scatter Plot Using Data Frame
student <- data.frame(
Hours=c(2,4,6,8,10),
Marks=c(45,60,70,85,95)
)
plot_ly(student,
x=~Hours,
y=~Marks,
type='scatter',
mode='markers')
The symbol ~ indicates columns from a data frame.
Example 15: Add Multiple Traces
3D Scatter Plot
x <- c(1,2,3,4)
y <- c(5,6,7,8)
z <- c(2,4,6,8)
plot_ly(
x=x,
y=y,
z=z,
type='scatter3d',
mode='markers'
)
Interactive rotation is supported.
Surface Plot
Heat Map
Contour Plot
Adding Colors
plot_ly(
x=x,
y=y,
type='scatter',
mode='markers',
marker=list(color='blue')
)
Marker Properties
marker=list(
color='red',
size=15,
symbol='circle'
)
Common symbols:
- circle
- square
- diamond
- cross
- triangle-up
Layout Function
layout(
title="Graph Title",
xaxis=list(title="X Axis"),
yaxis=list(title="Y Axis")
)
Example layout and markerlibrary(plotly) # Data x <- c(1, 2, 3, 4, 5) y <- c(10, 15, 13, 17, 20) # Create plot fig <- plot_ly( x = x, y = y, type = "scatter", mode = "markers", marker = list( color = "red", size = 15, symbol = "diamond" ) ) # Customize layout fig <- fig %>% layout( title = "Sales Analysis", xaxis = list( title = "Year", showgrid = TRUE ), yaxis = list( title = "Sales", showgrid = TRUE ) ) fig
Multiple Subplots
Common Plot Types
| Type | Description |
|---|---|
| scatter | Scatter plot |
| bar | Bar chart |
| pie | Pie chart |
| histogram | Histogram |
| box | Box plot |
| heatmap | Heat map |
| contour | Contour plot |
| scatter3d | 3D scatter plot |
| surface | Surface plot |
Important Functions
| Function | Purpose |
|---|---|
plot_ly() | Create graph |
add_trace() | Add another dataset |
layout() | Add titles and labels |
subplot() | Multiple graphs |
animation_opts() | Animation settings |
saveWidget() | Save graph as HTML |
Applications
- Exploratory Data Analysis (EDA)
- Machine Learning visualization
- Dashboard creation
- Scientific visualization
- Statistical analysis
- Financial charts
- Interactive reports
Difference Between Base R Graphics and Plotly
| Base R | Plotly |
|---|---|
| Static | Interactive |
| Simple graphics | Dynamic graphics |
| No zooming | Zooming available |
| Limited interactivity | Hover information |
| Faster | More feature-rich |
plot() | plot_ly() |
Advantages of Plotly
- Interactive graphs.
- Publication-quality visualizations.
- Supports 2D and 3D plots.
- Works with data frames.
- Easily integrated with Shiny dashboards.
- Provides zooming and hover information.
- Can export graphs as HTML.
Conclusion
The Plotly package is one of the most powerful visualization libraries available in R. Using plot_ly(), add_trace(), and layout(), users can create highly interactive 2D and 3D graphs. Plotly is widely used in data science, machine learning, statistical analysis, and dashboard development because of its flexibility and rich interactive features.
- Get link
- X
- Other Apps


















Comments
Post a Comment