adanlp
May 31, 2018
Using the mtcars dataset in R, I present a plot (plotly) that includes all the relevant aspects studied in previous lessons, such as the Miles Per Gallon (mpg), Weight (wt), displacement (disp), and transmission (am); as well as some factors considered by a potential buyer, such as number of cylinders (cyl), Horsepower (hp) and, of course, the brand and model of the car.
library(plotly)
data(mtcars)
mtcars$color = mtcars$cyl+mtcars$am
mtcars$color[which(mtcars$color == 4)] <- 'Automatic, 4 cyl'
mtcars$color[which(mtcars$color == 6)] <- 'Automatic, 6 cyl'
mtcars$color[which(mtcars$color == 8)] <- 'Automatic, 8 cyl'
mtcars$color[which(mtcars$color == 5)] <- 'Manual, 4 cyl'
mtcars$color[which(mtcars$color == 7)] <- 'Manual, 6 cyl'
mtcars$color[which(mtcars$color == 9)] <- 'Manual, 8 cyl'
mtcars$color <- as.factor(mtcars$color)
mtcars$car <- rownames(mtcars)
plot_ly(mtcars, x=~disp, y=~wt, z=~mpg,
mode='markers', color=~color, type='scatter3d',
size=~hp, sizes=c(10,75),
marker=list(opacity=0.6, sizemode='diameter'),
text=~paste(car,'<br>HP:', hp)) %>%
layout(title='Miles Per Gallon v. Weight & Displacement')