11/4/2021

Introduction

This slideshow is the week 3 assignment for the Coursera course, Developing Data Products.

The assignment is to create a slideshow with an interactive plotly chart.

Huge thanks to https://plotly.com/r/ for all the plotting options!

About the Data

We’re going to use the mtcars data set. Let’s look at the first 6 cars in the data set.

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Building our Plotly Chart

Here is how we’re going to build a scatterplot of fuel efficiency based on vehicle weight (in 1,000s of pounds). The color of each marker indicates the number of cylinders and the size indicates the horsepower. Hovering over each dot will give you the weight, mpg, cylinders, and vehicle name.

p <- plot_ly(data=mtcars, x=~wt, y=~mpg, type="scatter",
             mode="markers", color=~factor(cyl),
             marker=list(size=~hp/10),
             text=row.names(mtcars)) %>%
  layout(legend=list(font=list(family = "sans-serif", size = 12,
                               color = "#000"), 
                     title=list(text='<b> Cylinders </b>'),
                     bgcolor = "#E2E2E2", bordercolor = "#FFFFFF",
                     borderwidth = 2),
         xaxis = list(title = 'Vehicle Weight (1,000s of lbs)'),
         yaxis = list(title = 'Miles per Gallon'),
         title='Fuel Efficiency by Vehicle Weight and Cylinders')

Our Plotly Chart