This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Load necessary libraries
if (!require(tidyverse)) install.packages("tidyverse")
library(tidyverse)
library(lubridate) # For date parsing
# Step 1: Reload the data, treating the first row as the header
data <- read_csv("Rentaldata.csv", skip = 1, col_names = FALSE)
## Rows: 44 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (7): X1, X2, X3, X4, X5, X6, X7
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Step 2: Assign appropriate column names
colnames(data) <- c("Date", "Sydney", "Melbourne", "Brisbane",
"Adelaide", "Perth", "Australia")
# Step 3: Convert the columns to numeric (except 'Date')
data <- data %>%
mutate(across(-Date, as.numeric))
## Warning: There were 6 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `across(-Date, as.numeric)`.
## Caused by warning:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 5 remaining warnings.
# Step 4: Parse the 'Date' column correctly
data <- data %>%
mutate(Date = my(Date)) # Convert dates from 'Jun-14' format
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Date = my(Date)`.
## Caused by warning:
## ! 1 failed to parse.
# Step 5: Convert Data to Long Format for ggplot2
data_long <- data %>%
pivot_longer(
cols = -Date,
names_to = "City",
values_to = "Percentage_Change"
)
# Step 6: Generate the Multi-Line Plot
ggplot(data_long, aes(x = Date, y = Percentage_Change, color = City, group = City)) +
geom_line(size = 1) +
labs(
title = "Rents Annual Movement (%) Across Australian Cities",
x = "Date",
y = "Percentage Change (%)"
) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-axis labels
legend.position = "bottom" # Move legend to the bottom
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_line()`).
# Save the plot as a PNG file
ggsave("australian_cities_rent_trend.png", width = 10, height = 6)
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_line()`).
print("Plot saved as 'australian_cities_rent_trend.png'")
## [1] "Plot saved as 'australian_cities_rent_trend.png'"
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.