library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.3
library(gganimate)
## Warning: package 'gganimate' was built under R version 3.6.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.3
##
## 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(datasets)
glimpse(airquality)
## Rows: 153
## Columns: 6
## $ Ozone <int> 41, 36, 12, 18, NA, 28, 23, 19, 8, NA, 7, 16, 11, 14, 18, 1...
## $ Solar.R <int> 190, 118, 149, 313, NA, NA, 299, 99, 19, 194, NA, 256, 290,...
## $ Wind <dbl> 7.4, 8.0, 12.6, 11.5, 14.3, 14.9, 8.6, 13.8, 20.1, 8.6, 6.9...
## $ Temp <int> 67, 72, 74, 62, 56, 66, 65, 59, 61, 69, 74, 69, 66, 68, 58,...
## $ Month <int> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,...
## $ Day <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ...
We’ll make an animation of daily temperature.
temp <- airquality %>%
select(Day,Temp,Month) %>%
group_by(Month,Day) %>%
transform(Month = as.factor(Month)) %>%
filter(!is.na(Temp))
head(temp,10)
## Day Temp Month
## 1 1 67 5
## 2 2 72 5
## 3 3 74 5
## 4 4 62 5
## 5 5 56 5
## 6 6 66 5
## 7 7 65 5
## 8 8 59 5
## 9 9 61 5
## 10 10 69 5
p <- airquality %>%
transform(Month = as.factor(Month)) %>%
ggplot(aes(Day, Temp, group = Month, color = Month)) +
geom_line() +
theme_dark()+
coord_cartesian(ylim = c(50,100))+
scale_color_viridis_d() +
labs(x = "Day of Month", y = "Temperature") +
theme(legend.position = "right",legend.background = element_rect(colour = "blue",fill = "#00b7ae"))+
labs(title = "Daily Temperatures")
p
p + transition_reveal(Day)
pt <- ggplot(temp,aes(Day, Temp, group = Month,col = Month)) +
geom_line() +
scale_color_viridis_d() +
labs(x = "Day of Month", y = "Temperature") +
theme(legend.position = "right",legend.background = element_rect(colour = "blue",fill = "gold"))+
theme_dark()+
labs(title = "Daily Temperature changes from May to September")
pt + transition_reveal(Day)
## Animate barplot
bdata <- temp %>%
group_by(Month) %>%
summarise(Mean = mean(Temp,na.rm = TRUE))
bdata
## Warning: `...` is not empty.
##
## We detected these problematic arguments:
## * `needs_dots`
##
## These dots only exist to allow future extensions and should be empty.
## Did you misspecify an argument?
## # A tibble: 5 x 2
## Month Mean
## <fct> <dbl>
## 1 5 65.5
## 2 6 79.1
## 3 7 83.9
## 4 8 84.0
## 5 9 76.9
bplot <- ggplot(bdata,aes(x = as.factor(Month),y = Mean, fill = Month))+
geom_bar(stat = "identity")+
coord_cartesian(ylim = c(0,100))+
theme_dark()+
theme(legend.position = "right",legend.background = element_rect(colour = "blue",fill = "gold"))+
labs(x = "Month", y = "Mean Temperature",title = "Daily Mean Temperatures")
bplot
bplot+transition_reveal(as.numeric(Month))
bdata2 <- temp %>%
transform(Month = as.factor(Month)) %>%
group_by(Month) %>%
summarise(Mean = mean(Temp,na.rm = TRUE))
## Plot
bplot2 <- ggplot(bdata2,aes(x = Month,y = Mean, fill = Month, label = round(Mean,2)))+
geom_bar(stat = "identity")+
labs(x = "Month", y = "Mean Temperature",title = "Average Monthly Temperatures",caption = "Data Source: airquality Data")+
coord_cartesian(ylim = c(0,100))+
theme_dark()+
theme(axis.text.x = element_text(face="bold", color="#993366",size=11, angle=45),
axis.text.y = element_text(face="bold", color="#b3b3b3",size=11, angle=45))+
geom_label(col = "black",cex = 2.5)+
scale_x_discrete(labels = c("5" = "May","6" = "Jun","7" = "Jul","8" = "Aug","9" = "Sept"))
## Animate
bplot2+transition_reveal(as.numeric(Month))