Open In App

How to do 3D line plots grouped by two factors with the Plotly package in R?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Plotly is a powerful library for making interactive charts. We will be able to plot 3D line plots using Plotly that can be utilized to analyze complex data and display results in a clear manner.

Syntax:

plot_ly(data, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "lines")

Parameters:

  • data: The data frame with the data to be plotted.
  • x, y, z: Column names for the x, y and z axes.
  • type: Set to "scatter3d" for a 3D plot.
  • mode: Use "lines" for a line plot.

Required Packages

You’ll need the Plotly package, which can be installed and load into R

R
install.packages("plotly")
library(plotly)

Example 1: Basic 3D Line Plot

We'll use the mtcars dataset to make a 3D plot with wt (weight) on the x-axis, mpg (miles per gallon) on the y-axis and qsec (quarter-mile time) on the z-axis:

R
p <- plot_ly(mtcars, x = ~wt, y = ~mpg, z = ~qsec, 
             type = "scatter3d", mode = "lines")
ggplotly(p)

Output:

3D_line_plot
3D line plots

Example 2: Grouping by the Number of Cylinders

We can group the data by the number of cylinders (cyl) by adding the color argument:

R
q <- plot_ly(mtcars, x = ~wt, y = ~mpg, 
             z = ~qsec, color = ~cyl, 
             type = "scatter3d", mode = "lines")
ggplotly(q)

Output:

3D_line_plot
3D line plots

Example 3: Grouping by Transmission Type

Here, we modified the color argument in the plot_ly function to group the data by the transmission type of each car model, represented by the am variable. The color argument is set to ~am, which uses the am variable to color the data points in the plot.

R
r <- plot_ly(mtcars, x = ~wt, y = ~mpg, 
             z = ~qsec, color = ~am, 
             type = "scatter3d", mode = "lines")
ggplotly(r)

Output:

3D_line_plot
3D lines plots

Example 4: Grouping by Both Cylinders and Transmission

You can group the data by both cyl and am, The color argument is set to ~cyl + am, which uses both the cyl and am variables to color the data points in the plot.

R
s <- plot_ly(mtcars, x = ~wt, y = ~mpg, 
             z = ~qsec, color = ~cyl + am, 
             type = "scatter3d", mode = "lines")
ggplotly(s)

Output:

3D_line_plot
3D lines plots

Example 5: Adding a Legend and Axis Labels

To improve the plot, you can add a legend and axis titles:

R
t <- plot_ly(mtcars, x = ~wt, y = ~mpg, 
             z = ~qsec, color = ~cyl + am, 
             type = "scatter3d", 
             mode = "lines") %>% layout(scene = 
                               list(xaxis = list(title = "Weight"),
                      yaxis = list(title = "Miles per Gallon"),
                      zaxis = list(title = "Quarter Mile Time"),
                      legend = list(x = 0.85, y = 0.95)))
ggplotly(t)

Output:

3D_line_plot
3D lines plots

The final plot provides us with a clear visual representation of the relationship between the weight, miles per gallon and a quarter-mile time of different car models, grouped by their number of cylinders and transmission type.


Similar Reads