library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
# install.packages("plotly")
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
library(ggplot2)
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).
File from assignment 1
meadville <- read.csv("meadville_weather.csv")
colnames(meadville)[15] <- "Obs" # rename unnamed column
meadville$Date <- as.Date(meadville$Date, format = "%m/%d/%y")
meadville
# Create a subset for weather data for months January through September
meadville_jan_sept <- subset(meadville, (Date >= "2025-01-01") & (Date < "2025-10-01"))
# create a column for month that we can facet over
meadville_jan_sept$Month <- format(meadville_jan_sept$Date, "%b")
# change month column into factor and order them so that slider will be in chronological order
meadville_jan_sept$Month <- factor(
meadville_jan_sept$Month,
levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept"),
ordered = TRUE
)
# create a day of the month variable for x-axis
meadville_jan_sept$Day <- as.numeric(format(meadville_jan_sept$Date, "%d"))
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
# categorical variable to facet under = Month
# x-axis = Day
# quantitative variables = High.Temp
# create scatterplot
# meadville_jan_sept %>%
# plot_ly(
# x = ~Day,
# y = ~High.Temp,
# frame = ~Month,
# type = "scatter",
# mode = "markers",
# showlegend = FALSE
# )
# or
p_anim <- meadville_jan_sept %>%
plot_ly(x = ~Day, y = ~High.Temp, hoverinfo = "text", text = ~ High.Temp) %>%
add_markers(frame = ~Month,
showlegend = FALSE,
size = ~High.Temp,
color = ~Month,
marker = list(sizemode = "diameter", sizeref = 3))
p_anim %>%
animation_opts(frame = 2000,
transition = 1000) %>%
animation_slider(font = list(color = "black",
size = 12)) %>%
layout(xaxis = list(title = "Day of the Month"),
yaxis = list(title = "High Temp"),
title = "Day of the Month vs. High Temp")
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
For this problem, I decided to use the Meadville, PA weather dataset I used for the first assignment in this course. I selected this dataset because I thought it would be interesting to analyze how the high temperatures change from month to month. To be more specific, this animated graph displays daily high temperatures in Meadville from January through September, with each frame representing a different month. The x-axis shows the day of the month, while the y-axis shows the corresponding high temperature. The reader can use the hovering cursor over the points on the graph to read the exact high temperature that the point is displaying. I thought that it may be hard to decipher the exact temperature strictly off of the y-axis, so this hover text would remove any ambiguity.
There were two main issues I ran into when creating this graph. First, I debated whether or not the marker argument, changing the size of the points relative to temperature magnitiude, was necessary. When I had sizeref less than 3, some of the hotter months would display points so large that they began to overlap and almost form a large line. This was good because it showed that the high temperatures of the month had a high magnitude and were rather consistent, however, the graph was not very clear to someone unsure of what they were looking at. By increasing the sizeref, I was able to find a happy medium of displaying the size of the high temperatures to make it easy to see the changes from month to month while also keeping the graph looking neat.
My next challenge was ensuring that the slider was labelled in chronological order instead of alphabetical order. This definitely required some trial and error. Initially, I tried adding a column that listed the number of the month so January would be month 1. The issue with this attempt was that I could not determine how to change the numeric values on the slider to month abbreviations. I came up with a new plan. To work around the fact that plotly automatically plots alphabetically, I made my Month column a factor and stated that it was ordered. This resolved the problem.
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.
# select dataset
storms_1 <- storms
storms_2 <- storms %>%
filter(!is.na(category)) %>%
filter(!is.na(pressure)) %>%
filter(!is.na(wind)) %>%
filter(!is.na(year)) %>%
mutate(category = factor(category, levels = 0:5, ordered = TRUE))
p_anim <- storms_2 %>%
plot_ly(x = ~pressure, y = ~wind, hoverinfo = "text", text = ~ name) %>%
add_markers(frame = ~year,
showlegend = TRUE,
size = ~wind,
color = ~category,
marker = list(sizemode = "diameter", sizeref = 2.25))
p_anim %>%
animation_opts(frame = 2000,
transition = 1000) %>%
animation_slider(hide = FALSE, currentvalue = list(prefix = NULL, font = list( size = 25, color = "gray10"))) %>%
layout(legend = list(title = list(text = "Category")),
xaxis = list(title = "Pressure"),
yaxis = list(title = "Wind"),
title = "Pressure vs. Wind")
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length
For this animated plotly graph, I decided to use the storms data set that can be found in the dplyr package. I thought that this would be an ideal data set because I could make each frame a year and look further into storm data over a period of time. I plotted storm wind speed against pressure. As mentioned, each frame represents a different year. I used storm category as a categorical variable to color the points. This means that you can select which category of storm you want to analyze. If you want to look specifically at categories 1 and 2, for instance, you can select only these two categories on the legend. This makes the visualization easy to use to analyze the trends between pressure and wind each year for any specific category or multiple categories of a storm. We can see that typically as pressure increases, wind speed decreases. The visualization also shows that higher categories of a storm have higher wind speeds. All of these trends illustrated in the plotly graph animation are what we would anticipate based on previous knowledge of storms.
When making this visualization, I ran into a few issues. The main issue was that originally “category” was listed as a numeric variable. I wanted to be able to select category levels in the legend on the plot after grouping over this variable. When “category” was numeric, I was unable to do so. My solution was to change the variable in the dataset to an ordered factor. Another issue occured when I tried to move the frame label into the plotting region. I attempted to do so and realized quickly that the graph looked too messy to interpret. Instead, I moved the frame label back to the original location outside of the graph, but deleted the prefix. I increased the font size and changed the color to ensure that the frame label stood out.
write.csv(storms_2, "storms_2.csv")
write.csv(meadville_jan_sept, "meadville_jan_through_sept.csv", row.names = FALSE)
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).