On my honor as a student, I have neither given nor accepted unauthorized aid on this assignment or exam. Nhi Nguyen

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(plotly)
## 
## 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
data = as.data.frame(iris)
head(data)

create a scatterplot of petal length vs petal width colored by species using ggplot2. Set you colors manually.

p1 = ggplot(data, aes(x=Petal.Length, y=Petal.Width, color=Species))+
  geom_point()+
  scale_color_manual(values=c("#6C91BF","#5FB0B7","#5BC8AF"))+
  theme_minimal()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())+
  labs(x="Petal Length", y="Petal Width")

p1

Convert your plot above to an interactive graph using ggplotly

ggplotly(p1)

Now code the the above plot directly in plot_ly

  1. Manipulate data as needed
setosa = data %>% filter(Species == "setosa")
versicolor = data %>% filter(Species == "versicolor")
virginica = data %>% filter(Species == "virginica")
  1. initialize your plotly plot
p2 = plot_ly(type = 'scatter', mode = 'markers')
  1. add your Setosa trace
# values=c("#6C91BF","#5FB0B7","#5BC8AF")

p2 = p2 %>%
  add_trace(
    x = setosa$Petal.Length,
    y = setosa$Petal.Width,
    marker = list(
      color = "#6C91BF",
      size = 10),
    name = 'Setosa'
      )

p2
  1. Add your versicolor trace
p2 = p2 %>%
  add_trace(
    x = versicolor$Petal.Length,
    y = versicolor$Petal.Width,
    marker = list(
      color = "#5FB0B7",
      size = 10),
    name = 'Versicolor'
      )

p2
  1. Add your virginica trace
p2 = p2 %>%
  add_trace(
    x = virginica$Petal.Length,
    y = virginica$Petal.Width,
    marker = list(
      color = "#5BC8AF",
      size = 10),
    name = 'Virginica'
      )

p2
  1. Update the tooltip to read petal width: x.x petal length: x.x
# had to initialize again bc my graph key was showing double for each of the flower specie
p2 = plot_ly(type = 'scatter', mode = 'markers') 

p2 = p2 %>%
  add_trace(
    x = setosa$Petal.Length,
    y = setosa$Petal.Width,
    marker = list(
      color = "#6C91BF",
      size = 10),
    name = 'Setosa',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
      )

p2 = p2 %>%
  add_trace(
    x = versicolor$Petal.Length,
    y = versicolor$Petal.Width,
    marker = list(
      color = "#5FB0B7",
      size = 10),
    name = 'Versicolor',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
      )

p2 = p2 %>%
  add_trace(
    x = virginica$Petal.Length,
    y = virginica$Petal.Width,
    marker = list(
      color = "#5BC8AF",
      size = 10),
    name = 'Virginica',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
      )

p2
  1. Add graph title and remove the gridlines
p2 = p2 %>%
  layout(title = 'Scatterplot of Petal Length & Petal Width',
         xaxis = list(title = 'Petal Length',
                      showgrid = FALSE),
         yaxis = list(title = 'Petal Width',
                      showgrid = FALSE, zeroline = FALSE))
p2
  1. Lighten the marker, create a dark outline of the same color
p2 = plot_ly(type = 'scatter', mode = 'markers') 

p2 = p2 %>%
  add_trace(
    x = setosa$Petal.Length,
    y = setosa$Petal.Width,
    opacity = 0.5, 
    marker = list(
      color = "#6C91BF",
      size = 10,
      line = list(
        color = "#6C91BF",
        width = 2)),  # outline hard to see bc they're the same color, but I think 2 is pretty dark and thick
    name = 'Setosa',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
      )

p2 = p2 %>%
  add_trace(
    x = versicolor$Petal.Length,
    y = versicolor$Petal.Width,
    opacity = 0.5, 
    marker = list(
      color = "#5FB0B7",
      size = 10,
      line = list(
        color = "#5FB0B7",
        width = 2)),
    name = 'Versicolor',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
      )

p2 = p2 %>%
  add_trace(
    x = virginica$Petal.Length,
    y = virginica$Petal.Width,
    opacity = 0.5, 
    marker = list(
      color = "#5BC8AF",
      size = 10,
      line = list(
        color = "#5BC8AF",
        width = 2)),
    name = 'Virginica',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
      )

p2 = p2 %>%
  layout(title = 'Scatterplot of Petal Length & Petal Width',
         xaxis = list(title = 'Petal Length',
                      showgrid = FALSE),
         yaxis = list(title = 'Petal Width',
                      showgrid = FALSE, zeroline = FALSE))

p2

Homework:

  1. Take the above graph and make it 3D by adding Sepal Length on the z-axis.
# referenced class notes 'plotly-1.Rmd'
p3 = plot_ly(type = 'scatter3d', mode = 'markers') 

p3 = p3 %>%
  add_trace(
    x = setosa$Petal.Length,
    y = setosa$Petal.Width,
    z = setosa$Sepal.Length,
    opacity = 0.5, 
    marker = list(
      color = "#6C91BF",
      size = 10,
      line = list(
        color = "#6C91BF",
        width = 2)),  # outline hard to see bc they're the same color, but I think 2 is pretty dark and thick
    name = 'Setosa',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x} <br>Sepal Length: %{z}'
      )

p3 = p3 %>%
  add_trace(
    x = versicolor$Petal.Length,
    y = versicolor$Petal.Width,
    z = versicolor$Sepal.Length,
    opacity = 0.5, 
    marker = list(
      color = "#5FB0B7",
      size = 10,
      line = list(
        color = "#5FB0B7",
        width = 2)),
    name = 'Versicolor',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x} <br>Sepal Length: %{z}'
      )

p3 = p3 %>%
  add_trace(
    x = virginica$Petal.Length,
    y = virginica$Petal.Width,
    z = virginica$Sepal.Length,
    opacity = 0.5, 
    marker = list(
      color = "#5BC8AF",
      size = 10,
      line = list(
        color = "#5BC8AF",
        width = 2)),
    name = 'Virginica',
    hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x} <br>Sepal Length: %{z}'
      )

x.axis = list(
  title = "Petal Length"
)

y.axis = list(
  title = "Petal Width"
)

z.axis = list(
  title = "Sepal Length"
)

p3 = p3 %>%
  layout(title = 'Scatterplot of Petal Length, Petal Width & Sepal Length',
         scene = list(xaxis = x.axis,
                      yaxis = y.axis,
                      zaxis = z.axis))

p3
  1. Create grouped boxplots where each iris dataset measure in on the x-axis with 3 boxplots for each measure representing the species. Use your own cohesive color palette using coolors.co. Don’t forget to include all the parts of a chart.

Reshaping the data to make column names into column values

# Renaming to take away the dot in the column names
names = c("Sepal Length","Sepal Width","Petal Length","Petal Width","Species")

setosa1 = setosa
colnames(setosa1) = names

versicolor1 = versicolor
colnames(versicolor1) = names

virginica1 = virginica
colnames(virginica1) = names

# Source: https://campus.datacamp.com/courses/abc-intro-2-r/data-wrangling?ex=2
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
# id.vars is kind of the identifying column; everything else but the id.vars gets stretched out
setosa.new = melt(setosa1, id.vars = c("Species")) 
versicolor.new = melt(versicolor1, id.vars = c("Species"))
virginica.new = melt(virginica1, id.vars = c("Species"))
p4 = plot_ly(type = 'box')

p4 = p4 %>%
  add_trace(
    x = setosa.new$variable,
    y = setosa.new$value,
    marker = list(
      color = "#E3D7FF"
    ),
    name = "Setosa"
  ) %>%
  add_trace(
    x = versicolor.new$variable,
    y = versicolor.new$value,
    marker = list(
      color = "#AFA2FF"
    ),
    name = "Versicolor"
  ) %>%
  add_trace(
    x = virginica.new$variable,
    y = virginica.new$value,
    marker = list(
      color = "#7A89C2"
    ),
    name = "Virginica"
  ) %>%
  layout(title = 'Boxplot of All Measures Grouped by Species',
         xaxis = list(title = 'Measures'),
         yaxis = list(title = 'Value',
                      showgrid = FALSE, zeroline = FALSE)) 



p4
## Warning: Can't display both discrete & non-discrete data on same axis