Find working directory - If working on a local Rstudio

getwd()
## [1] "/cloud/project/Data_VIZ_Final"

Set working directory - If working on a local Rstudio

#setwd("/Users/jonathanfernandez/Downloads")

Install Packages

#install.packages("tidyverse")
#install.packages('psych')
#install.packages('scales')
#install.packages('corrplot')
#install.packages("DT")
install.packages("plotly")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)

Library Packages

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(psych)
## 
## Attaching package: 'psych'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(scales)
## 
## Attaching package: 'scales'
## 
## The following objects are masked from 'package:psych':
## 
##     alpha, rescale
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(ggplot2)
library(dplyr)
library(readr)
library(lubridate)
library(corrplot)
## corrplot 0.92 loaded
library(DT)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout

Read data

data <- read.csv('realtor-data.zip-2.csv')

Head function of the data

head(data)

Transform and pre process the data

# Omit all NA values
datat <- na.omit(data)
head(datat)
# Preprocess data (assuming date processing and extracting year if possible)
data_df <- datat %>%
  mutate(
    prev_sold_date = ymd(prev_sold_date),  # Convert to date format
    prev_sold_year = year(prev_sold_date)  # Extract year
  )
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `prev_sold_date = ymd(prev_sold_date)`.
## Caused by warning:
## ! All formats failed to parse. No formats found.

Filter the data for properties less than $10,000,000

# Filter data for prices less than $10,000,000
filtered_data <- datat %>% 
  filter(price < 10000000)


# Visualization 1: Distribution of Property Prices
ggplot(filtered_data, aes(x = price)) +
  geom_histogram(bins = 30, fill = "blue", color = "black") +
  scale_x_continuous(labels = comma, breaks = pretty_breaks(n = 10)) +
  coord_flip() +
  labs(title = "Distribution of Property Prices", x = "Price", y = "Frequency")

# Visualization 2: Relation between House Size and Price
ggplot(datat, aes(x = house_size, y = price)) +
  geom_point(alpha = 0.5, color = "green") +
  scale_x_continuous(labels = comma, breaks = pretty_breaks(n = 10)) +
  scale_y_continuous(labels = comma, breaks = pretty_breaks(n = 10)) +
  labs(title = "House Size vs. Price", x = "House Size (sq ft)", y = "Price")

# Visualization 3: Price Distribution by Number of Bedrooms
ggplot(datat, aes(x = as.factor(bed), y = price)) +
  geom_boxplot() +
  scale_y_continuous(labels = comma, breaks = pretty_breaks(n = 10))

  labs(title = "Price Distribution by Number of Bedrooms", x = "Number of Bedrooms", y = "Price")
## $x
## [1] "Number of Bedrooms"
## 
## $y
## [1] "Price"
## 
## $title
## [1] "Price Distribution by Number of Bedrooms"
## 
## attr(,"class")
## [1] "labels"
datat %>%
  group_by(state) %>%
  summarize(average_price = mean(price, na.rm = TRUE)) %>%
  ggplot(aes(x = reorder(state, average_price), y = average_price, fill = state)) +  # Add `fill = state` here
  geom_bar(stat = "identity") +  # Remove the fill argument from here
  scale_fill_viridis_d(option = "C") +  # Use a viridis color scale, which provides distinct colors
  coord_flip() +
  labs(title = "Average Property Prices by State", x = "State", y = "Average Price") +
  theme_minimal()  # Optional: Apply a minimal theme

data_df %>%
  group_by(state, bed) %>%
  summarize(average_price = mean(price, na.rm = TRUE)) %>%
  ggplot(aes(x = as.factor(bed), y = state, fill = average_price)) +
  geom_tile() +
  scale_fill_viridis_c() +
  labs(title = "Heatmap of Prices by State and Number of Bedrooms", x = "Number of Bedrooms", y = "State") +
  theme_minimal()
## `summarise()` has grouped output by 'state'. You can override using the
## `.groups` argument.

# 3. Correlation Heatmap
numeric_data <- datat %>% select(price, house_size, acre_lot, bed, bath)
cor_matrix <- cor(numeric_data, use = "complete.obs")
corrplot(cor_matrix, method = "circle")

# 4. Faceted Scatter Plot by State
datat %>%
  ggplot(aes(x = house_size, y = price)) +
  geom_point(alpha = 0.5) +
  facet_wrap(~state) +
  labs(title = "House Size vs. Price by State", x = "House Size (sq ft)", y = "Price") +
  theme_minimal()