R Markdown

install.packages(c('gapminder','ggplot2','gganimate','gifski','tidyverse'),repos="https://cran.us.r-project.org")
## Installing packages into 'C:/Users/Dell/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## package 'gapminder' successfully unpacked and MD5 sums checked
## package 'ggplot2' successfully unpacked and MD5 sums checked
## package 'gganimate' successfully unpacked and MD5 sums checked
## package 'gifski' successfully unpacked and MD5 sums checked
## package 'tidyverse' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Dell\AppData\Local\Temp\RtmpoJ6LUV\downloaded_packages
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.1.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.1.3
library(gifski)
## Warning: package 'gifski' was built under R version 4.1.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## Warning: package 'tibble' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'purrr' was built under R version 4.1.3
## Warning: package 'dplyr' was built under R version 4.1.3
## Warning: package 'stringr' was built under R version 4.1.3
## Warning: package 'forcats' was built under R version 4.1.3
## Warning: package 'lubridate' was built under R version 4.1.3
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v dplyr     1.1.0     v readr     2.1.4
## v forcats   1.0.0     v stringr   1.5.0
## v lubridate 1.9.2     v tibble    3.1.8
## v purrr     1.0.1     v tidyr     1.3.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
avocado <- read_csv("avocado.csv")
## New names:
## Rows: 18249 Columns: 14
## -- Column specification
## -------------------------------------------------------- Delimiter: "," chr
## (2): type, region dbl (11): ...1, AveragePrice, Total Volume, 4046, 4225, 4770,
## Total Bags, S... date (1): Date
## i Use `spec()` to retrieve the full column specification for this data. i
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## * `` -> `...1`
avocado_us <- avocado %>% filter(region == "TotalUS")
states <- c("California")
avocado_CA <- avocado %>% filter(region %in% states)
regions <- c("West","Southeast","SouthCentral","Plains","Northeast","Midsouth","GreatLakes","WestTexNewMexico","NorthernNewEngland")
avocado_region <- avocado %>% filter(region %in% regions)
avocado_cities <- avocado %>% filter(!region %in% c("TotalUS", states, regions))

avocado_cities_filtered <- avocado_cities %>% 
  filter(type == "conventional",
         Date > as.Date("2018-01-01"))


ggplot(data = avocado_cities_filtered,
       mapping = aes(x = AveragePrice, y = `Total Volume`, color = region)) +
  scale_y_continuous(labels = scales::comma_format()) +
  scale_x_continuous(labels = scales::dollar_format()) +
  geom_point(aes(size = `Total Volume`), alpha = .6) +  
  theme_minimal() +
  theme(legend.position = "none") +
  labs(title = "Avocados sold by price and city",
       subtitle = "Date: {frame_time}") +
  transition_time(Date) +
  shadow_wake(wake_length = 0.2)