#Load some packages# 载入一些程序包
library(data.table, warn.conflicts = FALSE, quietly=TRUE)
library(ggplot2, warn.conflicts = FALSE, quietly=TRUE)
library(dplyr, warn.conflicts = FALSE, quietly=TRUE)
library(zoo, warn.conflicts = FALSE, quietly=TRUE)
library(ggrepel, warn.conflicts = FALSE, quietly=TRUE)
library(ggthemes, warn.conflicts = FALSE, quietly=TRUE)
library(scales, warn.conflicts = FALSE, quietly=TRUE)
library(animation, warn.conflicts = FALSE, quietly=TRUE)
library(grid, warn.conflicts = FALSE, quietly=TRUE)
library(tidyr, warn.conflicts = FALSE, quietly=TRUE)
library(rgeos, warn.conflicts = FALSE, quietly=TRUE)
library(maptools, warn.conflicts = FALSE, quietly=TRUE)
library(albersusa, warn.conflicts = FALSE, quietly=TRUE)
#load data # 载入数据
statedata <- fread("data/fmhpi2016q3.txt")
statedata$date<-as.Date(statedata$date, format="%m/%d/%Y")
#Now uses some data table calculations to compute percent changes in house prices by state/metro
# 现在以州或者地铁为单位的使用数据集合计算结果来计算房价的百分比变化
statedata<-statedata[,hpa:=c(NA,((1+diff(hpi)/hpi))^12)-1,by=state]
statedata<-statedata[,hpa12:=c(rep(NA,12),((1+diff(hpi,12)/hpi))^1)-1,by=state]
#compute pre-2008 peak# 计算200年以前的峰值
statedata[,hpi.max08:=max(ifelse(year<2009,hpi,0),na.rm=T),by=state]
#compute post-2008 trough # 计算过去2008年的所有值
statedata[,hpi.min08:=min(ifelse(year>2008,hpi,1000),na.rm=T),by=state]
#do map stuff # 绘制地图
states<-usa_composite()
statedata[,iso_3166_2:=state] #rename state to match usa_composite
statedata[,hpi.comp:=hpi/hpi.max08-1] #compute house price growt
h relative to pre-2008 peak
statedata<-statedata[!(state %in% c("US.SA","US.NSA"))]
statedata[,hparank:=rank(-hpa12),by=date]
#create an indicator for house price appreciation relative to pre-2008 peak
#依据2008年以前的峰值创建一个新的房价评估的指数
statedata[,hpi.compd:=ifelse(hpi.comp< -0.25,"<-25%",
ifelse(hpi.comp< -0.05,"-25% to -5%",
ifelse(hpi.comp<0,"-5% to 0%",
ifelse(hpi.comp<0.05,"0% to +5%",
ifelse(hpi.comp<0.25,"+5% to +25%",
">+25%"))))) ]
statedata$hpi.compd<-factor(statedata$hpi.compd, levels=unique(statedata$hpi.compd))
smap<-fortify(states,region="fips_state")
states@data <- left_join(states@data, statedata, by = "iso_3166_2")
#make annual percent change map:
#绘制年百分比变化图
ggplot() +
geom_map(data = smap, map = smap,
aes(x = long, y = lat, map_id = id),
color = "#2b2b2b", size = 0.05, fill = NA) +
geom_map(data =subset(states@data,year==2016 & month==9), map = smap,
aes( map_id = fips_state,fill=hpa12),
color = "gray", size = .25) +
theme_map( base_size = 12) +
theme(plot.title=element_text( size = 16, margin=margin(b=10))) +
theme(plot.subtitle=element_text(size = 14, margin=margin(b=-20))) +
theme(plot.caption=element_text(size = 9, margin=margin(t=-15))) +
coord_proj(us_laea_proj) + labs(title="",subtitle="" ) +
scale_fill_viridis(name = "", discrete=F,option="D",end=0.95,
direction=1,limits=c(0,0.12),
label=percent
)+
theme(legend.position = "top") +theme(plot.caption=element_text(hjust=0,vjust=1,margin=margin(t=10)))+
labs(title="Annual House Price Growth (Y/Y %)",
caption="@lenkiefer Source: 房地美 House Price Index (Not Seasonally adjusted)")