R Markdown with Plotly

2025-02-03

Introduction

This is a presentation that features a Plotly plot.

library(ggplot2)
library(lattice)
library(caret)
suppressPackageStartupMessages(library(plotly))

Use Data Set mtcars

data <- mtcars
data$am <- as.factor(data$am)
data$vs <- as.factor(data$vs)

set.seed(123)
y <- data$mpg
x <-  data[, !(names(mtcars) %in% c("mpg"))]

control <- rfeControl(functions = rfFuncs,  
                      method = "cv",       
                      number = 10)       
results <- rfe(x, y,
               sizes =  c(1:10),           
               rfeControl = control)
if (length(results$optVariables) < 4) {
    selected_variables <- results$optVariables 
} else {
    selected_variables <- results$optVariables[1:4]  
}

print(selected_variables)
## [1] "disp" "wt"   "hp"   "cyl"

Plot_ly to Output

fig <- plot_ly(data, x = ~disp, y = ~wt, z = ~hp, color = ~cyl) %>%
        add_markers() %>%
        layout(scene = list(xaxis = list(title = 'Weight'),
                     yaxis = list(title = 'drat'),
                     zaxis = list(title = 'gear')) )
fig