Last active
October 25, 2024 00:08
-
-
Save USMortality/2a19fdf5a194f3c95e95e12fea4f7092 to your computer and use it in GitHub Desktop.
Median Sales Price of Houses Sold [USA]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(readr) | |
library(dplyr) | |
library(ggplot2) | |
library(lubridate) | |
library(tsibble) | |
library(scales) | |
sf <- 2 | |
width <- 600 * sf | |
height <- 335 * sf | |
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf)) | |
# Median Home Prices | |
df <- read_csv( | |
paste0( | |
paste0( | |
"https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e9f0&", | |
"chart_type=line&drp=0&fo=open%20sans&graph_bgcolor=%23ffffff&", | |
"height=450&mode=fred&recession_bars=on&txtcolor=%23444444&ts=12", | |
"&tts=12&width=958&nt=0&thu=0&trc=0&show_legend=yes&show_axis_titles=yes", | |
"&show_tooltip=yes&id=MSPUS&scale=left&cosd=1963-01-01&coed=" | |
), | |
Sys.Date(), | |
paste0( | |
"&line_color=%234572a7&link_values=false&line_style=solid&", | |
"mark_type=none&mw=3&lw=2&ost=-99999&oet=99999&mma=0&fml=a&fq=Quarterly&", | |
"fam=avg&fgst=lin&fgsnd=2020-02-01&line_index=1&transformation=lin", | |
"&vintage_date=" | |
), | |
Sys.Date(), | |
"&revision_date=", | |
Sys.Date(), | |
"&nd=1963-01-01" | |
) | |
) | |
df_median_home_price <- df |> | |
setNames(c("date", "price")) |> | |
mutate(date = yearquarter(ymd(date))) |> | |
as_tsibble(index = date) | |
chart <- | |
ggplot(df_median_home_price, aes(x = date, y = price)) + | |
scale_x_yearquarter(date_breaks = "8 year") + | |
scale_y_continuous( | |
trans = "log2", | |
labels = scales::dollar_format(prefix = "$", suffix = "") | |
) + | |
geom_line(aes(y = price)) + | |
labs( | |
title = "Median Sales Price of Houses Sold [USA]", | |
subtitle = "Source: Federal Reserve Bank of St. Louis", | |
y = "US Dollars", | |
x = "Quarter of Year" | |
) + | |
geom_smooth( | |
method = "lm", | |
formula = y ~ x, | |
color = "black", | |
linetype = "dashed" | |
) + | |
ggrepel::geom_label_repel( | |
data = tail(df_median_home_price, n = 1) |> | |
dplyr::mutate(str = paste0( | |
date, ": ", dollar(price) | |
)), | |
aes(label = str), | |
nudge_y = 0.1, | |
segment.color = "grey50", | |
) | |
ggplot2::ggsave( | |
filename = "chart1.png", plot = chart, width = width, height = height, | |
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.mortality.watch/charts/list.html#median-sales-price-of-houses-sold-usa