In this article you will learn how to create a ggplot-like 3D scatter plot using the plotly R package.
Contents:
Related Book
GGPlot2 Essentials for Great Data Visualization in RPrerequisites
Load required R packages
library(tidyverse)
library(plotly)
Data preparation:
df <- mtcars %>%
rownames_to_column() %>%
as_data_frame() %>%
mutate(am = ifelse(am == 0, "Automatic", "Manual")) %>%
mutate(am = as.factor(am))
df
## # A tibble: 32 x 12
## rowname mpg cyl disp hp drat wt qsec vs am gear carb
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fc> <dbl> <dbl>
## 1 Mazda … 21 6 160 110 3.9 2.62 16.5 0 Man… 4 4
## 2 Mazda … 21 6 160 110 3.9 2.88 17.0 0 Man… 4 4
## 3 Datsun… 22.8 4 108 93 3.85 2.32 18.6 1 Man… 4 1
## 4 Hornet… 21.4 6 258 110 3.08 3.22 19.4 1 Aut… 3 1
## 5 Hornet… 18.7 8 360 175 3.15 3.44 17.0 0 Aut… 3 2
## 6 Valiant 18.1 6 225 105 2.76 3.46 20.2 1 Aut… 3 1
## # ... with 26 more rows
Basic 3D Scatter Plot
# Create the plot
p <- plot_ly(
df, x = ~wt, y = ~hp, z = ~qsec,
color = ~am, colors = c('#BF382A', '#0C4B8E')
) %>%
add_markers() %>%
layout(
scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time'))
)
p
3D Scatter Plot with Color Scaling
Point markers will be colored according to the mpg
variable:
# Point colors
marker <- list(color = ~mpg, colorscale = c('#FFE1A1', '#683531'),
showscale = TRUE)
# Create the plot
p <- plot_ly(df, x = ~wt, y = ~hp, z = ~qsec, marker = marker) %>%
add_markers() %>%
layout(
scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time'))
)
p
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Stanford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Amazon FBA
Amazing Selling Machine
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments