---
title: "Chapter 12 w/ Zillow Data"
format: html
toc: true
theme: flatly
---


# Packages

1. scales
2. ggrepel (extension)
3. patchwork (extension)


```{r}
# packages for the day
library(tidyverse)
library(scales)
library(ggrepel)
library(patchwork)
```


# Data

```{r}
# the code below "calls" the script zillow-class.R, it's equivalent to running the code contained therein from the command line
source(file = "zillow-class.R")
# change name to zillow
zillow <- ls_df; 

# remove original data set
rm(ls_df)
```


## compare states

```{r}
states <- zillow |> group_by(state_name) |> 
          summarize(
            mean_sell = mean(sell_price, na.rm = TRUE),
            mean_list = mean(list_price, na.rm = TRUE),
            n = n())
```


## Labels

```{r}
ggplot(states, aes(
  x = fct_reorder(state_name,mean_sell), 
  y = mean_sell)) +
  geom_bar(aes(fill = state_name), 
           stat = "identity",
           width = .3) +
  geom_point(aes(fill = state_name, color = state_name)) +
  labs(
    x = "States",
    y = "Mean Selling Price",
    color = "State",
    title = "Per State Selling Prices",
    subtitle = "who's winning?",
    caption = "Data from Zillow"
  ) + 
  theme(legend.position = "none",
        axis.text.y = element_text(size = 6)) + 
  coord_flip() + 
  scale_y_continuous(labels = label_dollar())
```

```{r}
states <- mutate(states, 
                 state_name = fct_reorder(state_name, mean_sell))
ggplot(states, aes(
  x = state_name, 
  y = mean_sell)) +
  geom_point(aes(fill = state_name, color = state_name)) +
  geom_segment(aes(
      xend = state_name,
      yend = mean_list,
      color = state_name)) + 
  geom_point(aes(y = mean_list, color = state_name),
                 fill = "black",
                 shape = 21) +
  labs(
    x = "States",
    y = "Mean Selling Price",
    color = "State",
    title = "Per State Selling Prices",
    subtitle = "List prices (in black) compared to selling prices",
    caption = "Data from Zillow"
  ) + 
  scale_y_continuous(labels = label_dollar()) +
  scale_color_viridis_d() + 
  theme(legend.position = "none",
        axis.text.y = element_text(size = 6)) + 
  coord_flip()
```



  ## Scales
  

```{r}
  # x axis tick mark labels
# p + theme(axis.text.x= element_text(family, face, colour, size))
# y axis tick mark labels
# p + theme(axis.text.y = element_text(family, face, colour, size))
```

