Packages

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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\RtmpK4QaAd\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`
summary(avocado)
##       ...1            Date             AveragePrice    Total Volume     
##  Min.   : 0.00   Min.   :2015-01-04   Min.   :0.440   Min.   :      85  
##  1st Qu.:10.00   1st Qu.:2015-10-25   1st Qu.:1.100   1st Qu.:   10839  
##  Median :24.00   Median :2016-08-14   Median :1.370   Median :  107377  
##  Mean   :24.23   Mean   :2016-08-13   Mean   :1.406   Mean   :  850644  
##  3rd Qu.:38.00   3rd Qu.:2017-06-04   3rd Qu.:1.660   3rd Qu.:  432962  
##  Max.   :52.00   Max.   :2018-03-25   Max.   :3.250   Max.   :62505647  
##       4046               4225               4770           Total Bags      
##  Min.   :       0   Min.   :       0   Min.   :      0   Min.   :       0  
##  1st Qu.:     854   1st Qu.:    3009   1st Qu.:      0   1st Qu.:    5089  
##  Median :    8645   Median :   29061   Median :    185   Median :   39744  
##  Mean   :  293008   Mean   :  295155   Mean   :  22840   Mean   :  239639  
##  3rd Qu.:  111020   3rd Qu.:  150207   3rd Qu.:   6243   3rd Qu.:  110783  
##  Max.   :22743616   Max.   :20470573   Max.   :2546439   Max.   :19373134  
##    Small Bags         Large Bags       XLarge Bags           type          
##  Min.   :       0   Min.   :      0   Min.   :     0.0   Length:18249      
##  1st Qu.:    2849   1st Qu.:    127   1st Qu.:     0.0   Class :character  
##  Median :   26363   Median :   2648   Median :     0.0   Mode  :character  
##  Mean   :  182195   Mean   :  54338   Mean   :  3106.4                     
##  3rd Qu.:   83338   3rd Qu.:  22029   3rd Qu.:   132.5                     
##  Max.   :13384587   Max.   :5719097   Max.   :551693.7                     
##       year         region         
##  Min.   :2015   Length:18249      
##  1st Qu.:2015   Class :character  
##  Median :2016   Mode  :character  
##  Mean   :2016                     
##  3rd Qu.:2017                     
##  Max.   :2018
class(avocado$Date)
## [1] "Date"
unique(avocado$region)
##  [1] "Albany"              "Atlanta"             "BaltimoreWashington"
##  [4] "Boise"               "Boston"              "BuffaloRochester"   
##  [7] "California"          "Charlotte"           "Chicago"            
## [10] "CincinnatiDayton"    "Columbus"            "DallasFtWorth"      
## [13] "Denver"              "Detroit"             "GrandRapids"        
## [16] "GreatLakes"          "HarrisburgScranton"  "HartfordSpringfield"
## [19] "Houston"             "Indianapolis"        "Jacksonville"       
## [22] "LasVegas"            "LosAngeles"          "Louisville"         
## [25] "MiamiFtLauderdale"   "Midsouth"            "Nashville"          
## [28] "NewOrleansMobile"    "NewYork"             "Northeast"          
## [31] "NorthernNewEngland"  "Orlando"             "Philadelphia"       
## [34] "PhoenixTucson"       "Pittsburgh"          "Plains"             
## [37] "Portland"            "RaleighGreensboro"   "RichmondNorfolk"    
## [40] "Roanoke"             "Sacramento"          "SanDiego"           
## [43] "SanFrancisco"        "Seattle"             "SouthCarolina"      
## [46] "SouthCentral"        "Southeast"           "Spokane"            
## [49] "StLouis"             "Syracuse"            "Tampa"              
## [52] "TotalUS"             "West"                "WestTexNewMexico"
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))

ggplot(data = avocado_us, 
       mapping = aes(x = Date, y = AveragePrice, color = type)) +
  geom_line() +
  scale_y_continuous(labels = scales::dollar_format()) +  
  scale_color_manual(values= c("red1", "red4")) +
  theme_minimal() +
  labs(title = "Average Price of US Avocados",
       caption = "Source: Kaggle") + transition_reveal(Date)+theme(
         plot.title = element_text(color="black", size=18, face="bold"),
         axis.title.x = element_text(color="#333333", size=14, face="bold"),
         axis.title.y = element_text(color="#333333", size=14, face="bold")
       )
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?