<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/rent-prices-and-trelliscopejs/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.stoltzmaniac.com/rent-prices-and-trelliscopejs/"> R-Projects – Stoltzmaniac</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Rent Prices are Soaring in Most of Colorado</h2>
<p>Call it gentrification, supply-and-demand, call it whatever you’d like… the fact is, rent prices have gone up in Colorado in the last decade. Chip Oglesby – <a target="_blank" rel="nofollow" href="http://www.stoltzmaniac.com/data-visualization-part-1/">GitHub</a> – did a nice analysis on the data provided by <a target="_blank" rel="nofollow" href="https://colorado.gov/">colorado.gov</a>.</p>
<p>Chip’s analysis can be seen <a target="_blank" rel="nofollow" href="https://github.com/chipoglesby/coloradoRent/blob/master/analysis/README.md">here</a>.</p>
<p>His analysis states, “Efficiency apartments in Fort Collins/Loveland saw the largest increase in rent between 1996 and 2015. During this 19 year period, rent rose 226.5% from $239.26 to $781.18.”</p>
<p>One of his charts for median prices for Fort Collins/Loveland is very telling:</p>
<p><img title="plot of chunk unnamed-chunk-1" alt="plot of chunk unnamed-chunk-1" src="https://i2.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/04/unnamed-chunk-1-1.png?w=456&ssl=1" data-recalc-dims="1"><br>
<span></span>Sorry about the small font size, static image didn’t transfer well – but the 226.5% increase should be apparent!</p>
<p>I wanted to see if I could do a couple of fun visualizations and test out a package which is brand new to me – trelliscopejs. If you don’t know much about it, I suggest you look it up! It allows for interactivity, filtering, sorting, and plenty of other features without having to learn any new syntax (basically)! I don’t know <a target="_blank" rel="nofollow" href="http://ryanhafen.com/blog/trelliscopejs">Ryan Hafen</a>, its creator, but he provided this to the community for free so I’m already a fan.</p>
<p>Anyway, let’s get started.</p>
<p>As always, load some libraries and get the data loaded.</p>
<pre>library(rjson)
library(tidyverse)
library(RCurl)
library(RSocrata)
library(ggrepel)
library(ggmap)
library(plotly)

#########################
# Download and Clean Data
#########################
# Credentials can be made at https://data.colorado.gov/login
creds = fromJSON(file = &quot;credentials.json&quot;)
url = &quot;https://data.colorado.gov/resource/yifv-9mje.json&quot;
data = read.socrata(url,creds$token)

# Removing NA and omitting &quot;Metro Average&quot; because it's not actually a place
df = data %&amp;gt;% na.omit() %&amp;gt;% filter(subregion != 'Metro Average')

# converting 'averagerent' to numeric
df$averagerent = round(as.numeric(df$averagerent),0)

############################################
# Get Longitude and Latitude data &amp;amp; Merge it
############################################
# I downloaded with geocode() and then saved it as a csv to save time, I left the code commented out
# co.locations = df %&amp;gt;% select(subregion) %&amp;gt;% unique()
# co.locations = paste(co.locations$subregion,', CO',sep='')
# lonLat = geocode(co.locations)
# lonLat$subregion = sub(&quot;, CO&quot;, '',co.locations)
# write.csv(lonLat,'subregion_lon_lat.csv',row.names=FALSE)

lonLat = read.csv('subregion_lon_lat.csv')

mergedDF = merge(df,lonLat,by='subregion')
annualDF = mergedDF %&amp;gt;% 
  filter(apartmenttype == 'All') %&amp;gt;% 
  group_by(subregion,lon,lat,year) %&amp;gt;%
  summarise(rent = round(mean(averagerent)),0) %&amp;gt;%
  arrange(year) %&amp;gt;% filter(year &amp;gt;= 2015)</pre>
<p>It’s time to break into some regular ggplot2 plotting. I really wanted to see the most recent data (which was 2015) by city on a map. This would be a great view if you wanted to move to this state and cared about price and geographical location! However, trying to plot this all on a static image can be a bit rough…</p>
<pre>colo_map = qmap(&quot;Colorado, United States&quot;,zoom = 7, source = &quot;google&quot;,legend='topleft')
colo_map + geom_point(aes(x=lon, y=lat, col=rent),
                      data = annualDF, 
                      alpha = 0.9,
                      size=9) + scale_color_gradient(low=&quot;green&quot;, high=&quot;red&quot;) + 
  geom_label_repel(data=annualDF,aes(label=paste(subregion,&quot;\n&quot;,&quot;$&quot;,rent)),size=2.5) + 
  ggtitle(&quot;Colorado Average Rent Prices - 2015&quot;)</pre>
<p><img title="plot of chunk unnamed-chunk-3" alt="plot of chunk unnamed-chunk-3" src="https://i2.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/04/unnamed-chunk-3-1-5.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>That view gives you some information, but it blocks a lot out and looks extremely messy. I also tried it without any labels – which is much easier on the eyes but doesn’t provide much in terms of information.</p>
<p>Another fun visual is to cut it up into small multiples. The next plot shows the breakout of the “apartmenttype” variable. This could be useful for someone selecting a city based off of their family size.</p>
<pre>annualDF = mergedDF %&amp;gt;% 
  group_by(apartmenttype,subregion,lon,lat,year) %&amp;gt;%
  summarise(rent = round(mean(averagerent)),0) %&amp;gt;%
  arrange(year) %&amp;gt;% filter(year &amp;gt;= 2015)

#colo_map = qmap(&quot;Colorado, United States&quot;,zoom = 7, source = &quot;google&quot;,legend='bottom')
colo_map = qmap(&quot;Colorado, United States&quot;,zoom = 7, source='stamen', maptype='toner',legend='bottom')
colo_map + geom_point(aes(x=lon, y=lat, col=rent),
                      data = annualDF, 
                      alpha = 0.9,
                      size=5) + scale_color_gradient(low=&quot;green&quot;, high=&quot;red&quot;) + 
  facet_wrap(~apartmenttype) + 
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border=element_rect(fill=NA,size=2)) + 
  ggtitle(&quot;Colorado Average Rent Prices - 2015&quot;)</pre>
<p><img title="plot of chunk unnamed-chunk-4" alt="plot of chunk unnamed-chunk-4" src="https://i0.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/04/unnamed-chunk-4-1-3.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>It is kind of a fun chart but, again, doesn’t really offer enough information. Maps of small multiples can be extremely useful. I’ll explore this a bit more in the next week or two with this data set.</p>
<p>Now that we’re done with some random spatial views, we’ll take a look at trelliscopejs. All you need to do is take a normal ggplot object and replace facet_wrap() with facet_trelliscope(). The only thing to keep in mind is to add <code>self_contained=TRUE</code> if you are going to compile this in an RMarkdown file.</p>
<p>Just take a look at how <strong>AMAZING</strong> it is.</p>
<p>Below is a static image of how it looks.</p>
<p><strong>Due to limitations of WordPress, please go to: 
<h3><a target="_blank" rel="nofollow" href="https://www.stoltzmaniac.com/wp-content/uploads/2017/04/trelliscope/Renting_in_Colorado.html">Fully Functional TrelliscopeJS Chart</a></h3>
<p> and scroll to the bottom in order to see the real deal. If you know how I can<br>
upload this using knit2wp please let me know!</p></strong>
<pre>library(trelliscopejs)
library(rbokeh)

noQtrDF = df %&amp;gt;%
  group_by(apartmenttype,subregion,year) %&amp;gt;%
  summarise(averagerent = round(mean(averagerent),0))

noQtrDF$year = as.numeric(noQtrDF$year)
noQtrDF$apartmenttype = as.factor(noQtrDF$apartmenttype)
noQtrDF$subregion = as.factor(noQtrDF$subregion)

qplot(data=noQtrDF, x = year, y = averagerent) + 
  theme_bw() + 
  facet_trelliscope(~apartmenttype + subregion,nrow=2,ncol=3,self_contained=TRUE,name=&quot;Rent Prices by Location and Apartment Type&quot;)</pre>
<p><img title="plot of chunk unnamed-chunk-5" alt="plot of chunk unnamed-chunk-5" src="https://i0.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/04/unnamed-chunk-5-1-5.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Now you can compare whatever you want in terms of cities or apartment type. You can enlarge the image, add more plots for comparison, whatever you would like!</p>
<p>Sorry for the overexcitement – I just saw this package in use a few days ago and needed to put up a post about it, I’ll add this concept to upcoming posts!</p>
<p>As always, the code used in this post is on my <a target="_blank" rel="nofollow" href="https://github.com/stoltzmaniac/Renting-In-Colorado">GitHub</a></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.stoltzmaniac.com/rent-prices-and-trelliscopejs/"> R-Projects – Stoltzmaniac</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/WB--nasneVk"></a></div>