Last active
December 18, 2015 15:36
-
-
Save abresler/738be2df2284fed94401 to your computer and use it in GitHub Desktop.
Streamgraph NAREIT Growth
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(rvest); library(dplyr); library(tidyr);library(streamgraph); library(magrittr) | |
url <- #where does the data live | |
"https://www.reit.com/investing/industry-data-research/us-reit-industry-equity-market-cap" | |
reits_raw <- #scrape it | |
url %>% | |
html %>% | |
html_table(header = F) %>% | |
data.frame | |
names(reits_raw) <- #name it | |
c('year','companies_all.reits','marketcap_all.reits' | |
,'companies_equity.reits', 'marketcap_equity.reits' | |
,'companies_mortgage.reits', 'marketcap_mortgage.reits' | |
,'companies_hybrid.reits', 'marketcap_hybrid.reits') | |
reits <- #discard what we dont need | |
reits_raw[5:nrow(reits_raw),] | |
reits %<>% #convert to numbers | |
apply(2, function(x) extract_numeric(x)) %>% data.frame %>% tbl_df | |
reits_api_friendly <- #go from wide to long | |
reits %>% | |
gather(item, value, -year) | |
reits_api_friendly %<>% #split dual named columns | |
separate(item,c('aggregator','reit_type'), "_") | |
dat <- #filter the data for the plot -- exclude all | |
reits_api_friendly %>% | |
filter(aggregator %in% 'marketcap') %>% | |
filter(!reit_type == 'all.reits') | |
dat$year %<>% #datify it | |
paste0("12-31-",.) %>% as.Date("%m-%d-%Y") | |
dat %>% #plot it | |
streamgraph("reit_type", "value", "year",offset="silhouette", #what type of graph | |
interactive=TRUE) %>% | |
sg_axis_x(4, "year", "%Y") %>% #year increments | |
sg_axis_y(12, tick_format = "$4,d.0") %>% #y axis increments | |
sg_fill_brewer("Spectral") %>% | |
sg_legend(TRUE, "REIT Type: ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment