This plot explores the relationship between car mileage per gallon and weight, colored by the number of cylinders.
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble 3.0.1 ✓ dplyr 1.0.0
## ✓ tidyr 1.1.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ✓ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag() masks stats::lag()
plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(cyl),
type = 'scatter', mode = 'markers') %>%
layout(title = "Mileage vs. Weight by Cylinders",
xaxis = list(title = "Weight (1000 lbs)"),
yaxis = list(title = "Mileage per Gallon"))