Back to Programming irisPlot

Download Fisher’s Iris dataset

First, load the built-in iris dataset:

library(dplyr)
library(reshape)
library(magrittr)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Prepare Plot Data

Then, perform a few steps to prepare the data for plotting:

names(iris) <- c("Length.Sepal", "Width.Sepal", "Length.Petal", "Width.Petal",
                 "Species")
iris <- reshape(iris, direction = "long", varying = 1:4, sep = ".")
iris <- iris[,c("Species", "time", "Length", "Width")]
names(iris)[names(iris) == "time"] <- "Feature"
rownames(iris) <- NULL
head(iris)
##   Species Feature Length Width
## 1  setosa   Sepal    5.1   3.5
## 2  setosa   Sepal    4.9   3.0
## 3  setosa   Sepal    4.7   3.2
## 4  setosa   Sepal    4.6   3.1
## 5  setosa   Sepal    5.0   3.6
## 6  setosa   Sepal    5.4   3.9

Generate Plots using Plotly

Plot with Improperly Sorted Data

Create plot. Add a dropdown menu to select which feature (sepal or petal) will be shown:

library(plotly)
fig <- iris %>% plot_ly(
  type = 'scatter',
  mode = 'markers',
  x = ~Length, 
  y = ~Width,
  text = ~Species,
  color = ~Species,
  hovertemplate = ~paste('<b>%{text}</b><br>', Feature, 
  'Length: %{x}<br>', Feature, 'Width: %{y}<extra></extra>'),
  transforms = list(
    list(
      type = 'filter',
      target = ~Feature,
      operation = '=',
      value = unique(iris$Feature)[1]))
) %>% layout(
  title = "Iris Species Feature Dimensions",
  annotations = list(
    list(x = 0.0, y = 1.0, text = "Select Feature:", showarrow = F, xref='paper', yref='paper')),
  xaxis = list(
    showgrid = F
  ),
  yaxis = list(
    showgrid = F
  ),
  legend=list(title=list(text='Species'),
              itemclick="toggleothers"),
  updatemenus = list(
    list(
      type = 'dropdown', y = 0.95, x = 0.0,
      xanchor = "left", yanchor = "top",
      active = 0,
      buttons = list(
        list(method = "restyle",
             args = list("transforms[0].value", unique(iris$Feature)[1]),
             label = unique(iris$Feature)[1]),
        list(method = "restyle",
             args = list("transforms[0].value", unique(iris$Feature)[2]),
             label = unique(iris$Feature)[2]))
    )
  )
)
fig

Note that in the above plot, the features appear to be inaccurately represented.

Plot with Properly Sorted Data

We need to properly sort the data (series variable, dropdown variable, x and y variables) before running the plotly chunk above in order to generate an accurate plot:

attach(iris)
iris <- iris[order(Species, Feature, Length, Width),]
detach(iris)
rownames(iris) <- NULL

library(DT)
datatable(iris)

Find a great explanation of Fisher’s Iris dataset (with pictures) here.