Column

Hourse price movements

Suburbs by Value increments

Column

Price Category

Total Value

---
title: "Median House Value by Suburbs in Victoria 2012-2022"
author: "Hareendra Nilaweera |Data source: Victoria State Government" 
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(plotly)
library(tidyr)
library(dplyr)
library(readxl)
library(gapminder)
library(ggplot2)
library(googleVis)
library(leaflet)
library(highcharter)
library(dplyr)
library(viridisLite)
library(forecast)
library(treemap)
library(arules)
library(flexdashboard)
library(sf)
thm <- 
  hc_theme(
    colors = c("#1a6ecc", "#434348", "#90ed7d"),
    chart = list(
      backgroundColor = "transparent",
      style = list(fontFamily = "Source Sans Pro")
    ),
    xAxis = list(
      gridLineWidth = 1
    )
  )

# Read data from Excel file starting from the 6th row
data <- read_excel("Suburb_HouseV2022z.xlsx")
# Replace NA and - values with median of respective columns

# Replace NA and - values with median of respective columns
for (col in names(data)[-1]) {
  median_val <- median(as.numeric(data[[col]][data[[col]] != "-" & !is.na(data[[col]])]), na.rm = TRUE)
  data[[col]][is.na(data[[col]])] <- median_val
  data[[col]][data[[col]] == "-"] <- median_val
  data[[col]][data[[col]] == "NA"] <- median_val
}

 
```

## Column {data-width="600"}

### Hourse price movements

```{r}
# Reshape the data from wide to long format
data_long <- data %>%
  pivot_longer(cols = -locality, 
               names_to = "Year", 
               values_to = "Price") %>%
  mutate(locality = tolower(locality))
# Convert Price to numeric
data_long$Price <- as.numeric(data_long$Price)

# Add categorical variable based on price
data_long$Category <- cut(data_long$Price, 
                             breaks = c(0, 500000, 750000, 1000000, 1250000, 1500000, 1750000, 2000000, Inf),
                             labels = c("0-500k", "500k-750k", "750k-1M", "1M-1.25M", "1.25M-1.5M", "1.5M-1.75M", "1.75M-2M", "Over 2M"))
# remove outlier 
data_long_filt<- data_long %>% filter(locality != "toorak")

p01 <- plot_ly(data_long_filt, x = ~Price, y = ~locality, color = ~Category,
               size = ~Price, frame = ~Year, alpha = 2) %>% 
  
  add_trace(type = "scatter", mode = "markers") %>% 
  
  layout( title = "Median house price in Victoria",
    yaxis = list(zeroline = FALSE,showticklabels = FALSE, title = "Suburbs"),
         xaxis = list(zeroline = FALSE, title = "Median Price"))
p01

```

### Suburbs by Value increments

```{r}
# Calculate the increase in value from 2012 to 2022
# Calculate the increase in value from 2012 to 2022
data$ValueIncrease <- as.numeric(data$`2022`) - as.numeric(data$`2012`)

# Create categorical variable based on ValueIncrease
data$ValueCategory <- cut(data$ValueIncrease,
                          breaks = c(-Inf, 0.5e6, 1e6, 1.5e6, 2e6, Inf),
                          labels = c("0-0.5M", "0.5M-1M", "1M-1.5M", "1.5M-2M", "over 2M"),
                          include.lowest = TRUE)

# Select the location where the ASGS files are saved
AUS_STATE_VIC_shp <- read_sf("vic_localities.shp") %>%
  st_transform(crs = "+proj=longlat +datum=WGS84")  # Transform to WGS84


AUS_STATE_VIC_shp_f <- ggplot2::fortify(AUS_STATE_VIC_shp, region = "LOC_NAME")
# Rename first column to merge
data <- data %>% rename(LOC_NAME = 1)




# Convert the SA2_NAME16 column to lowercase in both datasets
AUS_STATE_VIC_shp_f <- AUS_STATE_VIC_shp_f %>%
  mutate(LOC_NAME = tolower(LOC_NAME))

data <- data %>%
  mutate(LOC_NAME = tolower(LOC_NAME))

# Perform the merge
merge.lga.profiles3 <- sp::merge(AUS_STATE_VIC_shp_f, data, 
                                 by = "LOC_NAME", duplicateGeoms = TRUE)

# Define bins for the color palette
bins <- c(0, 0.5e6, 1e6, 1.5e6, 2e6, Inf)

pal <- colorBin(
  "YlOrRd",
  domain = merge.lga.profiles3$ValueIncrease,
  bins = bins,
  pretty = FALSE
)

p3 <- leaflet(merge.lga.profiles3) %>% 
  setView(lng = 147, lat = -36.5, zoom = 8)

labels <- sprintf(
  "<strong>%s</strong><br/>%g Value Increase",
  merge.lga.profiles3$LOC_NAME, 
  merge.lga.profiles3$ValueIncrease
) %>% lapply(htmltools::HTML)

p3 %>% 
  addPolygons(
    fillColor = ~pal(ValueIncrease),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 3,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")
  ) %>%
  addLegend(
    pal = pal,
    values = ~ValueIncrease,
    opacity = 0.7,
    title = "Value Increase",
    position = "bottomright"
  ) 



```

## Column {.tabset data-width="400"}

### Price Category

```{r, fig.keep='none'}
# Count the number of localities in each category for each year
count_data <- data_long %>%
  group_by(Year, Category) %>%
  summarise(Total_Price = sum(Price, na.rm = TRUE),Count = n())

# Create plot showing the number of localities in each category for each year
p02 <- plot_ly(count_data, x = ~Year, y = ~Count, color = ~Category, 
               type = 'scatter', mode = 'line+markers',
               line = list(shape = 'linear')) %>%
  layout(title = "Number of Localities in Each Price Category Over the Years",
         xaxis = list(title = "Year"),
         yaxis = list(title = "Number of Localities"),
         legend = list(title = "Price Category"))

p02
```

### Total Value

```{r}
P03 <- plot_ly(count_data, x = ~Year, y = ~Total_Price, text = ~Category, type = 'scatter', mode = 'markers', size = ~Total_Price, color = ~Category, colors = 'Paired',
        #Choosing the range of the bubbles' sizes:
        sizes = c(10, 50),
        marker = list(opacity = 0.5, sizemode = 'diameter'))
P03 <- P03 %>% layout(title = 'Market share of each category',
         xaxis = list(showgrid = FALSE),
         yaxis = list(showgrid = FALSE),
         showlegend = TRUE)

P03
  
```