You may use any data of your choosing in the following problems, but I would suggest you choose a data set you find interesting or would give an interesting graph (so, don’t use something like the old iris data set). You will get more out of the project in the end, and it will look better to those in the future you are showing it to. If the data set comes from an R package then reference this. If the data set is from elsewhere, then upload a copy to blackboard (.csv format).
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.
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(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(plotly)
## Loading required package: ggplot2
##
## 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
library(Lahman)
salary_data <- Salaries %>%
filter(yearID == 2010)
d_al <- density(salary_data$salary[salary_data$lgID == "AL"], na.rm = TRUE)
d_nl <- density(salary_data$salary[salary_data$lgID == "NL"], na.rm = TRUE)
plot_ly() %>%
add_lines(x = ~d_al$x, y = ~d_al$y, name = "American League", fill = 'tozeroy', hoverinfo = 'text',
text = "American League") %>%
add_lines(x = ~d_nl$x, y = ~d_nl$y, name = "National League", fill = 'tozeroy', hoverinfo = 'text',
text = 'National League') %>%
layout(xaxis = list(title = 'Salary'),
yaxis = list(title = 'Density'),
title = "Salary Distribution by League in 2010")
# This overlayed density plot displays the density distribution for MLB salaries for American League and National League players in 2010. The data set contains 307 player salaries for Major League Baseball, and this visual separates the players into the League they played in in 2010. We can see that the two leagues typically follow the same distribution, with the vast majority of players earning close to the minimum as opposed to largely inflated salaries. The National League, shown in orange, had the higher density of players earning around the minimum, which was $400,000 in 2010. The American League (blue), however, had a slight edge in players earning around $6 million to around $12 million, as containing the largest salaries in the entire league. This visual supports intuition, as the American league contains teams like the New York Yankees and New York Mets who constantly spend enormous amounts of money on their players. The plot is also interactive, containing hover text if a user hovers their cursor over the plot. The user will see the respective league relating to which density they are examining. For example, hovering over the blue all the way to the right of the visual will display "American League", showing how the AL had the largest salaries. I had trouble trying to change the colors of the densitys before realizing the base colors worked. Originally, I had set the colors to red and blue to try and match their real life values, but it was not appealing to look at. By removing the colorfill = and color options, the base options without these lines was sufficient. The base orange and blue work perfectly for this visual and the user can easily discern between the two groups.
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 or trend.
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 still 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(Lahman)
baseball_data <- Teams
Team_Clean <- Teams %>%
filter(yearID >= 1998) %>% # 1998 was the year the MLB expanded to 30 teams
mutate(franchID = droplevels(factor(franchID)))
anim <- Team_Clean %>%
plot_ly(x = ~franchID, y = ~R, frame = ~yearID,
type = 'scatter',
mode = 'markers',
showlegend = FALSE,
size = 2,
color = ~DivWin, # color by six Division Winners (1)
hoverinfo = "text",
text = ~paste("Franchise:", franchID,
"<br> Runs:", R)) # hover text
anim %>%
add_text(x = "LAD", y = 400, text = ~yearID, frame = ~yearID,
textfont = list(size = 70, color = toRGB("grey70"))) %>% # frame label (3)
animation_slider(hide = TRUE) %>%
layout(xaxis = list(title = "Franchise"),
yaxis = list(title = "Runs Scored"),
title = "Franchise Runs Scored each year 1998 to 2024") # axis labels and title (2)
# This animated plotly scatter plot displays runs scored by each MLB franchise every year from 1998 to 2024. In 1998, the MLB expanded to the current and active 30 franchise format. The current year being displayed can be shown near the center of the background of the visual. Each Franchise is represented by a point for its respective offensive runs scored each year. The six points in blue represent division winners for that respective year, while the other 24 green points are the rest of the teams in the league. These division winners typically lie among the top offenses every year. We can see that runs throughout the league typically fall between 600 to 1000 from 1998 to about 2009. Starting in 2010, runs around the league start to decrease through 2016 before starting to increase again. Of course, we can see the extreme drop in runs scored in 2020 due to the shortened season during the Covid-19 pandemic. Lastly, in the final year of the animation, we can see one of the lowest points during the entire 27 year span, excluding the 2020 season. In 2024, the Chicago White Sox scored a measly 507 runs with one of the worst offenses in Major League Baseball recent history. While the final animation runs smoothly, I did run into a few problems along the way. Firstly, the original data set was way too large for this visual. The data contained historical data dating back to 1871 and contained franchises that have since been discontinued. So, I decided to only consider years starting at 1998. When I first created the visual, there were 40 franchises on the x axis and only four points were being shown every year. After some research, I found that franchID was a factor and contained the 30 distinct values I was after. I used the droplevels() in mutate() and it happened to fix both issues. After adding that line to clean the data, the plotly visual correctly displayed 30 franchises with a point for each.
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 or some other form to submit the link).