knitr::opts_chunk$set(echo = T,
warning = F,
message = F,
fig.align = "center")
## Load the required package: tidyverse
library(tidyverse)
## Read in the home_sales_sample.csv file as home_sales
home_sales <- read.csv("home_sales_sample.csv")
The home_sales data set has the information on 1,000 randomly sampled homes that were sold in 2022. There are six variables (columns):
id
: The unique identifier for the home in the data
setsale_date
: the date the home went up for saletown
: the town the home is inassessed_value
: The value of the property assessed by
the town for tax purposessale_amount
: How much the home sold for (in $)residential_type
: If the home is a Condo or
Single FamilyCreate a scatterplot of the sale_amount
by
assessed_value
with the points colored by
residential_type
and partially transparent. Add a straight
trend line. See what the graph should look like in Brightspace.
Save the graph as gg_q1
and display the graph in the
knitted document.
gg_q1 <-
ggplot(
data = home_sales,
mapping = aes(
x = assessed_value,
y = sale_amount,
color = residential_type
)
) +
# Scatter plot with partially see-thru points
geom_point(alpha = 0.5) +
# Adding the trend line
geom_smooth(
method = 'lm',
formula = y ~ x,
se = F,
color = 'black'
)
gg_q1
Create the graph seen in question 2 in Brightspace. Save the graph as
gg_q2
and display it in the knitted document.
gg_q2 <-
gg_q1 +
# Adding a classic theme
theme_classic() +
# Centering the title, making it larger
theme(
plot.title = element_text(hjust = 0.5,
size = 16),
plot.subtitle = element_text(hjust = 0.5,
size = 12),
plot.caption = element_text(face = 'italic'),
legend.position = 'bottom'
) +
labs(
title = 'Sale Price vs Assessed Values of Homes',
subtitle = 'Sold in 2022',
caption = 'Data: Kaggle.com',
x = 'Assessed Value',
y = 'Final Sale Price',
color = 'Home Type'
)
gg_q2
Using the graph created in question 2, create the graph seen in Brightspace for question three. The colors used for Condo and Single Family are tomato and steelblue, respectively. Make sure to pay close attention to the axes!
Save the graph as gg_q3
and make sure to display it in
the knitted document!
gg_q3 <-
gg_q2 +
# Changing the x and y-axis
scale_x_continuous(
labels = scales::label_dollar(),
breaks = seq(0, 2500000, 250000)
) +
scale_y_continuous(
labels = scales::label_dollar(),
breaks = seq(0, 5500000, 500000)
) +
scale_color_manual(
values = c('Condo' = 'tomato', 'Single Family' = 'steelblue')
)
gg_q3
Using gg_q3
, create two graphs, one for Condo and one
for Single Family, in the same plot. Hide the legend.
Since this is the final question, you don’t need to save the graph
gg_q3 +
# Creating small multiples
facet_wrap(
facets = vars(residential_type),
scales = 'free',
ncol = 1
) +
# Hiding the legend
theme(legend.position = 'none')