Create a plotly graph of your choosing that represents at least two variables, one of which must be a categorical variable.
This plot can be a scatter plot, overlayed density plots (graphing variable is continuous, separate densities grouped by categorical variable), etc. choropleth maps could also be on the list…you have to admit they look kinda cool.
You can use any data of your choosing, just reference this.
The graph must include:
customized hover text that is informative to the graphing elements in the plot
separate color to represent groups
labeled axes and appropriate title
Include at least a 1-paragraph discussion about the graph. Discuss what is being plotted and what information is being displayed in the graph. Discuss any information that the reader may gain from hovering the cursor over graphing elements. Discuss any issues/chalenges you had (if any) while making the plot, and you you dealt with or overcame them.
library(plotly)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(nlme)
library(dslabs)
# Create a subset of the data for each diet type
barley <- subset(Milk, Diet == "barley")
barley_lupins <- subset(Milk, Diet == "barley+lupins")
lupins <- subset(Milk, Diet == "lupins")
# Calculate densities of protein for each diet type
d_barley <- density(barley$protein)
d_barley_lupins <- density(barley_lupins$protein)
d_lupins <- density(lupins$protein)
# Use empirical cumulative distribution function to estimate areas under the curve to add to the hover text
d_b_emp <- ecdf(barley$protein)
d_b_l_emp <- ecdf(barley_lupins$protein)
d_l_emp <- ecdf(lupins$protein)
# Create graph
# Shows density of protein by diet type
# Added hover text shows an estimate of the area under the curve
Milk%>%
plot_ly()%>%
add_lines(x = d_barley$x, y = d_barley$y, name = "Barley", hoverinfo = "text", text = ~paste("Protein: ", d_barley$x, "<br> Area Under Curve Estimate: ", d_b_emp(d_barley$x)))%>%
add_lines(x = d_barley_lupins$x, y = d_barley_lupins$y, name = "Barley and Lupins", hoverinfo = "text", text = ~paste("Protein: ", d_barley_lupins$x, "<br> Area Under Curve Estimate: ", d_b_l_emp(d_barley_lupins$x)))%>%
add_lines(x = d_lupins$x, y = d_lupins$y, name = "Lupins", hoverinfo = "text", text = ~paste("Protein: ", d_lupins$x, "<br> Area Under Curve Estimate: ", d_l_emp(d_lupins$x))) %>%
layout(title = "Density of Protein Content of Cows, by Diet",
xaxis = list(title = "Protein Content"),
yaxis = list(title = "Density"))
Create an animated plotly graph with a data set of your choosing. This can be, but does not have to be a scatter plot. Also, the animation does not have to take place over time. As mentioned in the notes, the frame can be set to a categorical variable. However, the categories the frames cycle through should be organized (if needs be) such that the progression through them shows some pattern.
This graph should include:
Aside from the graphing variable, a separate categorical variable. For example, in our animated scatter plot we color grouped the points by continent.
Appropriate axis labels and a title
Augment the frame label to make it more visible. This can include changing the font size and color to make it stand out more, and/or moving the frame label to a new location in the plotting region. Note, if you do this, make sure it is till clearly visible and does not obstruct the view of your plot.
Include at least a 1-paragraph discussion about the plot. Discuss what you are plotting and what trends can be seen throughout the animation. Discuss any issues, if any, you ran into in making the plot and how you overcame them.
library(modeldata)
taxi %>%
plot_ly(x = ~distance, color = ~local)%>%
add_histogram(frame = ~dow, showlegend = TRUE, opacity = 0.95)%>%
animation_opts(frame = 3000, transition = 1500)%>%
layout(xaxis = list(title = "Distance (Miles)",color = "black", tickfont = list(size = 18, color = "black"), titlefont = list(size = 16, color = "black")),
yaxis = list(title = "Frequency", color = "black", tickfont = list(size = 18, color = "black"), titlefont = list(size = 16, color = "back")), legend = list(title = list(text = "Was trip local?")),
title = list(text = "Taxi Trips in Chicago in 2022: Distance and Locality of Trip", font = list(color = "black", size = 17)))%>%
animation_slider(currentvalue = list(prefix = "Day of Week: ", font = list(color = "blue", size = 20, x = 1.5, y = 1.5)), font = list(color = "black", size = 18))
What to turn in:
knit your final assignment to an html document and publish it to an RPubs page.
submit (1) the rmd file and (2) the link to this page in Blackboard (this can be in a word document some other form to submit the link).