i need to load libraries
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.1
library(gganimate)
## Warning: package 'gganimate' was built under R version 3.6.2
next I need a dataset
temperatureData <- read.csv("C:/Users/Owner/Downloads/1970977.csv")
check the data
head(temperatureData)
## STATION NAME DATE TAVG
## 1 USW00013967 OKLAHOMA CITY WILL ROGERS WORLD AIRPORT, OK US 2015-01-01 27
## 2 USW00013967 OKLAHOMA CITY WILL ROGERS WORLD AIRPORT, OK US 2015-01-02 33
## 3 USW00013967 OKLAHOMA CITY WILL ROGERS WORLD AIRPORT, OK US 2015-01-03 37
## 4 USW00013967 OKLAHOMA CITY WILL ROGERS WORLD AIRPORT, OK US 2015-01-04 28
## 5 USW00013967 OKLAHOMA CITY WILL ROGERS WORLD AIRPORT, OK US 2015-01-05 26
## 6 USW00013967 OKLAHOMA CITY WILL ROGERS WORLD AIRPORT, OK US 2015-01-06 36
## TMAX TMIN
## 1 34 24
## 2 37 31
## 3 42 35
## 4 34 18
## 5 44 16
## 6 48 28
clean the data
temperatureData$NAME <- as.character(temperatureData$NAME)
temperatureData$DATE <- as.Date(temperatureData$DATE)
temperatureData <- as.data.frame(temperatureData)
make a static ggplot of the data
ggplot(temperatureData,
aes(x = DATE, y = TAVG)
) +
geom_point(show.legend = F, alpha = 0.7, col = "red")+
labs(x = "Date", y = "Degrees Fahrenheit", title = "Average Daily Temperatures Over Time", caption = "Data from weather station at Will Rogers World Airport", subtitle = "2015-2019")
assign the plot to a variable and display also i changed it to a line plot so it looks better in animation
myPlot <-
ggplot(temperatureData,
aes(x = DATE, y = TAVG)
) +
geom_line(show.legend = F, alpha = 0.7, col = "red")+
labs(x = "Date", y = "Degrees Fahrenheit", title = "Average Daily Temperatures Over Time", caption = "Data from weather station at Will Rogers World Airport", subtitle = "2015-2019")
myPlot
add movement for an animation and follow it with a fix y axis.
myPlot + transition_reveal(DATE)+ view_follow(fixed_y = T)
i want to assign the animation to a variable to use the animate function
myPlotAnimation <-
ggplot(temperatureData,
aes(x = DATE, y = TAVG)
) +
geom_line(show.legend = F, alpha = 0.7, col = "red")+
labs(x = "Date", y = "Degrees Fahrenheit", title = "Average Daily Temperatures Over Time", caption = "Data from weather station at Will Rogers World Airport", subtitle = "2015-2019") +
transition_reveal(DATE) + view_follow(fixed_y = T)
now use the animate function to add a pause at the end
animate(myPlotAnimation, end_pause = 15)
a simpler plot without the fixed y-axis
animate(myPlot + transition_reveal(DATE), end_pause = 15)