Chapter 12 w/ Zillow Data

Published

October 22, 2024

Packages

  1. scales
  2. ggrepel (extension)
  3. patchwork (extension)
# packages for the day
library(tidyverse)
library(scales)
library(ggrepel)
library(patchwork)
library(tidyquant)

Data Import & Tidy

# load the data saved in "zillow-load.html"
load("zillow-data.Rdata")
# change name to zillow
zillow <- zil_sub; 

# remove original data set
rm(zil_sub)

compare states

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

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())

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

Adjust the code below to improve the plots above.

# 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))