library(plotly)
library(dslabs)
library(ggplot2)
library(tidyverse)
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/challenges you had (if any) while making the plot, and you you dealt with or overcame them.
# Read data
NWSL <- read.csv("NWSL.csv", header=TRUE, sep=",", skip=1)
NWSL %>%
mutate(Pos = sapply(strsplit(Pos, ","), function(x){
paste(sort(x),collapse=",")
})) %>%
group_by(Pos) %>%
filter(sum(!is.na(TklW)) >=2) %>%
nest() %>%
mutate(
dens = map(
data, \(df) {
d.t <- density(df$TklW, bw=2, na.rm = TRUE)
tibble(TklW = d.t$x, Density = d.t$y)
})) %>%
select(Pos,dens) %>%
unnest(dens) %>%
plot_ly() %>%
add_lines(x = ~TklW, y = ~Density, hoverinfo="text", text = ~paste("Position:", Pos), color = ~Pos, fill = "tozeroy", opacity = 0.5) %>%
layout(title = "Total Tackles Won by Position",
xaxis = list(title = "Total Tackles Won"),
yaxis = list(title = "Density"))
The above graph is using data from players in the NWSL, National Womens Soccer League, for the 2026 season. The variables being graphed are TklW, Total Tackles Won, TklW’s density, and the players position. I created an overlayed density plot to show the density of tackles won faceted by a players position. The legend in the upper right shows which position correlates with which density curve. Additionally, when hovering your cursor over the density plots, you can easily see which position you are hovering above. This graph is useful in seeing if certain positions are more likely to win tackles over others. As shown in the graph, Forwards are less likely to win tackles compared to midfield and defense. A coach could potentially use this data to see if certain players need to work on tackles at practice to improve their duel rate.
There were some challenges when it came to coding this graph. For some of the positions where players have multiple, the way the positions were recorded varied. To ensure there weren’t duplicates and players were grouped evenly, I used sapply to reorganize these positions to be ordered the same way. Additionally, there were certain positions with very few players. Since these groups were giving an error, I omitted groups with fewer than 2 players. Furthermore, the density graphs were crowding together to the far left of the graph. I adjusted the bandwidth to increase the readability of the graph.
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.
Salaries <- read.csv("soccer_salaries.csv")
Age2 <- sort(unique(Salaries$Age))
Salaries %>%
mutate(Weekly.Wage = as.numeric(gsub("[£,]", "", Weekly.Wage)), Yearly.Salary = as.numeric(gsub("[£,]", "", Yearly.Salary))) %>%
plot_ly(x = ~Yearly.Salary, y = ~Weekly.Wage,
hoverinfo = "text",
text = ~paste("Nationality:", Nationality)) %>%
# add text
add_text(x = 10000000, y = 200000, text = Age2, frame = Age2, textfont = list(size = 100, color = toRGB("grey"))) %>%
add_markers(frame = ~Age,
ids = ~Nationality,
size = ~Yearly.Salary, color = ~Team,
marker = list(sizemode = "diameter",
sizeref=1)) %>%
layout(title = "EPL Salary by Age", xaxis = list(title="Yearly Salary"), yaxis = list(title="Weekly Salary")) %>%
animation_slider(currentvalue = list(font = list(color="white")))
The graph above is using data from players in the EPL, English Premier League. The variables being graphed are the players Age, their Weekly Salary, Yearly Salary, the club they play for, and their Nationality. The scatter plot above has Yearly Salary along the x-axis and Weekly Salary along the y-axis. The points are colored based on the players club and the size is based on the Yearly Salary size. When hovering your cursor over the points, you will be able to see the players Nationality. These points are animated over the players Age, the plot moving as the age increases. This shows how a players salary can differ based on their age and whether younger or older players can be expected to make more money. A player could potentially use this graph to see what they might expect to make when they reach a certain age based on what club they play for and their nationality. While playing the graph above, it is shown that a players salary increases as they get older until they hit 30. After that, the salaries start to decrease again.
Some challenges I faced was with the code for having the age being shown. The age kept coming in from the upper left corner and duplicating sometimes. I found that the issue stemmed from multiple players being the same age. To combat this, I made a separate age variable called Age2 that only listed the Ages once and in order from youngest to oldest. Additionally, the salaries are characters with their currency symbol in front. To change them to numeric values, I had to mutate the columns and remove the currency symbol using gsub.