22 July 2018

Data preparation

First define the data - reference: https://www.enz.org/
Evolution of average house prices between 2013 and 2017 in New Zealand.

library(dplyr); library(tidyr)

df <- tibble::as.tibble(read.table("./data.txt", header=T, sep=";", 
                                   stringsAsFactors=F))
with_selection <- function(df, cities=c("Auckland")) {
  df %>% filter(City %in% cities) %>%
    filter(grepl("All .* Area", Surburb)) %>%
    gather(AvgHousePrices_2013:AvgHousePrices_2017, 
           key = "year", value = "AvgHousePrices") %>%
    separate(year, into = c("todrop", "year"), sep="_", convert=T) %>%
    select(-todrop)
}

df_akl <- df %>% with_selection()
df_wel <- df %>% with_selection(cities = c("Wellington"))
df_chc <- df %>% with_selection(cities = c("Christchurch"))

Data Preparation (cont'ed)

Summary

     City             Surburb               year      AvgHousePrices   
 Length:5           Length:5           Min.   :2013   Min.   : 631000  
 Class :character   Class :character   1st Qu.:2014   1st Qu.: 714000  
 Mode  :character   Mode  :character   Median :2015   Median : 829000  
                                       Mean   :2015   Mean   : 834800  
                                       3rd Qu.:2016   3rd Qu.: 956000  
                                       Max.   :2017   Max.   :1044000  
    City.1           Surburb.1             year.1     AvgHousePrices.1
 Length:5           Length:5           Min.   :2013   Min.   :395000  
 Class :character   Class :character   1st Qu.:2014   1st Qu.:401000  
 Mode  :character   Mode  :character   Median :2015   Median :405000  
                                       Mean   :2015   Mean   :422800  
                                       3rd Qu.:2016   3rd Qu.:427000  
                                       Max.   :2017   Max.   :486000  
    City.2           Surburb.2             year.2     AvgHousePrices.2
 Length:5           Length:5           Min.   :2013   Min.   :455500  
 Class :character   Class :character   1st Qu.:2014   1st Qu.:475667  
 Mode  :character   Mode  :character   Median :2015   Median :495333  
                                       Mean   :2015   Mean   :490300  
                                       3rd Qu.:2016   3rd Qu.:511833  
                                       Max.   :2017   Max.   :513167  

Plot

Two ways:

  1. Directly with plotly

  2. With ggplot2 and plotly

with plotly

with plotly (cont'ed)

library(plotly)

plot_ly(ndf, x=df_akl$year, y=~df_akl$AvgHousePrices,  color=c("slateblue"),
        type='scatter', mode='lines+markers', name="Auckland", 
        line=list(width=2)) %>%
        add_trace(y=~df_wel$AvgHousePrices, mode='lines+markers', 
                  name="Wellington", color=c("lightgreen")) %>%
        add_trace(y=~df_chc$AvgHousePrices, mode='lines+markers', 
                  name="Christchurch", color=c("ligthsalmon")) %>%
        add_annotations(text="Average House price evolution\n million $NZ", 
                        xref="paper", yref="paper",
                        x=0.1, xanchor="left",
                        y=0.9, yanchor="top",
                        legendtitle=T, showarrow=F) %>%
        layout(legend=list(y=0.3, yanchor="bottom"),
               yaxis = list(title = "Prices in million of $NZ",
                            zeroline = T,
                            range = range(c(200000, df_akl$AvgHousePrices))),
               xaxis = list(title = 'Years')) 

with ggplot2 + plotly

with ggplot2 + plotly (cont'ed)

library(ggplot2)
library(plotly)

gp <- ggplot(ndf, aes(x=year, y=AvgHousePrices/1000, colour=City)) +
    geom_point(alpha=.8) +
    geom_line(alpha=.6, size=1) +
    xlab("years") + ylab("thousand $NZ") +
    ggtitle("Average House price evolution thousand $NZ") +
    theme_classic() +
    theme(
      plot.title = element_text(color="black", size=10, face="bold", hjust=0.0),
      legend.position=c(1.0, 0.1)
    ) 

ggplotly(gp, session="knitr") %>%
   layout(legend=list(x=1.0, y=0.1))