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)
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
ggplotlyggplotly(p1)
plot_lysetosa = data %>% filter(Species == "setosa")
versicolor = data %>% filter(Species == "versicolor")
virginica = data %>% filter(Species == "virginica")
p2 = plot_ly(type = 'scatter', mode = 'markers')
# 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
p2 = p2 %>%
add_trace(
x = versicolor$Petal.Length,
y = versicolor$Petal.Width,
marker = list(
color = "#5FB0B7",
size = 10),
name = 'Versicolor'
)
p2
p2 = p2 %>%
add_trace(
x = virginica$Petal.Length,
y = virginica$Petal.Width,
marker = list(
color = "#5BC8AF",
size = 10),
name = 'Virginica'
)
p2
# 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
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
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:
# 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
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