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).
# load in packages
library(readxl)
library(readr)
library(tidyverse)
library(plotly)
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
# load in data
nba <- read.csv("nba_players_shooting.csv")
nba <- as.data.frame(nba)
# filter the data to just russell westbrook
nba <- nba %>%
filter(SHOOTER == "Russell Westbrook")
# view structure
# str(nba)
nba %>%
plot_ly(x = ~X, y = ~Y, color = ~SCORE,
hoverinfo = "text", # custom hover info
text = ~paste("Defender:", DEFENDER, "<br>",
"Range:", RANGE, "<br>",
"x coordinate:", round(X,2), "<br>",
"y coordinate:", round(Y,2))) %>%
add_markers(size = 2,
colors = c("darkgreen", "tomato")) %>%
layout(xaxis = list(title = "x position"),
yaxis = list(title = "y position"),
title = "Location of Shots Taken by Russell Westbrook")
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.
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.
# load in covid data from (https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv)
df <- read.csv("covid_states_data.csv")
# load in regions data
regions <- read.csv("states.csv")
# change date class
df$date <- as.Date(df$date)
# filter to just the year 2020 May - July
df <- df %>%
filter(year(date) == 2020) %>%
filter(date >= "2020/05/01" & date < "2020/08/01")
# change date format for frame
df$date <- as.character(df$date)
# change state column name in regions to merge data frames
colnames(regions)[1] <- "state"
# merge the data frames
df2 <- left_join(df, regions, by = "state")
df2 <- na.omit(df2)
# view structure
str(df2)
## 'data.frame': 4692 obs. of 9 variables:
## $ X : int 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 ...
## $ date : chr "2020-05-01" "2020-05-01" "2020-05-01" "2020-05-01" ...
## $ state : chr "Alabama" "Alaska" "Arizona" "Arkansas" ...
## $ fips : int 1 2 4 5 6 8 9 10 11 12 ...
## $ cases : num 7294 362 7962 3310 52318 ...
## $ deaths : int 289 7 330 64 2147 818 2339 159 231 1313 ...
## $ State.Code: chr "AL" "AK" "AZ" "AR" ...
## $ Region : chr "South" "West" "West" "South" ...
## $ Division : chr "East South Central" "Pacific" "Mountain" "West South Central" ...
## - attr(*, "na.action")= 'omit' Named int [1:368] 12 37 42 50 67 92 97 105 122 147 ...
## ..- attr(*, "names")= chr [1:368] "12" "37" "42" "50" ...
df2 %>%
plot_ly(x = ~cases, y = ~deaths, color = ~Region,
hoverinfo = "text", # custom hover info
text = ~paste("State:", state, "<br>",
"Region:", Region, "<br>",
"Division:", Division, "<br>",
"Cases:", cases, "<br>",
"Deaths:", deaths)) %>%
add_markers(frame = ~date, # animate over date
size = 2) %>% # adjust point size
animation_slider(currentvalue = list(prefix = NULL, # adjustment for the current date on the slider
font = list(color = toRGB("indianred3"),
size = 20))) %>%
animation_opts(frame = 100, transition = 50) %>%
layout(xaxis = list(title = "number of cases"),
yaxis = list(title = "number of deaths"),
title = list(text = "Covid Deaths vs Cases for 2020 by State",
font = list(size = 17, color = toRGB("indianred3")),
y = 0.95, x = 0.13)) # change position of title for readability
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.
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).