4/17/2020

Introduction

We used the dataset “road” from the MASS package to test the Plotly package. The set contains data on road deaths in U.S. states.

The plot does not contain particularly interesting data but is rather a demonstration of Plotly and R Presentations.

Underlying data

The dataset road has six variables – not counting the row names, which are states – and twenty-six observations, each associated with a U.S. state.

library(MASS)
head(road)
        deaths drivers popden rural temp  fuel
Alabama    968     158   64.0  66.0   62 119.0
Alaska      43      11    0.4   5.9   30   6.2
Arizona    588      91   12.0  33.0   64  65.0
Arkanas    640      92   34.0  73.0   51  74.0
Calif     4743     952  100.0 118.0   65 105.0
Colo       566     109   17.0  73.0   42  78.0

Code for the Plot

This page shows the code used for the plot. Documentation is embedded in the code.

Load necessary libraries

library(plotly); library(MASS); library(tidyverse)

Prepare data for plotting

df <- mutate(road, state = rownames(road), deathrank = rank(-deaths))

Create ggplot

gg <- ggplot(df, aes(x = drivers, y = deaths, z = state, z2 = deathrank)) +
labs(x = "Number of drivers (10,000s)", y = "Number of deaths",
title = "Road Deaths and Drivers by State") +
geom_point()

Convert to Plotly

ggplotly(gg)

Slide with R Output

This page shows the plot, which is interactive.