Use the avocado sales data to look at avocado prices and sales by city, year, and type (organic vs conventional).
library(readr)
library(ggplot2)
data <- read_csv('avocado.csv')
summary(data)
## date average_price total_volume type year geography Mileage
## Length :12628 Min. :0.500 Min. : 253 Length :12628 Min. :2017 Length :12628 Min. : 111
## N.unique : 154 1st Qu.:1.100 1st Qu.: 15733 N.unique : 2 1st Qu.:2018 N.unique : 41 1st Qu.:1097
## N.blank : 0 Median :1.320 Median : 94806 N.blank : 0 Median :2019 N.blank : 0 Median :2193
## Min.nchar: 8 Mean :1.359 Mean : 325259 Min.nchar: 7 Mean :2019 Min.nchar: 5 Mean :1911
## Max.nchar: 10 3rd Qu.:1.570 3rd Qu.: 430222 Max.nchar: 12 3rd Qu.:2020 Max.nchar: 20 3rd Qu.:2632
## Max. :2.780 Max. :5660216 Max. :2020 Max. :2998
str(data)
## spc_tbl_ [12,628 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ date : chr [1:12628] "2017/12/3" "2017/12/3" "2017/12/3" "2017/12/3" ...
## $ average_price: num [1:12628] 1.39 1.44 1.07 1.62 1.43 1.58 1.14 1.77 1.4 1.88 ...
## $ total_volume : num [1:12628] 139970 3577 504933 10609 658939 ...
## $ type : chr [1:12628] "conventional" "organic" "conventional" "organic" ...
## $ year : num [1:12628] 2017 2017 2017 2017 2017 ...
## $ geography : chr [1:12628] "Albany" "Albany" "Atlanta" "Atlanta" ...
## $ Mileage : num [1:12628] 2832 2832 2199 2199 2679 ...
## - attr(*, "spec")=
## .. cols(
## .. date = col_character(),
## .. average_price = col_double(),
## .. total_volume = col_double(),
## .. type = col_character(),
## .. year = col_double(),
## .. geography = col_character(),
## .. Mileage = col_double()
## .. )
## - attr(*, "problems")=<pointer: 0x565a95192670>
hist(data$average_price,
main = "Histogram of average_price",
xlab = "Price in USD")
ggplot(data, aes(x = average_price, fill = type)) +
geom_histogram(bins = 30, col = "red") +
scale_fill_manual(values = c("blue", "green")) +
ggtitle("Frequency of Average Price - Organic vs. Conventional")
ggplot(data, aes(x = date, y = average_price)) +
geom_point() + geom_smooth() +
ggtitle("average_price as a function of date")
range(data$average_price)
## [1] 0.50 2.78
data[which.min(data$average_price), ]
## # A tibble: 1 × 7
## date average_price total_volume type year geography Mileage
## <chr> <dbl> <dbl> <chr> <dbl> <chr> <dbl>
## 1 2018/4/22 0.5 2335867 conventional 2018 Houston 1656
data[which.max(data$average_price), ]
## # A tibble: 1 × 7
## date average_price total_volume type year geography Mileage
## <chr> <dbl> <dbl> <chr> <dbl> <chr> <dbl>
## 1 2019/7/14 2.78 15994 organic 2019 San Diego 253
The lowest average price was $0.50 and the highest was $2.78.
Dollar sales = average price x total volume.
data$sales <- data$average_price * data$total_volume
d <- subset(data, year %in% c(2017, 2018))
city_sales <- aggregate(sales ~ year + type + geography, data = d, FUN = sum)
top <- do.call(rbind, lapply(split(city_sales, list(city_sales$year, city_sales$type)),
function(x) x[which.max(x$sales), ]))
top
## year type geography sales
## 2017.conventional 2017 conventional Los Angeles 13456646.8
## 2018.conventional 2018 conventional Los Angeles 153526216.3
## 2017.organic 2017 organic New York 720450.9
## 2018.organic 2018 organic New York 9391204.2
ggplot(d, aes(x = reorder(geography, sales), y = sales, fill = type)) +
geom_col() +
coord_flip() +
facet_wrap(~ year) +
labs(title = "Dollar Sales by City (2017 and 2018)", x = "City", y = "Dollar sales")
Los Angeles had the most conventional sales both years and New York had the most organic sales both years. The 2017 data only starts in December, so those totals are smaller.
data$date <- as.Date(data$date, format = "%Y/%m/%d")
ggplot(data, aes(x = date, y = average_price, color = type)) +
stat_summary(geom = "line", fun = mean) +
labs(title = "Avocado Price Over Time", x = "Date", y = "Average Price ($)")
Prices go up in the summer and down in the winter, so stores could run sales in the cheaper months and plan inventory before prices go up.
ggplot(data, aes(x = type, y = average_price, fill = type)) +
geom_boxplot() +
labs(title = "Price: Organic vs Conventional", x = "Type", y = "Average Price ($)")
aggregate(average_price ~ type, data = data, FUN = mean)
## type average_price
## 1 conventional 1.142566
## 2 organic 1.575117
Organic costs about $0.44 more on average ($1.58 vs $1.14) but people still buy it, so organic can be marketed to shoppers who are willing to pay more.