Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
274 lines (218 sloc) 10.2 KB
output
html_document
fig_width fig_height keep_md
9
5
true
#Note to run the animation you will have to instal Imagemagik from http://www.imagemagick.org/script/binary-releases.php
# when you install Imagemagik remember to select the "instal legacy files" option so convert.exe is included

#Set working directory where this script and the raw excel file are saved 
setwd("C:/Users/Dan/Documents/GitHub/MelbournePropertyPrices")

### Install/load required packages
#List of R packages required for this analysis:
required_packages <- c("ggmap", "readr", "ggplot2", "dplyr", "readxl","tidyr",
                       "stringr", "gganimate","animation","ggiraph", "viridis")
#Install required_packages:
new.packages <- required_packages[!(required_packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
#Load required_packages:
lapply(required_packages, require, character.only = TRUE)

#Set decimal points and disable scientific notation
options(digits=8, scipen=999) 

###Load Map
# Can download using get_map function:
# Melbourne <- get_map(location = 'Melbourne, Australia', zoom = 10, maptype="terrain")
# But I saved it to workind directory so can load it and work offline:
load("Melbourne.rda")

###Load Apartment price data
# Got this data here http://www.dtpli.vic.gov.au/property-and-land-titles/property-information/property-prices ...
# ...but saved it to workind directory so can load it and work offline
suburb_Apartment_2015<-read_excel("suburb_unit_2015.xls", col_names=F, skip = 3) %>% 
            na.omit() %>%
            distinct() %>%
            mutate(`Property Type`="Apartment")
#Load House Price Data and row bind it to Apartment price data 
property_2005_2015<-read_excel("suburb_house_2015.xls", col_names=F, skip = 3) %>% 
            na.omit() %>%
            distinct() %>%
            mutate(`Property Type`="House") %>%
            bind_rows(suburb_Apartment_2015)
rm(suburb_Apartment_2015)


#rename the columns 
names(property_2005_2015)<-c("suburb", 2005:2015, "Prelim 2016", "Change 2014-2015 (%)", "Change 2005-2015 (%)", "Growth (%) PA 2005-2015","Property Type")

# assuming that "-" and "0" means data not available (NA), 
property_2005_2015[property_2005_2015 == 0] <- NA
property_2005_2015[property_2005_2015 == "-"] <- NA


## Read in the lat/long data:
# Also saved it to workind directory so can load it and work offline:
lat_long<-read_csv("Australian_Post_Codes_Lat_Lon.csv")  %>% 
    mutate(postcode=as.character(postcode)) %>%
    distinct() %>%
    select(-dc, -type)


#create VIC only lat_long
VIC_lat_long<- lat_long %>% 
    filter(state=="VIC") %>%
    select(-postcode) %>%
    distinct() %>% 
    filter(suburb %in%  property_2005_2015$suburb)

#Merge VIC_lat_long into property_2005_2015
property_2005_2015<- full_join(property_2005_2015, VIC_lat_long,  by=c("suburb"))
rm(VIC_lat_long)


property_2005_2015 <- property_2005_2015 %>%
    mutate(`Change 2014-2015 (%)` = as.numeric(`Change 2014-2015 (%)`),
           `Change 2005-2015 (%)` = as.numeric(`Change 2005-2015 (%)`),
           `Growth (%) PA 2005-2015` = as.numeric(`Growth (%) PA 2005-2015`),
           `Change 2005-2015 ($)` = as.numeric(`2015`)-as.numeric(`2005`)) %>%
     distinct()
    

#melt from wide to long format
property_2005_2015<-property_2005_2015 %>% 
    gather(key=Year, value=`Median Price ($)`, -suburb, -lon, -lat, -state, -`Property Type`, -`Change 2014-2015 (%)`, -`Change 2005-2015 (%)`, -`Growth (%) PA 2005-2015`, -`Change 2005-2015 ($)`) %>%
    mutate(`Median Price ($)`=as.numeric(`Median Price ($)`)) %>%
    distinct()






p1<-ggmap(Melbourne) +
    geom_point(data = property_2005_2015,
               aes(x =lon, y= lat, frame = Year, size=`Median Price ($)`,
                   colour = `Median Price ($)`), alpha=.75, shape="$") +
        # scale_colour_gradientn(colours=rainbow(5)) +
    scale_color_viridis() +
        scale_radius (range = c(5, 14), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("Median Melbourne Property \n Prices ($) from 2005-2016 \n")

p1 <- p1 + theme(aspect.ratio=1) +
        theme(axis.title.x = element_blank(),
            axis.text.x  =  element_blank(),
            axis.title.x = element_blank(),
            axis.ticks.x=element_blank(),
            axis.text.y  =  element_blank(),
            axis.title.y = element_blank(),
            axis.ticks.y=element_blank(),
            legend.title = element_text(size=14, face="bold"),
            legend.text = element_text(size = 14, face = "bold"),
            strip.text.x = element_text(size=14, face="bold"),
            plot.title = element_text(size = 16, face = "bold"),
            legend.position="right")

#Save the .gif to working directory
# gg_animate(p1, 'Melbourne.gif', ani.width = 500, ani.height = 300, interval = 0.75)
# 
# #show the gif in the graphics pannel
# gg_animate(p1)


##-Scroll your mouse to zoom in and out on the maps ##-Hover over a point to see more information about that suburb ##-Click on a suburb to go to a new google maps window/tab

#####create column with text to appear with 'tooltip' mouse hover###
#Growth (%) PA 2005-2015`
property_2005_2015$tooltip_point1 <- paste0(
    "<b>", property_2005_2015$suburb, "</b>",
    "<br>Growth per annum:",
    "<br>",property_2005_2015$`Growth (%) PA 2005-2015`,"%",
    "</span></div>")   
#`Change 2005-2015 (%)`
property_2005_2015$tooltip_point2 <- paste0(
    "<b>", property_2005_2015$suburb, "</b>",
    "<br>Change in Median Price:",
    "<br>",property_2005_2015$`Change 2005-2015 (%)`,"%",
    "</span></div>")   

property_2005_2015$tooltip_point3 <- paste0(
    "<b>", property_2005_2015$suburb, "</b>",
    "<br>Change in Median Price:",
    "<br>$",property_2005_2015$`Change 2005-2015 ($)`,
    "</span></div>")   


# create an 'onclick' column
property_2005_2015<-property_2005_2015 %>% mutate(google_map_lookup = paste(suburb, state, "Australia", sep=", "))
property_2005_2015$onclick <- sprintf("window.open(\"%s%s\")",
  "https://www.google.com.au/maps/place/", as.character(property_2005_2015$google_map_lookup))



#Make plots
p2<-ggmap(Melbourne) + 
    geom_point_interactive(data = property_2005_2015,
                           aes(x =lon, y= lat,
                               size= `Growth (%) PA 2005-2015`, 
                               colour = `Growth (%) PA 2005-2015`, 
                               tooltip=tooltip_point1, 
                               data_id = tooltip_point1,
                               onclick = onclick)) +
    scale_color_viridis() +
    scale_radius (range = c(1, 5), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("Growth per annum in Median Melbourne \n Property Prices from 2005-2015 \n") +
    theme(axis.title.x = element_blank(), 
        axis.text.x  =  element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y  =  element_blank(), 
        axis.title.y = element_blank(),
        axis.ticks.y=element_blank(),
        legend.title = element_text(face="bold"),
        legend.text = element_text(face = "bold"),
        strip.text.x = element_text(face="bold"),
        plot.title = element_text(face = "bold"))  
ggiraph(code = {print(p2)}, 
        zoom_max = 5,
        width_svg = 8, height_svg = 6,
        tooltip_offx = 20, tooltip_offy = -10, 
        hover_css = "fill:black;",
        tooltip_opacity = 0.7)



p3<-ggmap(Melbourne) + 
    geom_point_interactive(data = property_2005_2015,
                           aes(x =lon, y= lat,
                               size= `Change 2005-2015 (%)`, 
                               colour = `Change 2005-2015 (%)`, 
                               tooltip=tooltip_point2, 
                               data_id = tooltip_point2,
                               onclick = onclick)) +
    scale_color_viridis() +
    scale_radius (range = c(1, 6), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("Total change (%) in Median Melbourne \n Property Prices from 2005-2015 \n") +
    theme(axis.title.x = element_blank(), 
        axis.text.x  =  element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y  =  element_blank(), 
        axis.title.y = element_blank(),
        axis.ticks.y=element_blank(),
        legend.title = element_text(face="bold"),
        legend.text = element_text(face = "bold"),
        strip.text.x = element_text(face="bold"),
        plot.title = element_text(face = "bold"))  
ggiraph(code = {print(p3)}, 
        zoom_max = 5,
        width_svg = 8, height_svg = 6,
        tooltip_offx = 20, tooltip_offy = -10, 
        hover_css = "fill:black;",
        tooltip_opacity = 0.7)



p4<-ggmap(Melbourne) + 
    geom_point_interactive(data=property_2005_2015, 
                                aes(x =lon, y= lat,
                                size= `Change 2005-2015 ($)`, 
                                colour = `Change 2005-2015 ($)`, 
                                tooltip=tooltip_point3, 
                                data_id = tooltip_point3,
                                onclick = onclick)) +
    scale_color_viridis() +
    scale_radius (range = c(2, 8), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("Total change ($) in Median Melbourne \n Property Prices from 2005-2015 \n") +
    theme(axis.title.x = element_blank(), 
        axis.text.x  =  element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y  =  element_blank(), 
        axis.title.y = element_blank(),
        axis.ticks.y=element_blank(),
        legend.title = element_text(face="bold"),
        legend.text = element_text(face = "bold"),
        strip.text.x = element_text(face="bold"),
        plot.title = element_text(face = "bold"))  
ggiraph(code = {print(p4)}, 
        zoom_max = 5,
        width_svg = 8, height_svg = 6,
        tooltip_offx = 20, tooltip_offy = -10, 
        hover_css = "fill:black;",
        tooltip_opacity = 0.7)