library(ggplot2) library(tidyr) library(dplyr) # Creating the dataframe df <- data.frame(year = 0:10, interest_rate = seq(0.05, 0.12, 0.01)) # Setting initial values df <- df %>% mutate(balance = 30000) # Calculating compound interest df <- df %>% mutate(balance = balance * (1 + interest_rate) ^ year) # Reshaping the data df <- df %>% pivot_longer(cols = -c(year, balance), names_to = "Interest Rate", values_to = "Rate") # Creating the line plot plot <- ggplot(df, aes(x = year, y = balance, color = Rate)) + geom_line() + xlab("Time (years)") + ylab("£ (Balance)") + ggtitle("Compound Interest by Different Interest Rates") # Saving the plot as a PDF ggsave("compound_interest_plot.pdf", plot, width = 8, height = 6)